nir: Add blend_const_color_rgba sysval
[mesa.git] / src / compiler / nir / nir_intrinsics.py
1 #
2 # Copyright (C) 2018 Red Hat
3 # Copyright (C) 2014 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23 #
24
25 # This file defines all the available intrinsics in one place.
26 #
27 # The Intrinsic class corresponds one-to-one with nir_intrinsic_info
28 # structure.
29
30 class Intrinsic(object):
31 """Class that represents all the information about an intrinsic opcode.
32 NOTE: this must be kept in sync with nir_intrinsic_info.
33 """
34 def __init__(self, name, src_components, dest_components,
35 indices, flags, sysval, bit_sizes):
36 """Parameters:
37
38 - name: the intrinsic name
39 - src_components: list of the number of components per src, 0 means
40 vectorized instruction with number of components given in the
41 num_components field in nir_intrinsic_instr.
42 - dest_components: number of destination components, -1 means no
43 dest, 0 means number of components given in num_components field
44 in nir_intrinsic_instr.
45 - indices: list of constant indicies
46 - flags: list of semantic flags
47 - sysval: is this a system-value intrinsic
48 - bit_sizes: allowed dest bit_sizes
49 """
50 assert isinstance(name, str)
51 assert isinstance(src_components, list)
52 if src_components:
53 assert isinstance(src_components[0], int)
54 assert isinstance(dest_components, int)
55 assert isinstance(indices, list)
56 if indices:
57 assert isinstance(indices[0], str)
58 assert isinstance(flags, list)
59 if flags:
60 assert isinstance(flags[0], str)
61 assert isinstance(sysval, bool)
62 if bit_sizes:
63 assert isinstance(bit_sizes[0], int)
64
65 self.name = name
66 self.num_srcs = len(src_components)
67 self.src_components = src_components
68 self.has_dest = (dest_components >= 0)
69 self.dest_components = dest_components
70 self.num_indices = len(indices)
71 self.indices = indices
72 self.flags = flags
73 self.sysval = sysval
74 self.bit_sizes = bit_sizes
75
76 #
77 # Possible indices:
78 #
79
80 # A constant 'base' value that is added to an offset src:
81 BASE = "NIR_INTRINSIC_BASE"
82 # For store instructions, a writemask:
83 WRMASK = "NIR_INTRINSIC_WRMASK"
84 # The stream-id for GS emit_vertex/end_primitive intrinsics:
85 STREAM_ID = "NIR_INTRINSIC_STREAM_ID"
86 # The clip-plane id for load_user_clip_plane intrinsics:
87 UCP_ID = "NIR_INTRINSIC_UCP_ID"
88 # The amount of data, starting from BASE, that this instruction
89 # may access. This is used to provide bounds if the offset is
90 # not constant.
91 RANGE = "NIR_INTRINSIC_RANGE"
92 # The vulkan descriptor set binding for vulkan_resource_index
93 # intrinsic
94 DESC_SET = "NIR_INTRINSIC_DESC_SET"
95 # The vulkan descriptor set binding for vulkan_resource_index
96 # intrinsic
97 BINDING = "NIR_INTRINSIC_BINDING"
98 # Component offset
99 COMPONENT = "NIR_INTRINSIC_COMPONENT"
100 # Interpolation mode (only meaningful for FS inputs)
101 INTERP_MODE = "NIR_INTRINSIC_INTERP_MODE"
102 # A binary nir_op to use when performing a reduction or scan operation
103 REDUCTION_OP = "NIR_INTRINSIC_REDUCTION_OP"
104 # Cluster size for reduction operations
105 CLUSTER_SIZE = "NIR_INTRINSIC_CLUSTER_SIZE"
106 # Parameter index for a load_param intrinsic
107 PARAM_IDX = "NIR_INTRINSIC_PARAM_IDX"
108 # Image dimensionality for image intrinsics
109 IMAGE_DIM = "NIR_INTRINSIC_IMAGE_DIM"
110 # Non-zero if we are accessing an array image
111 IMAGE_ARRAY = "NIR_INTRINSIC_IMAGE_ARRAY"
112 # Access qualifiers for image and memory access intrinsics
113 ACCESS = "NIR_INTRINSIC_ACCESS"
114 # Image format for image intrinsics
115 FORMAT = "NIR_INTRINSIC_FORMAT"
116 # Offset or address alignment
117 ALIGN_MUL = "NIR_INTRINSIC_ALIGN_MUL"
118 ALIGN_OFFSET = "NIR_INTRINSIC_ALIGN_OFFSET"
119 # The vulkan descriptor type for vulkan_resource_index
120 DESC_TYPE = "NIR_INTRINSIC_DESC_TYPE"
121
122 #
123 # Possible flags:
124 #
125
126 CAN_ELIMINATE = "NIR_INTRINSIC_CAN_ELIMINATE"
127 CAN_REORDER = "NIR_INTRINSIC_CAN_REORDER"
128
129 INTR_OPCODES = {}
130
131 # Defines a new NIR intrinsic. By default, the intrinsic will have no sources
132 # and no destination.
133 #
134 # You can set dest_comp=n to enable a destination for the intrinsic, in which
135 # case it will have that many components, or =0 for "as many components as the
136 # NIR destination value."
137 #
138 # Set src_comp=n to enable sources for the intruction. It can be an array of
139 # component counts, or (for convenience) a scalar component count if there's
140 # only one source. If a component count is 0, it will be as many components as
141 # the intrinsic has based on the dest_comp.
142 def intrinsic(name, src_comp=[], dest_comp=-1, indices=[],
143 flags=[], sysval=False, bit_sizes=[]):
144 assert name not in INTR_OPCODES
145 INTR_OPCODES[name] = Intrinsic(name, src_comp, dest_comp,
146 indices, flags, sysval, bit_sizes)
147
148 intrinsic("nop", flags=[CAN_ELIMINATE])
149
150 intrinsic("load_param", dest_comp=0, indices=[PARAM_IDX], flags=[CAN_ELIMINATE])
151
152 intrinsic("load_deref", dest_comp=0, src_comp=[-1],
153 indices=[ACCESS], flags=[CAN_ELIMINATE])
154 intrinsic("store_deref", src_comp=[-1, 0], indices=[WRMASK, ACCESS])
155 intrinsic("copy_deref", src_comp=[-1, -1])
156
157 # Interpolation of input. The interp_deref_at* intrinsics are similar to the
158 # load_var intrinsic acting on a shader input except that they interpolate the
159 # input differently. The at_sample and at_offset intrinsics take an
160 # additional source that is an integer sample id or a vec2 position offset
161 # respectively.
162
163 intrinsic("interp_deref_at_centroid", dest_comp=0, src_comp=[1],
164 flags=[ CAN_ELIMINATE, CAN_REORDER])
165 intrinsic("interp_deref_at_sample", src_comp=[1, 1], dest_comp=0,
166 flags=[CAN_ELIMINATE, CAN_REORDER])
167 intrinsic("interp_deref_at_offset", src_comp=[1, 2], dest_comp=0,
168 flags=[CAN_ELIMINATE, CAN_REORDER])
169
170 # Gets the length of an unsized array at the end of a buffer
171 intrinsic("deref_buffer_array_length", src_comp=[-1], dest_comp=1,
172 flags=[CAN_ELIMINATE, CAN_REORDER])
173
174 # Ask the driver for the size of a given buffer. It takes the buffer index
175 # as source.
176 intrinsic("get_buffer_size", src_comp=[-1], dest_comp=1,
177 flags=[CAN_ELIMINATE, CAN_REORDER])
178
179 # a barrier is an intrinsic with no inputs/outputs but which can't be moved
180 # around/optimized in general
181 def barrier(name):
182 intrinsic(name)
183
184 barrier("barrier")
185 barrier("discard")
186
187 # Memory barrier with semantics analogous to the memoryBarrier() GLSL
188 # intrinsic.
189 barrier("memory_barrier")
190
191 # Shader clock intrinsic with semantics analogous to the clock2x32ARB()
192 # GLSL intrinsic.
193 # The latter can be used as code motion barrier, which is currently not
194 # feasible with NIR.
195 intrinsic("shader_clock", dest_comp=2, flags=[CAN_ELIMINATE])
196
197 # Shader ballot intrinsics with semantics analogous to the
198 #
199 # ballotARB()
200 # readInvocationARB()
201 # readFirstInvocationARB()
202 #
203 # GLSL functions from ARB_shader_ballot.
204 intrinsic("ballot", src_comp=[1], dest_comp=0, flags=[CAN_ELIMINATE])
205 intrinsic("read_invocation", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
206 intrinsic("read_first_invocation", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
207
208 # Additional SPIR-V ballot intrinsics
209 #
210 # These correspond to the SPIR-V opcodes
211 #
212 # OpGroupUniformElect
213 # OpSubgroupFirstInvocationKHR
214 intrinsic("elect", dest_comp=1, flags=[CAN_ELIMINATE])
215 intrinsic("first_invocation", dest_comp=1, flags=[CAN_ELIMINATE])
216
217 # Memory barrier with semantics analogous to the compute shader
218 # groupMemoryBarrier(), memoryBarrierAtomicCounter(), memoryBarrierBuffer(),
219 # memoryBarrierImage() and memoryBarrierShared() GLSL intrinsics.
220 barrier("group_memory_barrier")
221 barrier("memory_barrier_atomic_counter")
222 barrier("memory_barrier_buffer")
223 barrier("memory_barrier_image")
224 barrier("memory_barrier_shared")
225 barrier("begin_invocation_interlock")
226 barrier("end_invocation_interlock")
227
228 # A conditional discard, with a single boolean source.
229 intrinsic("discard_if", src_comp=[1])
230
231 # ARB_shader_group_vote intrinsics
232 intrinsic("vote_any", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
233 intrinsic("vote_all", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
234 intrinsic("vote_feq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
235 intrinsic("vote_ieq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
236
237 # Ballot ALU operations from SPIR-V.
238 #
239 # These operations work like their ALU counterparts except that the operate
240 # on a uvec4 which is treated as a 128bit integer. Also, they are, in
241 # general, free to ignore any bits which are above the subgroup size.
242 intrinsic("ballot_bitfield_extract", src_comp=[4, 1], dest_comp=1, flags=[CAN_ELIMINATE])
243 intrinsic("ballot_bit_count_reduce", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
244 intrinsic("ballot_bit_count_inclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
245 intrinsic("ballot_bit_count_exclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
246 intrinsic("ballot_find_lsb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
247 intrinsic("ballot_find_msb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
248
249 # Shuffle operations from SPIR-V.
250 intrinsic("shuffle", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
251 intrinsic("shuffle_xor", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
252 intrinsic("shuffle_up", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
253 intrinsic("shuffle_down", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
254
255 # Quad operations from SPIR-V.
256 intrinsic("quad_broadcast", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
257 intrinsic("quad_swap_horizontal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
258 intrinsic("quad_swap_vertical", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
259 intrinsic("quad_swap_diagonal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
260
261 intrinsic("reduce", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP, CLUSTER_SIZE],
262 flags=[CAN_ELIMINATE])
263 intrinsic("inclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
264 flags=[CAN_ELIMINATE])
265 intrinsic("exclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
266 flags=[CAN_ELIMINATE])
267
268 # Basic Geometry Shader intrinsics.
269 #
270 # emit_vertex implements GLSL's EmitStreamVertex() built-in. It takes a single
271 # index, which is the stream ID to write to.
272 #
273 # end_primitive implements GLSL's EndPrimitive() built-in.
274 intrinsic("emit_vertex", indices=[STREAM_ID])
275 intrinsic("end_primitive", indices=[STREAM_ID])
276
277 # Geometry Shader intrinsics with a vertex count.
278 #
279 # Alternatively, drivers may implement these intrinsics, and use
280 # nir_lower_gs_intrinsics() to convert from the basic intrinsics.
281 #
282 # These maintain a count of the number of vertices emitted, as an additional
283 # unsigned integer source.
284 intrinsic("emit_vertex_with_counter", src_comp=[1], indices=[STREAM_ID])
285 intrinsic("end_primitive_with_counter", src_comp=[1], indices=[STREAM_ID])
286 intrinsic("set_vertex_count", src_comp=[1])
287
288 # Atomic counters
289 #
290 # The *_var variants take an atomic_uint nir_variable, while the other,
291 # lowered, variants take a constant buffer index and register offset.
292
293 def atomic(name, flags=[]):
294 intrinsic(name + "_deref", src_comp=[-1], dest_comp=1, flags=flags)
295 intrinsic(name, src_comp=[1], dest_comp=1, indices=[BASE], flags=flags)
296
297 def atomic2(name):
298 intrinsic(name + "_deref", src_comp=[-1, 1], dest_comp=1)
299 intrinsic(name, src_comp=[1, 1], dest_comp=1, indices=[BASE])
300
301 def atomic3(name):
302 intrinsic(name + "_deref", src_comp=[-1, 1, 1], dest_comp=1)
303 intrinsic(name, src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
304
305 atomic("atomic_counter_inc")
306 atomic("atomic_counter_pre_dec")
307 atomic("atomic_counter_post_dec")
308 atomic("atomic_counter_read", flags=[CAN_ELIMINATE])
309 atomic2("atomic_counter_add")
310 atomic2("atomic_counter_min")
311 atomic2("atomic_counter_max")
312 atomic2("atomic_counter_and")
313 atomic2("atomic_counter_or")
314 atomic2("atomic_counter_xor")
315 atomic2("atomic_counter_exchange")
316 atomic3("atomic_counter_comp_swap")
317
318 # Image load, store and atomic intrinsics.
319 #
320 # All image intrinsics come in three versions. One which take an image target
321 # passed as a deref chain as the first source, one which takes an index as the
322 # first source, and one which takes a bindless handle as the first source.
323 # In the first version, the image variable contains the memory and layout
324 # qualifiers that influence the semantics of the intrinsic. In the second and
325 # third, the image format and access qualifiers are provided as constant
326 # indices.
327 #
328 # All image intrinsics take a four-coordinate vector and a sample index as
329 # 2nd and 3rd sources, determining the location within the image that will be
330 # accessed by the intrinsic. Components not applicable to the image target
331 # in use are undefined. Image store takes an additional four-component
332 # argument with the value to be written, and image atomic operations take
333 # either one or two additional scalar arguments with the same meaning as in
334 # the ARB_shader_image_load_store specification.
335 def image(name, src_comp=[], **kwargs):
336 intrinsic("image_deref_" + name, src_comp=[1] + src_comp, **kwargs)
337 intrinsic("image_" + name, src_comp=[1] + src_comp,
338 indices=[IMAGE_DIM, IMAGE_ARRAY, FORMAT, ACCESS], **kwargs)
339 intrinsic("bindless_image_" + name, src_comp=[1] + src_comp,
340 indices=[IMAGE_DIM, IMAGE_ARRAY, FORMAT, ACCESS], **kwargs)
341
342 image("load", src_comp=[4, 1], dest_comp=0, flags=[CAN_ELIMINATE])
343 image("store", src_comp=[4, 1, 0])
344 image("atomic_add", src_comp=[4, 1, 1], dest_comp=1)
345 image("atomic_min", src_comp=[4, 1, 1], dest_comp=1)
346 image("atomic_max", src_comp=[4, 1, 1], dest_comp=1)
347 image("atomic_and", src_comp=[4, 1, 1], dest_comp=1)
348 image("atomic_or", src_comp=[4, 1, 1], dest_comp=1)
349 image("atomic_xor", src_comp=[4, 1, 1], dest_comp=1)
350 image("atomic_exchange", src_comp=[4, 1, 1], dest_comp=1)
351 image("atomic_comp_swap", src_comp=[4, 1, 1, 1], dest_comp=1)
352 image("atomic_fadd", src_comp=[1, 4, 1, 1], dest_comp=1)
353 image("size", dest_comp=0, flags=[CAN_ELIMINATE, CAN_REORDER])
354 image("samples", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
355
356 # Intel-specific query for loading from the brw_image_param struct passed
357 # into the shader as a uniform. The variable is a deref to the image
358 # variable. The const index specifies which of the six parameters to load.
359 intrinsic("image_deref_load_param_intel", src_comp=[1], dest_comp=0,
360 indices=[BASE], flags=[CAN_ELIMINATE, CAN_REORDER])
361 image("load_raw_intel", src_comp=[1], dest_comp=0,
362 flags=[CAN_ELIMINATE])
363 image("store_raw_intel", src_comp=[1, 0])
364
365 # Vulkan descriptor set intrinsics
366 #
367 # The Vulkan API uses a different binding model from GL. In the Vulkan
368 # API, all external resources are represented by a tuple:
369 #
370 # (descriptor set, binding, array index)
371 #
372 # where the array index is the only thing allowed to be indirect. The
373 # vulkan_surface_index intrinsic takes the descriptor set and binding as
374 # its first two indices and the array index as its source. The third
375 # index is a nir_variable_mode in case that's useful to the backend.
376 #
377 # The intended usage is that the shader will call vulkan_surface_index to
378 # get an index and then pass that as the buffer index ubo/ssbo calls.
379 #
380 # The vulkan_resource_reindex intrinsic takes a resource index in src0
381 # (the result of a vulkan_resource_index or vulkan_resource_reindex) which
382 # corresponds to the tuple (set, binding, index) and computes an index
383 # corresponding to tuple (set, binding, idx + src1).
384 intrinsic("vulkan_resource_index", src_comp=[1], dest_comp=0,
385 indices=[DESC_SET, BINDING, DESC_TYPE],
386 flags=[CAN_ELIMINATE, CAN_REORDER])
387 intrinsic("vulkan_resource_reindex", src_comp=[0, 1], dest_comp=0,
388 indices=[DESC_TYPE], flags=[CAN_ELIMINATE, CAN_REORDER])
389 intrinsic("load_vulkan_descriptor", src_comp=[-1], dest_comp=0,
390 indices=[DESC_TYPE], flags=[CAN_ELIMINATE, CAN_REORDER])
391
392 # variable atomic intrinsics
393 #
394 # All of these variable atomic memory operations read a value from memory,
395 # compute a new value using one of the operations below, write the new value
396 # to memory, and return the original value read.
397 #
398 # All operations take 2 sources except CompSwap that takes 3. These sources
399 # represent:
400 #
401 # 0: A deref to the memory on which to perform the atomic
402 # 1: The data parameter to the atomic function (i.e. the value to add
403 # in shared_atomic_add, etc).
404 # 2: For CompSwap only: the second data parameter.
405 intrinsic("deref_atomic_add", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
406 intrinsic("deref_atomic_imin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
407 intrinsic("deref_atomic_umin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
408 intrinsic("deref_atomic_imax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
409 intrinsic("deref_atomic_umax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
410 intrinsic("deref_atomic_and", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
411 intrinsic("deref_atomic_or", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
412 intrinsic("deref_atomic_xor", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
413 intrinsic("deref_atomic_exchange", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
414 intrinsic("deref_atomic_comp_swap", src_comp=[-1, 1, 1], dest_comp=1, indices=[ACCESS])
415 intrinsic("deref_atomic_fadd", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
416 intrinsic("deref_atomic_fmin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
417 intrinsic("deref_atomic_fmax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
418 intrinsic("deref_atomic_fcomp_swap", src_comp=[-1, 1, 1], dest_comp=1, indices=[ACCESS])
419
420 # SSBO atomic intrinsics
421 #
422 # All of the SSBO atomic memory operations read a value from memory,
423 # compute a new value using one of the operations below, write the new
424 # value to memory, and return the original value read.
425 #
426 # All operations take 3 sources except CompSwap that takes 4. These
427 # sources represent:
428 #
429 # 0: The SSBO buffer index.
430 # 1: The offset into the SSBO buffer of the variable that the atomic
431 # operation will operate on.
432 # 2: The data parameter to the atomic function (i.e. the value to add
433 # in ssbo_atomic_add, etc).
434 # 3: For CompSwap only: the second data parameter.
435 intrinsic("ssbo_atomic_add", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
436 intrinsic("ssbo_atomic_imin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
437 intrinsic("ssbo_atomic_umin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
438 intrinsic("ssbo_atomic_imax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
439 intrinsic("ssbo_atomic_umax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
440 intrinsic("ssbo_atomic_and", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
441 intrinsic("ssbo_atomic_or", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
442 intrinsic("ssbo_atomic_xor", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
443 intrinsic("ssbo_atomic_exchange", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
444 intrinsic("ssbo_atomic_comp_swap", src_comp=[1, 1, 1, 1], dest_comp=1, indices=[ACCESS])
445 intrinsic("ssbo_atomic_fadd", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
446 intrinsic("ssbo_atomic_fmin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
447 intrinsic("ssbo_atomic_fmax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
448 intrinsic("ssbo_atomic_fcomp_swap", src_comp=[1, 1, 1, 1], dest_comp=1, indices=[ACCESS])
449
450 # CS shared variable atomic intrinsics
451 #
452 # All of the shared variable atomic memory operations read a value from
453 # memory, compute a new value using one of the operations below, write the
454 # new value to memory, and return the original value read.
455 #
456 # All operations take 2 sources except CompSwap that takes 3. These
457 # sources represent:
458 #
459 # 0: The offset into the shared variable storage region that the atomic
460 # operation will operate on.
461 # 1: The data parameter to the atomic function (i.e. the value to add
462 # in shared_atomic_add, etc).
463 # 2: For CompSwap only: the second data parameter.
464 intrinsic("shared_atomic_add", src_comp=[1, 1], dest_comp=1, indices=[BASE])
465 intrinsic("shared_atomic_imin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
466 intrinsic("shared_atomic_umin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
467 intrinsic("shared_atomic_imax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
468 intrinsic("shared_atomic_umax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
469 intrinsic("shared_atomic_and", src_comp=[1, 1], dest_comp=1, indices=[BASE])
470 intrinsic("shared_atomic_or", src_comp=[1, 1], dest_comp=1, indices=[BASE])
471 intrinsic("shared_atomic_xor", src_comp=[1, 1], dest_comp=1, indices=[BASE])
472 intrinsic("shared_atomic_exchange", src_comp=[1, 1], dest_comp=1, indices=[BASE])
473 intrinsic("shared_atomic_comp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
474 intrinsic("shared_atomic_fadd", src_comp=[1, 1], dest_comp=1, indices=[BASE])
475 intrinsic("shared_atomic_fmin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
476 intrinsic("shared_atomic_fmax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
477 intrinsic("shared_atomic_fcomp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
478
479 # Global atomic intrinsics
480 #
481 # All of the shared variable atomic memory operations read a value from
482 # memory, compute a new value using one of the operations below, write the
483 # new value to memory, and return the original value read.
484 #
485 # All operations take 2 sources except CompSwap that takes 3. These
486 # sources represent:
487 #
488 # 0: The memory address that the atomic operation will operate on.
489 # 1: The data parameter to the atomic function (i.e. the value to add
490 # in shared_atomic_add, etc).
491 # 2: For CompSwap only: the second data parameter.
492 intrinsic("global_atomic_add", src_comp=[1, 1], dest_comp=1, indices=[BASE])
493 intrinsic("global_atomic_imin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
494 intrinsic("global_atomic_umin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
495 intrinsic("global_atomic_imax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
496 intrinsic("global_atomic_umax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
497 intrinsic("global_atomic_and", src_comp=[1, 1], dest_comp=1, indices=[BASE])
498 intrinsic("global_atomic_or", src_comp=[1, 1], dest_comp=1, indices=[BASE])
499 intrinsic("global_atomic_xor", src_comp=[1, 1], dest_comp=1, indices=[BASE])
500 intrinsic("global_atomic_exchange", src_comp=[1, 1], dest_comp=1, indices=[BASE])
501 intrinsic("global_atomic_comp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
502 intrinsic("global_atomic_fadd", src_comp=[1, 1], dest_comp=1, indices=[BASE])
503 intrinsic("global_atomic_fmin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
504 intrinsic("global_atomic_fmax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
505 intrinsic("global_atomic_fcomp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
506
507 def system_value(name, dest_comp, indices=[], bit_sizes=[32]):
508 intrinsic("load_" + name, [], dest_comp, indices,
509 flags=[CAN_ELIMINATE, CAN_REORDER], sysval=True,
510 bit_sizes=bit_sizes)
511
512 system_value("frag_coord", 4)
513 system_value("front_face", 1, bit_sizes=[1, 32])
514 system_value("vertex_id", 1)
515 system_value("vertex_id_zero_base", 1)
516 system_value("first_vertex", 1)
517 system_value("is_indexed_draw", 1)
518 system_value("base_vertex", 1)
519 system_value("instance_id", 1)
520 system_value("base_instance", 1)
521 system_value("draw_id", 1)
522 system_value("sample_id", 1)
523 # sample_id_no_per_sample is like sample_id but does not imply per-
524 # sample shading. See the lower_helper_invocation option.
525 system_value("sample_id_no_per_sample", 1)
526 system_value("sample_pos", 2)
527 system_value("sample_mask_in", 1)
528 system_value("primitive_id", 1)
529 system_value("invocation_id", 1)
530 system_value("tess_coord", 3)
531 system_value("tess_level_outer", 4)
532 system_value("tess_level_inner", 2)
533 system_value("patch_vertices_in", 1)
534 system_value("local_invocation_id", 3)
535 system_value("local_invocation_index", 1)
536 system_value("work_group_id", 3)
537 system_value("user_clip_plane", 4, indices=[UCP_ID])
538 system_value("num_work_groups", 3)
539 system_value("helper_invocation", 1, bit_sizes=[1, 32])
540 system_value("alpha_ref_float", 1)
541 system_value("layer_id", 1)
542 system_value("view_index", 1)
543 system_value("subgroup_size", 1)
544 system_value("subgroup_invocation", 1)
545 system_value("subgroup_eq_mask", 0, bit_sizes=[32, 64])
546 system_value("subgroup_ge_mask", 0, bit_sizes=[32, 64])
547 system_value("subgroup_gt_mask", 0, bit_sizes=[32, 64])
548 system_value("subgroup_le_mask", 0, bit_sizes=[32, 64])
549 system_value("subgroup_lt_mask", 0, bit_sizes=[32, 64])
550 system_value("num_subgroups", 1)
551 system_value("subgroup_id", 1)
552 system_value("local_group_size", 3)
553 system_value("global_invocation_id", 3, bit_sizes=[32, 64])
554 system_value("global_invocation_index", 1, bit_sizes=[32, 64])
555 system_value("work_dim", 1)
556 # Driver-specific viewport scale/offset parameters.
557 #
558 # VC4 and V3D need to emit a scaled version of the position in the vertex
559 # shaders for binning, and having system values lets us move the math for that
560 # into NIR.
561 #
562 # Panfrost needs to implement all coordinate transformation in the
563 # vertex shader; system values allow us to share this routine in NIR.
564 system_value("viewport_x_scale", 1)
565 system_value("viewport_y_scale", 1)
566 system_value("viewport_z_scale", 1)
567 system_value("viewport_z_offset", 1)
568 system_value("viewport_scale", 3)
569 system_value("viewport_offset", 3)
570
571 # Blend constant color values. Float values are clamped. Vectored versions are
572 # provided as well for driver convenience
573
574 system_value("blend_const_color_r_float", 1)
575 system_value("blend_const_color_g_float", 1)
576 system_value("blend_const_color_b_float", 1)
577 system_value("blend_const_color_a_float", 1)
578 system_value("blend_const_color_rgba", 4)
579 system_value("blend_const_color_rgba8888_unorm", 1)
580 system_value("blend_const_color_aaaa8888_unorm", 1)
581
582 # Barycentric coordinate intrinsics.
583 #
584 # These set up the barycentric coordinates for a particular interpolation.
585 # The first three are for the simple cases: pixel, centroid, or per-sample
586 # (at gl_SampleID). The next two handle interpolating at a specified
587 # sample location, or interpolating with a vec2 offset,
588 #
589 # The interp_mode index should be either the INTERP_MODE_SMOOTH or
590 # INTERP_MODE_NOPERSPECTIVE enum values.
591 #
592 # The vec2 value produced by these intrinsics is intended for use as the
593 # barycoord source of a load_interpolated_input intrinsic.
594
595 def barycentric(name, src_comp=[]):
596 intrinsic("load_barycentric_" + name, src_comp=src_comp, dest_comp=2,
597 indices=[INTERP_MODE], flags=[CAN_ELIMINATE, CAN_REORDER])
598
599 # no sources.
600 barycentric("pixel")
601 barycentric("centroid")
602 barycentric("sample")
603 # src[] = { sample_id }.
604 barycentric("at_sample", [1])
605 # src[] = { offset.xy }.
606 barycentric("at_offset", [2])
607
608 # Load sample position:
609 #
610 # Takes a sample # and returns a sample position. Used for lowering
611 # interpolateAtSample() to interpolateAtOffset()
612 intrinsic("load_sample_pos_from_id", src_comp=[1], dest_comp=2,
613 flags=[CAN_ELIMINATE, CAN_REORDER])
614
615 # Loads what I believe is the primitive size, for scaling ij to pixel size:
616 intrinsic("load_size_ir3", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
617
618 # Load operations pull data from some piece of GPU memory. All load
619 # operations operate in terms of offsets into some piece of theoretical
620 # memory. Loads from externally visible memory (UBO and SSBO) simply take a
621 # byte offset as a source. Loads from opaque memory (uniforms, inputs, etc.)
622 # take a base+offset pair where the nir_intrinsic_base() gives the location
623 # of the start of the variable being loaded and and the offset source is a
624 # offset into that variable.
625 #
626 # Uniform load operations have a nir_intrinsic_range() index that specifies the
627 # range (starting at base) of the data from which we are loading. If
628 # range == 0, then the range is unknown.
629 #
630 # Some load operations such as UBO/SSBO load and per_vertex loads take an
631 # additional source to specify which UBO/SSBO/vertex to load from.
632 #
633 # The exact address type depends on the lowering pass that generates the
634 # load/store intrinsics. Typically, this is vec4 units for things such as
635 # varying slots and float units for fragment shader inputs. UBO and SSBO
636 # offsets are always in bytes.
637
638 def load(name, num_srcs, indices=[], flags=[]):
639 intrinsic("load_" + name, [1] * num_srcs, dest_comp=0, indices=indices,
640 flags=flags)
641
642 # src[] = { offset }.
643 load("uniform", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
644 # src[] = { buffer_index, offset }.
645 load("ubo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], flags=[CAN_ELIMINATE, CAN_REORDER])
646 # src[] = { offset }.
647 load("input", 1, [BASE, COMPONENT], [CAN_ELIMINATE, CAN_REORDER])
648 # src[] = { vertex, offset }.
649 load("per_vertex_input", 2, [BASE, COMPONENT], [CAN_ELIMINATE, CAN_REORDER])
650 # src[] = { barycoord, offset }.
651 intrinsic("load_interpolated_input", src_comp=[2, 1], dest_comp=0,
652 indices=[BASE, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER])
653
654 # src[] = { buffer_index, offset }.
655 load("ssbo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
656 # src[] = { offset }.
657 load("output", 1, [BASE, COMPONENT], flags=[CAN_ELIMINATE])
658 # src[] = { vertex, offset }.
659 load("per_vertex_output", 2, [BASE, COMPONENT], [CAN_ELIMINATE])
660 # src[] = { offset }.
661 load("shared", 1, [BASE, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
662 # src[] = { offset }.
663 load("push_constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
664 # src[] = { offset }.
665 load("constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
666 # src[] = { address }.
667 load("global", 1, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
668 # src[] = { address }.
669 load("kernel_input", 1, [BASE, RANGE, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE, CAN_REORDER])
670 # src[] = { offset }.
671 load("scratch", 1, [ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
672
673 # Stores work the same way as loads, except now the first source is the value
674 # to store and the second (and possibly third) source specify where to store
675 # the value. SSBO and shared memory stores also have a
676 # nir_intrinsic_write_mask()
677
678 def store(name, num_srcs, indices=[], flags=[]):
679 intrinsic("store_" + name, [0] + ([1] * (num_srcs - 1)), indices=indices, flags=flags)
680
681 # src[] = { value, offset }.
682 store("output", 2, [BASE, WRMASK, COMPONENT])
683 # src[] = { value, vertex, offset }.
684 store("per_vertex_output", 3, [BASE, WRMASK, COMPONENT])
685 # src[] = { value, block_index, offset }
686 store("ssbo", 3, [WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
687 # src[] = { value, offset }.
688 store("shared", 2, [BASE, WRMASK, ALIGN_MUL, ALIGN_OFFSET])
689 # src[] = { value, address }.
690 store("global", 2, [WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
691 # src[] = { value, offset }.
692 store("scratch", 2, [ALIGN_MUL, ALIGN_OFFSET, WRMASK])
693
694 # IR3-specific version of most SSBO intrinsics. The only different
695 # compare to the originals is that they add an extra source to hold
696 # the dword-offset, which is needed by the backend code apart from
697 # the byte-offset already provided by NIR in one of the sources.
698 #
699 # NIR lowering pass 'ir3_nir_lower_io_offset' will replace the
700 # original SSBO intrinsics by these, placing the computed
701 # dword-offset always in the last source.
702 #
703 # The float versions are not handled because those are not supported
704 # by the backend.
705 intrinsic("store_ssbo_ir3", src_comp=[0, 1, 1, 1],
706 indices=[WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
707 intrinsic("load_ssbo_ir3", src_comp=[1, 1, 1], dest_comp=0,
708 indices=[ACCESS, ALIGN_MUL, ALIGN_OFFSET], flags=[CAN_ELIMINATE])
709 intrinsic("ssbo_atomic_add_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
710 intrinsic("ssbo_atomic_imin_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
711 intrinsic("ssbo_atomic_umin_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
712 intrinsic("ssbo_atomic_imax_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
713 intrinsic("ssbo_atomic_umax_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
714 intrinsic("ssbo_atomic_and_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
715 intrinsic("ssbo_atomic_or_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
716 intrinsic("ssbo_atomic_xor_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
717 intrinsic("ssbo_atomic_exchange_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
718 intrinsic("ssbo_atomic_comp_swap_ir3", src_comp=[1, 1, 1, 1, 1], dest_comp=1)