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