nir: Add explicit signs to image min/max intrinsics
[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 # Demote fragment shader invocation to a helper invocation. Any stores to
194 # memory after this instruction are suppressed and the fragment does not write
195 # outputs to the framebuffer. Unlike discard, demote needs to ensure that
196 # derivatives will still work for invocations that were not demoted.
197 #
198 # As specified by SPV_EXT_demote_to_helper_invocation.
199 barrier("demote")
200 intrinsic("is_helper_invocation", dest_comp=1, flags=[CAN_ELIMINATE])
201
202
203 # Memory barrier with semantics analogous to the memoryBarrier() GLSL
204 # intrinsic.
205 barrier("memory_barrier")
206
207 # Shader clock intrinsic with semantics analogous to the clock2x32ARB()
208 # GLSL intrinsic.
209 # The latter can be used as code motion barrier, which is currently not
210 # feasible with NIR.
211 intrinsic("shader_clock", dest_comp=2, flags=[CAN_ELIMINATE])
212
213 # Shader ballot intrinsics with semantics analogous to the
214 #
215 # ballotARB()
216 # readInvocationARB()
217 # readFirstInvocationARB()
218 #
219 # GLSL functions from ARB_shader_ballot.
220 intrinsic("ballot", src_comp=[1], dest_comp=0, flags=[CAN_ELIMINATE])
221 intrinsic("read_invocation", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
222 intrinsic("read_first_invocation", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
223
224 # Additional SPIR-V ballot intrinsics
225 #
226 # These correspond to the SPIR-V opcodes
227 #
228 # OpGroupUniformElect
229 # OpSubgroupFirstInvocationKHR
230 intrinsic("elect", dest_comp=1, flags=[CAN_ELIMINATE])
231 intrinsic("first_invocation", dest_comp=1, flags=[CAN_ELIMINATE])
232
233 # Memory barrier with semantics analogous to the compute shader
234 # groupMemoryBarrier(), memoryBarrierAtomicCounter(), memoryBarrierBuffer(),
235 # memoryBarrierImage() and memoryBarrierShared() GLSL intrinsics.
236 barrier("group_memory_barrier")
237 barrier("memory_barrier_atomic_counter")
238 barrier("memory_barrier_buffer")
239 barrier("memory_barrier_image")
240 barrier("memory_barrier_shared")
241 barrier("begin_invocation_interlock")
242 barrier("end_invocation_interlock")
243
244 # A conditional discard/demote, with a single boolean source.
245 intrinsic("discard_if", src_comp=[1])
246 intrinsic("demote_if", src_comp=[1])
247
248 # ARB_shader_group_vote intrinsics
249 intrinsic("vote_any", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
250 intrinsic("vote_all", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
251 intrinsic("vote_feq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
252 intrinsic("vote_ieq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
253
254 # Ballot ALU operations from SPIR-V.
255 #
256 # These operations work like their ALU counterparts except that the operate
257 # on a uvec4 which is treated as a 128bit integer. Also, they are, in
258 # general, free to ignore any bits which are above the subgroup size.
259 intrinsic("ballot_bitfield_extract", src_comp=[4, 1], dest_comp=1, flags=[CAN_ELIMINATE])
260 intrinsic("ballot_bit_count_reduce", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
261 intrinsic("ballot_bit_count_inclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
262 intrinsic("ballot_bit_count_exclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
263 intrinsic("ballot_find_lsb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
264 intrinsic("ballot_find_msb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
265
266 # Shuffle operations from SPIR-V.
267 intrinsic("shuffle", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
268 intrinsic("shuffle_xor", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
269 intrinsic("shuffle_up", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
270 intrinsic("shuffle_down", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
271
272 # Quad operations from SPIR-V.
273 intrinsic("quad_broadcast", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
274 intrinsic("quad_swap_horizontal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
275 intrinsic("quad_swap_vertical", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
276 intrinsic("quad_swap_diagonal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
277
278 intrinsic("reduce", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP, CLUSTER_SIZE],
279 flags=[CAN_ELIMINATE])
280 intrinsic("inclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
281 flags=[CAN_ELIMINATE])
282 intrinsic("exclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
283 flags=[CAN_ELIMINATE])
284
285 # AMD shader ballot operations
286 intrinsic("quad_swizzle_amd", src_comp=[0], dest_comp=0, indices=[SWIZZLE_MASK],
287 flags=[CAN_ELIMINATE])
288 intrinsic("masked_swizzle_amd", src_comp=[0], dest_comp=0, indices=[SWIZZLE_MASK],
289 flags=[CAN_ELIMINATE])
290 intrinsic("write_invocation_amd", src_comp=[0, 0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
291 intrinsic("mbcnt_amd", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
292
293 # Basic Geometry Shader intrinsics.
294 #
295 # emit_vertex implements GLSL's EmitStreamVertex() built-in. It takes a single
296 # index, which is the stream ID to write to.
297 #
298 # end_primitive implements GLSL's EndPrimitive() built-in.
299 intrinsic("emit_vertex", indices=[STREAM_ID])
300 intrinsic("end_primitive", indices=[STREAM_ID])
301
302 # Geometry Shader intrinsics with a vertex count.
303 #
304 # Alternatively, drivers may implement these intrinsics, and use
305 # nir_lower_gs_intrinsics() to convert from the basic intrinsics.
306 #
307 # These maintain a count of the number of vertices emitted, as an additional
308 # unsigned integer source.
309 intrinsic("emit_vertex_with_counter", src_comp=[1], indices=[STREAM_ID])
310 intrinsic("end_primitive_with_counter", src_comp=[1], indices=[STREAM_ID])
311 intrinsic("set_vertex_count", src_comp=[1])
312
313 # Atomic counters
314 #
315 # The *_var variants take an atomic_uint nir_variable, while the other,
316 # lowered, variants take a constant buffer index and register offset.
317
318 def atomic(name, flags=[]):
319 intrinsic(name + "_deref", src_comp=[-1], dest_comp=1, flags=flags)
320 intrinsic(name, src_comp=[1], dest_comp=1, indices=[BASE], flags=flags)
321
322 def atomic2(name):
323 intrinsic(name + "_deref", src_comp=[-1, 1], dest_comp=1)
324 intrinsic(name, src_comp=[1, 1], dest_comp=1, indices=[BASE])
325
326 def atomic3(name):
327 intrinsic(name + "_deref", src_comp=[-1, 1, 1], dest_comp=1)
328 intrinsic(name, src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
329
330 atomic("atomic_counter_inc")
331 atomic("atomic_counter_pre_dec")
332 atomic("atomic_counter_post_dec")
333 atomic("atomic_counter_read", flags=[CAN_ELIMINATE])
334 atomic2("atomic_counter_add")
335 atomic2("atomic_counter_min")
336 atomic2("atomic_counter_max")
337 atomic2("atomic_counter_and")
338 atomic2("atomic_counter_or")
339 atomic2("atomic_counter_xor")
340 atomic2("atomic_counter_exchange")
341 atomic3("atomic_counter_comp_swap")
342
343 # Image load, store and atomic intrinsics.
344 #
345 # All image intrinsics come in three versions. One which take an image target
346 # passed as a deref chain as the first source, one which takes an index as the
347 # first source, and one which takes a bindless handle as the first source.
348 # In the first version, the image variable contains the memory and layout
349 # qualifiers that influence the semantics of the intrinsic. In the second and
350 # third, the image format and access qualifiers are provided as constant
351 # indices.
352 #
353 # All image intrinsics take a four-coordinate vector and a sample index as
354 # 2nd and 3rd sources, determining the location within the image that will be
355 # accessed by the intrinsic. Components not applicable to the image target
356 # in use are undefined. Image store takes an additional four-component
357 # argument with the value to be written, and image atomic operations take
358 # either one or two additional scalar arguments with the same meaning as in
359 # the ARB_shader_image_load_store specification.
360 def image(name, src_comp=[], **kwargs):
361 intrinsic("image_deref_" + name, src_comp=[1] + src_comp,
362 indices=[ACCESS], **kwargs)
363 intrinsic("image_" + name, src_comp=[1] + src_comp,
364 indices=[IMAGE_DIM, IMAGE_ARRAY, FORMAT, ACCESS], **kwargs)
365 intrinsic("bindless_image_" + name, src_comp=[1] + src_comp,
366 indices=[IMAGE_DIM, IMAGE_ARRAY, FORMAT, ACCESS], **kwargs)
367
368 image("load", src_comp=[4, 1], dest_comp=0, flags=[CAN_ELIMINATE])
369 image("store", src_comp=[4, 1, 0])
370 image("atomic_add", src_comp=[4, 1, 1], dest_comp=1)
371 image("atomic_imin", src_comp=[4, 1, 1], dest_comp=1)
372 image("atomic_umin", src_comp=[4, 1, 1], dest_comp=1)
373 image("atomic_imax", src_comp=[4, 1, 1], dest_comp=1)
374 image("atomic_umax", src_comp=[4, 1, 1], dest_comp=1)
375 image("atomic_and", src_comp=[4, 1, 1], dest_comp=1)
376 image("atomic_or", src_comp=[4, 1, 1], dest_comp=1)
377 image("atomic_xor", src_comp=[4, 1, 1], dest_comp=1)
378 image("atomic_exchange", src_comp=[4, 1, 1], dest_comp=1)
379 image("atomic_comp_swap", src_comp=[4, 1, 1, 1], dest_comp=1)
380 image("atomic_fadd", src_comp=[1, 4, 1, 1], dest_comp=1)
381 image("size", dest_comp=0, flags=[CAN_ELIMINATE, CAN_REORDER])
382 image("samples", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
383 image("atomic_inc_wrap", src_comp=[4, 1, 1], dest_comp=1)
384 image("atomic_dec_wrap", src_comp=[4, 1, 1], dest_comp=1)
385
386 # Intel-specific query for loading from the brw_image_param struct passed
387 # into the shader as a uniform. The variable is a deref to the image
388 # variable. The const index specifies which of the six parameters to load.
389 intrinsic("image_deref_load_param_intel", src_comp=[1], dest_comp=0,
390 indices=[BASE], flags=[CAN_ELIMINATE, CAN_REORDER])
391 image("load_raw_intel", src_comp=[1], dest_comp=0,
392 flags=[CAN_ELIMINATE])
393 image("store_raw_intel", src_comp=[1, 0])
394
395 # Vulkan descriptor set intrinsics
396 #
397 # The Vulkan API uses a different binding model from GL. In the Vulkan
398 # API, all external resources are represented by a tuple:
399 #
400 # (descriptor set, binding, array index)
401 #
402 # where the array index is the only thing allowed to be indirect. The
403 # vulkan_surface_index intrinsic takes the descriptor set and binding as
404 # its first two indices and the array index as its source. The third
405 # index is a nir_variable_mode in case that's useful to the backend.
406 #
407 # The intended usage is that the shader will call vulkan_surface_index to
408 # get an index and then pass that as the buffer index ubo/ssbo calls.
409 #
410 # The vulkan_resource_reindex intrinsic takes a resource index in src0
411 # (the result of a vulkan_resource_index or vulkan_resource_reindex) which
412 # corresponds to the tuple (set, binding, index) and computes an index
413 # corresponding to tuple (set, binding, idx + src1).
414 intrinsic("vulkan_resource_index", src_comp=[1], dest_comp=0,
415 indices=[DESC_SET, BINDING, DESC_TYPE],
416 flags=[CAN_ELIMINATE, CAN_REORDER])
417 intrinsic("vulkan_resource_reindex", src_comp=[0, 1], dest_comp=0,
418 indices=[DESC_TYPE], flags=[CAN_ELIMINATE, CAN_REORDER])
419 intrinsic("load_vulkan_descriptor", src_comp=[-1], dest_comp=0,
420 indices=[DESC_TYPE], flags=[CAN_ELIMINATE, CAN_REORDER])
421
422 # variable atomic intrinsics
423 #
424 # All of these variable atomic memory operations read a value from memory,
425 # compute a new value using one of the operations below, write the new value
426 # to memory, and return the original value read.
427 #
428 # All operations take 2 sources except CompSwap that takes 3. These sources
429 # represent:
430 #
431 # 0: A deref to the memory on which to perform the atomic
432 # 1: The data parameter to the atomic function (i.e. the value to add
433 # in shared_atomic_add, etc).
434 # 2: For CompSwap only: the second data parameter.
435 intrinsic("deref_atomic_add", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
436 intrinsic("deref_atomic_imin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
437 intrinsic("deref_atomic_umin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
438 intrinsic("deref_atomic_imax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
439 intrinsic("deref_atomic_umax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
440 intrinsic("deref_atomic_and", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
441 intrinsic("deref_atomic_or", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
442 intrinsic("deref_atomic_xor", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
443 intrinsic("deref_atomic_exchange", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
444 intrinsic("deref_atomic_comp_swap", src_comp=[-1, 1, 1], dest_comp=1, indices=[ACCESS])
445 intrinsic("deref_atomic_fadd", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
446 intrinsic("deref_atomic_fmin", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
447 intrinsic("deref_atomic_fmax", src_comp=[-1, 1], dest_comp=1, indices=[ACCESS])
448 intrinsic("deref_atomic_fcomp_swap", src_comp=[-1, 1, 1], dest_comp=1, indices=[ACCESS])
449
450 # SSBO atomic intrinsics
451 #
452 # All of the SSBO atomic memory operations read a value from memory,
453 # compute a new value using one of the operations below, write the new
454 # value to memory, and return the original value read.
455 #
456 # All operations take 3 sources except CompSwap that takes 4. These
457 # sources represent:
458 #
459 # 0: The SSBO buffer index.
460 # 1: The offset into the SSBO buffer of the variable that the atomic
461 # operation will operate on.
462 # 2: The data parameter to the atomic function (i.e. the value to add
463 # in ssbo_atomic_add, etc).
464 # 3: For CompSwap only: the second data parameter.
465 intrinsic("ssbo_atomic_add", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
466 intrinsic("ssbo_atomic_imin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
467 intrinsic("ssbo_atomic_umin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
468 intrinsic("ssbo_atomic_imax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
469 intrinsic("ssbo_atomic_umax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
470 intrinsic("ssbo_atomic_and", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
471 intrinsic("ssbo_atomic_or", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
472 intrinsic("ssbo_atomic_xor", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
473 intrinsic("ssbo_atomic_exchange", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
474 intrinsic("ssbo_atomic_comp_swap", src_comp=[1, 1, 1, 1], dest_comp=1, indices=[ACCESS])
475 intrinsic("ssbo_atomic_fadd", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
476 intrinsic("ssbo_atomic_fmin", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
477 intrinsic("ssbo_atomic_fmax", src_comp=[1, 1, 1], dest_comp=1, indices=[ACCESS])
478 intrinsic("ssbo_atomic_fcomp_swap", src_comp=[1, 1, 1, 1], dest_comp=1, indices=[ACCESS])
479
480 # CS shared variable atomic intrinsics
481 #
482 # All of the shared variable atomic memory operations read a value from
483 # memory, compute a new value using one of the operations below, write the
484 # new value to memory, and return the original value read.
485 #
486 # All operations take 2 sources except CompSwap that takes 3. These
487 # sources represent:
488 #
489 # 0: The offset into the shared variable storage region that the atomic
490 # operation will operate on.
491 # 1: The data parameter to the atomic function (i.e. the value to add
492 # in shared_atomic_add, etc).
493 # 2: For CompSwap only: the second data parameter.
494 intrinsic("shared_atomic_add", src_comp=[1, 1], dest_comp=1, indices=[BASE])
495 intrinsic("shared_atomic_imin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
496 intrinsic("shared_atomic_umin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
497 intrinsic("shared_atomic_imax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
498 intrinsic("shared_atomic_umax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
499 intrinsic("shared_atomic_and", src_comp=[1, 1], dest_comp=1, indices=[BASE])
500 intrinsic("shared_atomic_or", src_comp=[1, 1], dest_comp=1, indices=[BASE])
501 intrinsic("shared_atomic_xor", src_comp=[1, 1], dest_comp=1, indices=[BASE])
502 intrinsic("shared_atomic_exchange", src_comp=[1, 1], dest_comp=1, indices=[BASE])
503 intrinsic("shared_atomic_comp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
504 intrinsic("shared_atomic_fadd", src_comp=[1, 1], dest_comp=1, indices=[BASE])
505 intrinsic("shared_atomic_fmin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
506 intrinsic("shared_atomic_fmax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
507 intrinsic("shared_atomic_fcomp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
508
509 # Global atomic intrinsics
510 #
511 # All of the shared variable atomic memory operations read a value from
512 # memory, compute a new value using one of the operations below, write the
513 # new value to memory, and return the original value read.
514 #
515 # All operations take 2 sources except CompSwap that takes 3. These
516 # sources represent:
517 #
518 # 0: The memory address that the atomic operation will operate on.
519 # 1: The data parameter to the atomic function (i.e. the value to add
520 # in shared_atomic_add, etc).
521 # 2: For CompSwap only: the second data parameter.
522 intrinsic("global_atomic_add", src_comp=[1, 1], dest_comp=1, indices=[BASE])
523 intrinsic("global_atomic_imin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
524 intrinsic("global_atomic_umin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
525 intrinsic("global_atomic_imax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
526 intrinsic("global_atomic_umax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
527 intrinsic("global_atomic_and", src_comp=[1, 1], dest_comp=1, indices=[BASE])
528 intrinsic("global_atomic_or", src_comp=[1, 1], dest_comp=1, indices=[BASE])
529 intrinsic("global_atomic_xor", src_comp=[1, 1], dest_comp=1, indices=[BASE])
530 intrinsic("global_atomic_exchange", src_comp=[1, 1], dest_comp=1, indices=[BASE])
531 intrinsic("global_atomic_comp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
532 intrinsic("global_atomic_fadd", src_comp=[1, 1], dest_comp=1, indices=[BASE])
533 intrinsic("global_atomic_fmin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
534 intrinsic("global_atomic_fmax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
535 intrinsic("global_atomic_fcomp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
536
537 def system_value(name, dest_comp, indices=[], bit_sizes=[32]):
538 intrinsic("load_" + name, [], dest_comp, indices,
539 flags=[CAN_ELIMINATE, CAN_REORDER], sysval=True,
540 bit_sizes=bit_sizes)
541
542 system_value("frag_coord", 4)
543 system_value("point_coord", 2)
544 system_value("front_face", 1, bit_sizes=[1, 32])
545 system_value("vertex_id", 1)
546 system_value("vertex_id_zero_base", 1)
547 system_value("first_vertex", 1)
548 system_value("is_indexed_draw", 1)
549 system_value("base_vertex", 1)
550 system_value("instance_id", 1)
551 system_value("base_instance", 1)
552 system_value("draw_id", 1)
553 system_value("sample_id", 1)
554 # sample_id_no_per_sample is like sample_id but does not imply per-
555 # sample shading. See the lower_helper_invocation option.
556 system_value("sample_id_no_per_sample", 1)
557 system_value("sample_pos", 2)
558 system_value("sample_mask_in", 1)
559 system_value("primitive_id", 1)
560 system_value("invocation_id", 1)
561 system_value("tess_coord", 3)
562 system_value("tess_level_outer", 4)
563 system_value("tess_level_inner", 2)
564 system_value("tess_level_outer_default", 4)
565 system_value("tess_level_inner_default", 2)
566 system_value("patch_vertices_in", 1)
567 system_value("local_invocation_id", 3)
568 system_value("local_invocation_index", 1)
569 system_value("work_group_id", 3)
570 system_value("user_clip_plane", 4, indices=[UCP_ID])
571 system_value("num_work_groups", 3)
572 system_value("helper_invocation", 1, bit_sizes=[1, 32])
573 system_value("alpha_ref_float", 1)
574 system_value("layer_id", 1)
575 system_value("view_index", 1)
576 system_value("subgroup_size", 1)
577 system_value("subgroup_invocation", 1)
578 system_value("subgroup_eq_mask", 0, bit_sizes=[32, 64])
579 system_value("subgroup_ge_mask", 0, bit_sizes=[32, 64])
580 system_value("subgroup_gt_mask", 0, bit_sizes=[32, 64])
581 system_value("subgroup_le_mask", 0, bit_sizes=[32, 64])
582 system_value("subgroup_lt_mask", 0, bit_sizes=[32, 64])
583 system_value("num_subgroups", 1)
584 system_value("subgroup_id", 1)
585 system_value("local_group_size", 3)
586 system_value("global_invocation_id", 3, bit_sizes=[32, 64])
587 system_value("global_invocation_index", 1, bit_sizes=[32, 64])
588 system_value("work_dim", 1)
589 # Driver-specific viewport scale/offset parameters.
590 #
591 # VC4 and V3D need to emit a scaled version of the position in the vertex
592 # shaders for binning, and having system values lets us move the math for that
593 # into NIR.
594 #
595 # Panfrost needs to implement all coordinate transformation in the
596 # vertex shader; system values allow us to share this routine in NIR.
597 system_value("viewport_x_scale", 1)
598 system_value("viewport_y_scale", 1)
599 system_value("viewport_z_scale", 1)
600 system_value("viewport_z_offset", 1)
601 system_value("viewport_scale", 3)
602 system_value("viewport_offset", 3)
603
604 # Blend constant color values. Float values are clamped. Vectored versions are
605 # provided as well for driver convenience
606
607 system_value("blend_const_color_r_float", 1)
608 system_value("blend_const_color_g_float", 1)
609 system_value("blend_const_color_b_float", 1)
610 system_value("blend_const_color_a_float", 1)
611 system_value("blend_const_color_rgba", 4)
612 system_value("blend_const_color_rgba8888_unorm", 1)
613 system_value("blend_const_color_aaaa8888_unorm", 1)
614
615 # System values for gl_Color, for radeonsi which interpolates these in the
616 # shader prolog to handle two-sided color without recompiles and therefore
617 # doesn't handle these in the main shader part like normal varyings.
618 system_value("color0", 4)
619 system_value("color1", 4)
620
621 # System value for internal compute shaders in radeonsi.
622 system_value("user_data_amd", 4)
623
624 # Barycentric coordinate intrinsics.
625 #
626 # These set up the barycentric coordinates for a particular interpolation.
627 # The first three are for the simple cases: pixel, centroid, or per-sample
628 # (at gl_SampleID). The next two handle interpolating at a specified
629 # sample location, or interpolating with a vec2 offset,
630 #
631 # The interp_mode index should be either the INTERP_MODE_SMOOTH or
632 # INTERP_MODE_NOPERSPECTIVE enum values.
633 #
634 # The vec2 value produced by these intrinsics is intended for use as the
635 # barycoord source of a load_interpolated_input intrinsic.
636
637 def barycentric(name, src_comp=[]):
638 intrinsic("load_barycentric_" + name, src_comp=src_comp, dest_comp=2,
639 indices=[INTERP_MODE], flags=[CAN_ELIMINATE, CAN_REORDER])
640
641 # no sources.
642 barycentric("pixel")
643 barycentric("centroid")
644 barycentric("sample")
645 # src[] = { sample_id }.
646 barycentric("at_sample", [1])
647 # src[] = { offset.xy }.
648 barycentric("at_offset", [2])
649
650 # Load sample position:
651 #
652 # Takes a sample # and returns a sample position. Used for lowering
653 # interpolateAtSample() to interpolateAtOffset()
654 intrinsic("load_sample_pos_from_id", src_comp=[1], dest_comp=2,
655 flags=[CAN_ELIMINATE, CAN_REORDER])
656
657 # Loads what I believe is the primitive size, for scaling ij to pixel size:
658 intrinsic("load_size_ir3", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
659
660 # Fragment shader input interpolation delta intrinsic.
661 #
662 # For hw where fragment shader input interpolation is handled in shader, the
663 # load_fs_input_interp deltas intrinsics can be used to load the input deltas
664 # used for interpolation as follows:
665 #
666 # vec3 iid = load_fs_input_interp_deltas(varying_slot)
667 # vec2 bary = load_barycentric_*(...)
668 # float result = iid.x + iid.y * bary.y + iid.z * bary.x
669
670 intrinsic("load_fs_input_interp_deltas", src_comp=[1], dest_comp=3,
671 indices=[BASE, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER])
672
673 # Load operations pull data from some piece of GPU memory. All load
674 # operations operate in terms of offsets into some piece of theoretical
675 # memory. Loads from externally visible memory (UBO and SSBO) simply take a
676 # byte offset as a source. Loads from opaque memory (uniforms, inputs, etc.)
677 # take a base+offset pair where the nir_intrinsic_base() gives the location
678 # of the start of the variable being loaded and and the offset source is a
679 # offset into that variable.
680 #
681 # Uniform load operations have a nir_intrinsic_range() index that specifies the
682 # range (starting at base) of the data from which we are loading. If
683 # range == 0, then the range is unknown.
684 #
685 # Some load operations such as UBO/SSBO load and per_vertex loads take an
686 # additional source to specify which UBO/SSBO/vertex to load from.
687 #
688 # The exact address type depends on the lowering pass that generates the
689 # load/store intrinsics. Typically, this is vec4 units for things such as
690 # varying slots and float units for fragment shader inputs. UBO and SSBO
691 # offsets are always in bytes.
692
693 def load(name, num_srcs, indices=[], flags=[]):
694 intrinsic("load_" + name, [1] * num_srcs, dest_comp=0, indices=indices,
695 flags=flags)
696
697 # src[] = { offset }.
698 load("uniform", 1, [BASE, RANGE, TYPE], [CAN_ELIMINATE, CAN_REORDER])
699 # src[] = { buffer_index, offset }.
700 load("ubo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], flags=[CAN_ELIMINATE, CAN_REORDER])
701 # src[] = { offset }.
702 load("input", 1, [BASE, COMPONENT, TYPE], [CAN_ELIMINATE, CAN_REORDER])
703 # src[] = { vertex, offset }.
704 load("per_vertex_input", 2, [BASE, COMPONENT], [CAN_ELIMINATE, CAN_REORDER])
705 # src[] = { barycoord, offset }.
706 intrinsic("load_interpolated_input", src_comp=[2, 1], dest_comp=0,
707 indices=[BASE, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER])
708
709 # src[] = { buffer_index, offset }.
710 load("ssbo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
711 # src[] = { offset }.
712 load("output", 1, [BASE, COMPONENT], flags=[CAN_ELIMINATE])
713 # src[] = { vertex, offset }.
714 load("per_vertex_output", 2, [BASE, COMPONENT], [CAN_ELIMINATE])
715 # src[] = { offset }.
716 load("shared", 1, [BASE, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
717 # src[] = { offset }.
718 load("push_constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
719 # src[] = { offset }.
720 load("constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
721 # src[] = { address }.
722 load("global", 1, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
723 # src[] = { address }.
724 load("kernel_input", 1, [BASE, RANGE, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE, CAN_REORDER])
725 # src[] = { offset }.
726 load("scratch", 1, [ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
727
728 # Stores work the same way as loads, except now the first source is the value
729 # to store and the second (and possibly third) source specify where to store
730 # the value. SSBO and shared memory stores also have a
731 # nir_intrinsic_write_mask()
732
733 def store(name, num_srcs, indices=[], flags=[]):
734 intrinsic("store_" + name, [0] + ([1] * (num_srcs - 1)), indices=indices, flags=flags)
735
736 # src[] = { value, offset }.
737 store("output", 2, [BASE, WRMASK, COMPONENT, TYPE])
738 # src[] = { value, vertex, offset }.
739 store("per_vertex_output", 3, [BASE, WRMASK, COMPONENT])
740 # src[] = { value, block_index, offset }
741 store("ssbo", 3, [WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
742 # src[] = { value, offset }.
743 store("shared", 2, [BASE, WRMASK, ALIGN_MUL, ALIGN_OFFSET])
744 # src[] = { value, address }.
745 store("global", 2, [WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
746 # src[] = { value, offset }.
747 store("scratch", 2, [ALIGN_MUL, ALIGN_OFFSET, WRMASK])
748
749 # IR3-specific version of most SSBO intrinsics. The only different
750 # compare to the originals is that they add an extra source to hold
751 # the dword-offset, which is needed by the backend code apart from
752 # the byte-offset already provided by NIR in one of the sources.
753 #
754 # NIR lowering pass 'ir3_nir_lower_io_offset' will replace the
755 # original SSBO intrinsics by these, placing the computed
756 # dword-offset always in the last source.
757 #
758 # The float versions are not handled because those are not supported
759 # by the backend.
760 intrinsic("store_ssbo_ir3", src_comp=[0, 1, 1, 1],
761 indices=[WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
762 intrinsic("load_ssbo_ir3", src_comp=[1, 1, 1], dest_comp=0,
763 indices=[ACCESS, ALIGN_MUL, ALIGN_OFFSET], flags=[CAN_ELIMINATE])
764 intrinsic("ssbo_atomic_add_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
765 intrinsic("ssbo_atomic_imin_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
766 intrinsic("ssbo_atomic_umin_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
767 intrinsic("ssbo_atomic_imax_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
768 intrinsic("ssbo_atomic_umax_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
769 intrinsic("ssbo_atomic_and_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
770 intrinsic("ssbo_atomic_or_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
771 intrinsic("ssbo_atomic_xor_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
772 intrinsic("ssbo_atomic_exchange_ir3", src_comp=[1, 1, 1, 1], dest_comp=1)
773 intrinsic("ssbo_atomic_comp_swap_ir3", src_comp=[1, 1, 1, 1, 1], dest_comp=1)
774
775 # Intrinsics used by the Midgard/Bifrost blend pipeline. These are defined
776 # within a blend shader to read/write the raw value from the tile buffer,
777 # without applying any format conversion in the process. If the shader needs
778 # usable pixel values, it must apply format conversions itself.
779 #
780 # These definitions are generic, but they are explicitly vendored to prevent
781 # other drivers from using them, as their semantics is defined in terms of the
782 # Midgard/Bifrost hardware tile buffer and may not line up with anything sane.
783 # One notable divergence is sRGB, which is asymmetric: raw_input_pan requires
784 # an sRGB->linear conversion, but linear values should be written to
785 # raw_output_pan and the hardware handles linear->sRGB.
786
787 # src[] = { value }
788 store("raw_output_pan", 1, [])
789 load("raw_output_pan", 0, [], [CAN_ELIMINATE, CAN_REORDER])
790
791 # V3D-specific instrinc for tile buffer color reads.
792 #
793 # The hardware requires that we read the samples and components of a pixel
794 # in order, so we cannot eliminate or remove any loads in a sequence.
795 #
796 # src[] = { render_target }
797 # BASE = sample index
798 load("tlb_color_v3d", 1, [BASE, COMPONENT], [])
799
800 # V3D-specific instrinc for per-sample tile buffer color writes.
801 #
802 # The driver backend needs to identify per-sample color writes and emit
803 # specific code for them.
804 #
805 # src[] = { value, render_target }
806 # BASE = sample index
807 store("tlb_sample_color_v3d", 2, [BASE, COMPONENT, TYPE], [])