nir/intrinsics: Allow deref sources to consume anything
[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):
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 """
49 assert isinstance(name, str)
50 assert isinstance(src_components, list)
51 if src_components:
52 assert isinstance(src_components[0], int)
53 assert isinstance(dest_components, int)
54 assert isinstance(indices, list)
55 if indices:
56 assert isinstance(indices[0], str)
57 assert isinstance(flags, list)
58 if flags:
59 assert isinstance(flags[0], str)
60 assert isinstance(sysval, bool)
61
62 self.name = name
63 self.num_srcs = len(src_components)
64 self.src_components = src_components
65 self.has_dest = (dest_components >= 0)
66 self.dest_components = dest_components
67 self.num_indices = len(indices)
68 self.indices = indices
69 self.flags = flags
70 self.sysval = sysval
71
72 #
73 # Possible indices:
74 #
75
76 # A constant 'base' value that is added to an offset src:
77 BASE = "NIR_INTRINSIC_BASE"
78 # For store instructions, a writemask:
79 WRMASK = "NIR_INTRINSIC_WRMASK"
80 # The stream-id for GS emit_vertex/end_primitive intrinsics:
81 STREAM_ID = "NIR_INTRINSIC_STREAM_ID"
82 # The clip-plane id for load_user_clip_plane intrinsics:
83 UCP_ID = "NIR_INTRINSIC_UCP_ID"
84 # The amount of data, starting from BASE, that this instruction
85 # may access. This is used to provide bounds if the offset is
86 # not constant.
87 RANGE = "NIR_INTRINSIC_RANGE"
88 # The vulkan descriptor set binding for vulkan_resource_index
89 # intrinsic
90 DESC_SET = "NIR_INTRINSIC_DESC_SET"
91 # The vulkan descriptor set binding for vulkan_resource_index
92 # intrinsic
93 BINDING = "NIR_INTRINSIC_BINDING"
94 # Component offset
95 COMPONENT = "NIR_INTRINSIC_COMPONENT"
96 # Interpolation mode (only meaningful for FS inputs)
97 INTERP_MODE = "NIR_INTRINSIC_INTERP_MODE"
98 # A binary nir_op to use when performing a reduction or scan operation
99 REDUCTION_OP = "NIR_INTRINSIC_REDUCTION_OP"
100 # Cluster size for reduction operations
101 CLUSTER_SIZE = "NIR_INTRINSIC_CLUSTER_SIZE"
102 # Parameter index for a load_param intrinsic
103 PARAM_IDX = "NIR_INTRINSIC_PARAM_IDX"
104 # Image dimensionality for image intrinsics
105 IMAGE_DIM = "NIR_INTRINSIC_IMAGE_DIM"
106 # Non-zero if we are accessing an array image
107 IMAGE_ARRAY = "NIR_INTRINSIC_IMAGE_ARRAY"
108 # Access qualifiers for image intrinsics
109 ACCESS = "NIR_INTRINSIC_ACCESS"
110 # Image format for image intrinsics
111 FORMAT = "NIR_INTRINSIC_FORMAT"
112 # Offset or address alignment
113 ALIGN_MUL = "NIR_INTRINSIC_ALIGN_MUL"
114 ALIGN_OFFSET = "NIR_INTRINSIC_ALIGN_OFFSET"
115
116 #
117 # Possible flags:
118 #
119
120 CAN_ELIMINATE = "NIR_INTRINSIC_CAN_ELIMINATE"
121 CAN_REORDER = "NIR_INTRINSIC_CAN_REORDER"
122
123 INTR_OPCODES = {}
124
125 def intrinsic(name, src_comp=[], dest_comp=-1, indices=[],
126 flags=[], sysval=False):
127 assert name not in INTR_OPCODES
128 INTR_OPCODES[name] = Intrinsic(name, src_comp, dest_comp,
129 indices, flags, sysval)
130
131 intrinsic("nop", flags=[CAN_ELIMINATE])
132
133 intrinsic("load_param", dest_comp=0, indices=[PARAM_IDX], flags=[CAN_ELIMINATE])
134
135 intrinsic("load_deref", dest_comp=0, src_comp=[-1], flags=[CAN_ELIMINATE])
136 intrinsic("store_deref", src_comp=[-1, 0], indices=[WRMASK])
137 intrinsic("copy_deref", src_comp=[-1, -1])
138
139 # Interpolation of input. The interp_deref_at* intrinsics are similar to the
140 # load_var intrinsic acting on a shader input except that they interpolate the
141 # input differently. The at_sample and at_offset intrinsics take an
142 # additional source that is an integer sample id or a vec2 position offset
143 # respectively.
144
145 intrinsic("interp_deref_at_centroid", dest_comp=0, src_comp=[1],
146 flags=[ CAN_ELIMINATE, CAN_REORDER])
147 intrinsic("interp_deref_at_sample", src_comp=[1, 1], dest_comp=0,
148 flags=[CAN_ELIMINATE, CAN_REORDER])
149 intrinsic("interp_deref_at_offset", src_comp=[1, 2], dest_comp=0,
150 flags=[CAN_ELIMINATE, CAN_REORDER])
151
152 # Ask the driver for the size of a given buffer. It takes the buffer index
153 # as source.
154 intrinsic("get_buffer_size", src_comp=[1], dest_comp=1,
155 flags=[CAN_ELIMINATE, CAN_REORDER])
156
157 # a barrier is an intrinsic with no inputs/outputs but which can't be moved
158 # around/optimized in general
159 def barrier(name):
160 intrinsic(name)
161
162 barrier("barrier")
163 barrier("discard")
164
165 # Memory barrier with semantics analogous to the memoryBarrier() GLSL
166 # intrinsic.
167 barrier("memory_barrier")
168
169 # Shader clock intrinsic with semantics analogous to the clock2x32ARB()
170 # GLSL intrinsic.
171 # The latter can be used as code motion barrier, which is currently not
172 # feasible with NIR.
173 intrinsic("shader_clock", dest_comp=2, flags=[CAN_ELIMINATE])
174
175 # Shader ballot intrinsics with semantics analogous to the
176 #
177 # ballotARB()
178 # readInvocationARB()
179 # readFirstInvocationARB()
180 #
181 # GLSL functions from ARB_shader_ballot.
182 intrinsic("ballot", src_comp=[1], dest_comp=0, flags=[CAN_ELIMINATE])
183 intrinsic("read_invocation", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
184 intrinsic("read_first_invocation", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
185
186 # Additional SPIR-V ballot intrinsics
187 #
188 # These correspond to the SPIR-V opcodes
189 #
190 # OpGroupUniformElect
191 # OpSubgroupFirstInvocationKHR
192 intrinsic("elect", dest_comp=1, flags=[CAN_ELIMINATE])
193 intrinsic("first_invocation", dest_comp=1, flags=[CAN_ELIMINATE])
194
195 # Memory barrier with semantics analogous to the compute shader
196 # groupMemoryBarrier(), memoryBarrierAtomicCounter(), memoryBarrierBuffer(),
197 # memoryBarrierImage() and memoryBarrierShared() GLSL intrinsics.
198 barrier("group_memory_barrier")
199 barrier("memory_barrier_atomic_counter")
200 barrier("memory_barrier_buffer")
201 barrier("memory_barrier_image")
202 barrier("memory_barrier_shared")
203 barrier("begin_invocation_interlock")
204 barrier("end_invocation_interlock")
205
206 # A conditional discard, with a single boolean source.
207 intrinsic("discard_if", src_comp=[1])
208
209 # ARB_shader_group_vote intrinsics
210 intrinsic("vote_any", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
211 intrinsic("vote_all", src_comp=[1], dest_comp=1, flags=[CAN_ELIMINATE])
212 intrinsic("vote_feq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
213 intrinsic("vote_ieq", src_comp=[0], dest_comp=1, flags=[CAN_ELIMINATE])
214
215 # Ballot ALU operations from SPIR-V.
216 #
217 # These operations work like their ALU counterparts except that the operate
218 # on a uvec4 which is treated as a 128bit integer. Also, they are, in
219 # general, free to ignore any bits which are above the subgroup size.
220 intrinsic("ballot_bitfield_extract", src_comp=[4, 1], dest_comp=1, flags=[CAN_ELIMINATE])
221 intrinsic("ballot_bit_count_reduce", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
222 intrinsic("ballot_bit_count_inclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
223 intrinsic("ballot_bit_count_exclusive", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
224 intrinsic("ballot_find_lsb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
225 intrinsic("ballot_find_msb", src_comp=[4], dest_comp=1, flags=[CAN_ELIMINATE])
226
227 # Shuffle operations from SPIR-V.
228 intrinsic("shuffle", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
229 intrinsic("shuffle_xor", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
230 intrinsic("shuffle_up", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
231 intrinsic("shuffle_down", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
232
233 # Quad operations from SPIR-V.
234 intrinsic("quad_broadcast", src_comp=[0, 1], dest_comp=0, flags=[CAN_ELIMINATE])
235 intrinsic("quad_swap_horizontal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
236 intrinsic("quad_swap_vertical", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
237 intrinsic("quad_swap_diagonal", src_comp=[0], dest_comp=0, flags=[CAN_ELIMINATE])
238
239 intrinsic("reduce", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP, CLUSTER_SIZE],
240 flags=[CAN_ELIMINATE])
241 intrinsic("inclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
242 flags=[CAN_ELIMINATE])
243 intrinsic("exclusive_scan", src_comp=[0], dest_comp=0, indices=[REDUCTION_OP],
244 flags=[CAN_ELIMINATE])
245
246 # Basic Geometry Shader intrinsics.
247 #
248 # emit_vertex implements GLSL's EmitStreamVertex() built-in. It takes a single
249 # index, which is the stream ID to write to.
250 #
251 # end_primitive implements GLSL's EndPrimitive() built-in.
252 intrinsic("emit_vertex", indices=[STREAM_ID])
253 intrinsic("end_primitive", indices=[STREAM_ID])
254
255 # Geometry Shader intrinsics with a vertex count.
256 #
257 # Alternatively, drivers may implement these intrinsics, and use
258 # nir_lower_gs_intrinsics() to convert from the basic intrinsics.
259 #
260 # These maintain a count of the number of vertices emitted, as an additional
261 # unsigned integer source.
262 intrinsic("emit_vertex_with_counter", src_comp=[1], indices=[STREAM_ID])
263 intrinsic("end_primitive_with_counter", src_comp=[1], indices=[STREAM_ID])
264 intrinsic("set_vertex_count", src_comp=[1])
265
266 # Atomic counters
267 #
268 # The *_var variants take an atomic_uint nir_variable, while the other,
269 # lowered, variants take a constant buffer index and register offset.
270
271 def atomic(name, flags=[]):
272 intrinsic(name + "_deref", src_comp=[-1], dest_comp=1, flags=flags)
273 intrinsic(name, src_comp=[1], dest_comp=1, indices=[BASE], flags=flags)
274
275 def atomic2(name):
276 intrinsic(name + "_deref", src_comp=[-1, 1], dest_comp=1)
277 intrinsic(name, src_comp=[1, 1], dest_comp=1, indices=[BASE])
278
279 def atomic3(name):
280 intrinsic(name + "_deref", src_comp=[-1, 1, 1], dest_comp=1)
281 intrinsic(name, src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
282
283 atomic("atomic_counter_inc")
284 atomic("atomic_counter_pre_dec")
285 atomic("atomic_counter_post_dec")
286 atomic("atomic_counter_read", flags=[CAN_ELIMINATE])
287 atomic2("atomic_counter_add")
288 atomic2("atomic_counter_min")
289 atomic2("atomic_counter_max")
290 atomic2("atomic_counter_and")
291 atomic2("atomic_counter_or")
292 atomic2("atomic_counter_xor")
293 atomic2("atomic_counter_exchange")
294 atomic3("atomic_counter_comp_swap")
295
296 # Image load, store and atomic intrinsics.
297 #
298 # All image intrinsics come in two versions. One which take an image target
299 # passed as a deref chain as the first source and one which takes an index or
300 # handle as the first source. In the first version, the image variable
301 # contains the memory and layout qualifiers that influence the semantics of
302 # the intrinsic. In the second, the image format and access qualifiers are
303 # provided as constant indices.
304 #
305 # All image intrinsics take a four-coordinate vector and a sample index as
306 # 2nd and 3rd sources, determining the location within the image that will be
307 # accessed by the intrinsic. Components not applicable to the image target
308 # in use are undefined. Image store takes an additional four-component
309 # argument with the value to be written, and image atomic operations take
310 # either one or two additional scalar arguments with the same meaning as in
311 # the ARB_shader_image_load_store specification.
312 def image(name, src_comp=[], **kwargs):
313 intrinsic("image_deref_" + name, src_comp=[1] + src_comp, **kwargs)
314 intrinsic("image_" + name, src_comp=[1] + src_comp,
315 indices=[IMAGE_DIM, IMAGE_ARRAY, FORMAT, ACCESS], **kwargs)
316
317 image("load", src_comp=[4, 1], dest_comp=0, flags=[CAN_ELIMINATE])
318 image("store", src_comp=[4, 1, 0])
319 image("atomic_add", src_comp=[4, 1, 1], dest_comp=1)
320 image("atomic_min", src_comp=[4, 1, 1], dest_comp=1)
321 image("atomic_max", src_comp=[4, 1, 1], dest_comp=1)
322 image("atomic_and", src_comp=[4, 1, 1], dest_comp=1)
323 image("atomic_or", src_comp=[4, 1, 1], dest_comp=1)
324 image("atomic_xor", src_comp=[4, 1, 1], dest_comp=1)
325 image("atomic_exchange", src_comp=[4, 1, 1], dest_comp=1)
326 image("atomic_comp_swap", src_comp=[4, 1, 1, 1], dest_comp=1)
327 image("atomic_fadd", src_comp=[1, 4, 1, 1], dest_comp=1)
328 image("size", dest_comp=0, flags=[CAN_ELIMINATE, CAN_REORDER])
329 image("samples", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
330
331 # Intel-specific query for loading from the brw_image_param struct passed
332 # into the shader as a uniform. The variable is a deref to the image
333 # variable. The const index specifies which of the six parameters to load.
334 intrinsic("image_deref_load_param_intel", src_comp=[1], dest_comp=0,
335 indices=[BASE], flags=[CAN_ELIMINATE, CAN_REORDER])
336 image("load_raw_intel", src_comp=[1], dest_comp=0,
337 flags=[CAN_ELIMINATE])
338 image("store_raw_intel", src_comp=[1, 0])
339
340 # Vulkan descriptor set intrinsics
341 #
342 # The Vulkan API uses a different binding model from GL. In the Vulkan
343 # API, all external resources are represented by a tuple:
344 #
345 # (descriptor set, binding, array index)
346 #
347 # where the array index is the only thing allowed to be indirect. The
348 # vulkan_surface_index intrinsic takes the descriptor set and binding as
349 # its first two indices and the array index as its source. The third
350 # index is a nir_variable_mode in case that's useful to the backend.
351 #
352 # The intended usage is that the shader will call vulkan_surface_index to
353 # get an index and then pass that as the buffer index ubo/ssbo calls.
354 #
355 # The vulkan_resource_reindex intrinsic takes a resource index in src0
356 # (the result of a vulkan_resource_index or vulkan_resource_reindex) which
357 # corresponds to the tuple (set, binding, index) and computes an index
358 # corresponding to tuple (set, binding, idx + src1).
359 intrinsic("vulkan_resource_index", src_comp=[1], dest_comp=1,
360 indices=[DESC_SET, BINDING], flags=[CAN_ELIMINATE, CAN_REORDER])
361 intrinsic("vulkan_resource_reindex", src_comp=[1, 1], dest_comp=1,
362 flags=[CAN_ELIMINATE, CAN_REORDER])
363
364 # variable atomic intrinsics
365 #
366 # All of these variable atomic memory operations read a value from memory,
367 # compute a new value using one of the operations below, write the new value
368 # to memory, and return the original value read.
369 #
370 # All operations take 2 sources except CompSwap that takes 3. These sources
371 # represent:
372 #
373 # 0: A deref to the memory on which to perform the atomic
374 # 1: The data parameter to the atomic function (i.e. the value to add
375 # in shared_atomic_add, etc).
376 # 2: For CompSwap only: the second data parameter.
377 intrinsic("deref_atomic_add", src_comp=[-1, 1], dest_comp=1)
378 intrinsic("deref_atomic_imin", src_comp=[-1, 1], dest_comp=1)
379 intrinsic("deref_atomic_umin", src_comp=[-1, 1], dest_comp=1)
380 intrinsic("deref_atomic_imax", src_comp=[-1, 1], dest_comp=1)
381 intrinsic("deref_atomic_umax", src_comp=[-1, 1], dest_comp=1)
382 intrinsic("deref_atomic_and", src_comp=[-1, 1], dest_comp=1)
383 intrinsic("deref_atomic_or", src_comp=[-1, 1], dest_comp=1)
384 intrinsic("deref_atomic_xor", src_comp=[-1, 1], dest_comp=1)
385 intrinsic("deref_atomic_exchange", src_comp=[-1, 1], dest_comp=1)
386 intrinsic("deref_atomic_comp_swap", src_comp=[-1, 1, 1], dest_comp=1)
387 intrinsic("deref_atomic_fadd", src_comp=[-1, 1], dest_comp=1)
388 intrinsic("deref_atomic_fmin", src_comp=[-1, 1], dest_comp=1)
389 intrinsic("deref_atomic_fmax", src_comp=[-1, 1], dest_comp=1)
390 intrinsic("deref_atomic_fcomp_swap", src_comp=[-1, 1, 1], dest_comp=1)
391
392 # SSBO atomic intrinsics
393 #
394 # All of the SSBO atomic memory operations read a value from memory,
395 # compute a new value using one of the operations below, write the new
396 # value to memory, and return the original value read.
397 #
398 # All operations take 3 sources except CompSwap that takes 4. These
399 # sources represent:
400 #
401 # 0: The SSBO buffer index.
402 # 1: The offset into the SSBO buffer of the variable that the atomic
403 # operation will operate on.
404 # 2: The data parameter to the atomic function (i.e. the value to add
405 # in ssbo_atomic_add, etc).
406 # 3: For CompSwap only: the second data parameter.
407 intrinsic("ssbo_atomic_add", src_comp=[1, 1, 1], dest_comp=1)
408 intrinsic("ssbo_atomic_imin", src_comp=[1, 1, 1], dest_comp=1)
409 intrinsic("ssbo_atomic_umin", src_comp=[1, 1, 1], dest_comp=1)
410 intrinsic("ssbo_atomic_imax", src_comp=[1, 1, 1], dest_comp=1)
411 intrinsic("ssbo_atomic_umax", src_comp=[1, 1, 1], dest_comp=1)
412 intrinsic("ssbo_atomic_and", src_comp=[1, 1, 1], dest_comp=1)
413 intrinsic("ssbo_atomic_or", src_comp=[1, 1, 1], dest_comp=1)
414 intrinsic("ssbo_atomic_xor", src_comp=[1, 1, 1], dest_comp=1)
415 intrinsic("ssbo_atomic_exchange", src_comp=[1, 1, 1], dest_comp=1)
416 intrinsic("ssbo_atomic_comp_swap", src_comp=[1, 1, 1, 1], dest_comp=1)
417 intrinsic("ssbo_atomic_fadd", src_comp=[1, 1, 1], dest_comp=1)
418 intrinsic("ssbo_atomic_fmin", src_comp=[1, 1, 1], dest_comp=1)
419 intrinsic("ssbo_atomic_fmax", src_comp=[1, 1, 1], dest_comp=1)
420 intrinsic("ssbo_atomic_fcomp_swap", src_comp=[1, 1, 1, 1], dest_comp=1)
421
422 # CS shared variable atomic intrinsics
423 #
424 # All of the shared variable atomic memory operations read a value from
425 # memory, compute a new value using one of the operations below, write the
426 # new value to memory, and return the original value read.
427 #
428 # All operations take 2 sources except CompSwap that takes 3. These
429 # sources represent:
430 #
431 # 0: The offset into the shared variable storage region that the atomic
432 # operation will operate on.
433 # 1: The data parameter to the atomic function (i.e. the value to add
434 # in shared_atomic_add, etc).
435 # 2: For CompSwap only: the second data parameter.
436 intrinsic("shared_atomic_add", src_comp=[1, 1], dest_comp=1, indices=[BASE])
437 intrinsic("shared_atomic_imin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
438 intrinsic("shared_atomic_umin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
439 intrinsic("shared_atomic_imax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
440 intrinsic("shared_atomic_umax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
441 intrinsic("shared_atomic_and", src_comp=[1, 1], dest_comp=1, indices=[BASE])
442 intrinsic("shared_atomic_or", src_comp=[1, 1], dest_comp=1, indices=[BASE])
443 intrinsic("shared_atomic_xor", src_comp=[1, 1], dest_comp=1, indices=[BASE])
444 intrinsic("shared_atomic_exchange", src_comp=[1, 1], dest_comp=1, indices=[BASE])
445 intrinsic("shared_atomic_comp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
446 intrinsic("shared_atomic_fadd", src_comp=[1, 1], dest_comp=1, indices=[BASE])
447 intrinsic("shared_atomic_fmin", src_comp=[1, 1], dest_comp=1, indices=[BASE])
448 intrinsic("shared_atomic_fmax", src_comp=[1, 1], dest_comp=1, indices=[BASE])
449 intrinsic("shared_atomic_fcomp_swap", src_comp=[1, 1, 1], dest_comp=1, indices=[BASE])
450
451 def system_value(name, dest_comp, indices=[]):
452 intrinsic("load_" + name, [], dest_comp, indices,
453 flags=[CAN_ELIMINATE, CAN_REORDER], sysval=True)
454
455 system_value("frag_coord", 4)
456 system_value("front_face", 1)
457 system_value("vertex_id", 1)
458 system_value("vertex_id_zero_base", 1)
459 system_value("first_vertex", 1)
460 system_value("is_indexed_draw", 1)
461 system_value("base_vertex", 1)
462 system_value("instance_id", 1)
463 system_value("base_instance", 1)
464 system_value("draw_id", 1)
465 system_value("sample_id", 1)
466 # sample_id_no_per_sample is like sample_id but does not imply per-
467 # sample shading. See the lower_helper_invocation option.
468 system_value("sample_id_no_per_sample", 1)
469 system_value("sample_pos", 2)
470 system_value("sample_mask_in", 1)
471 system_value("primitive_id", 1)
472 system_value("invocation_id", 1)
473 system_value("tess_coord", 3)
474 system_value("tess_level_outer", 4)
475 system_value("tess_level_inner", 2)
476 system_value("patch_vertices_in", 1)
477 system_value("local_invocation_id", 3)
478 system_value("local_invocation_index", 1)
479 system_value("work_group_id", 3)
480 system_value("user_clip_plane", 4, indices=[UCP_ID])
481 system_value("num_work_groups", 3)
482 system_value("helper_invocation", 1)
483 system_value("alpha_ref_float", 1)
484 system_value("layer_id", 1)
485 system_value("view_index", 1)
486 system_value("subgroup_size", 1)
487 system_value("subgroup_invocation", 1)
488 system_value("subgroup_eq_mask", 0)
489 system_value("subgroup_ge_mask", 0)
490 system_value("subgroup_gt_mask", 0)
491 system_value("subgroup_le_mask", 0)
492 system_value("subgroup_lt_mask", 0)
493 system_value("num_subgroups", 1)
494 system_value("subgroup_id", 1)
495 system_value("local_group_size", 3)
496 system_value("global_invocation_id", 3)
497 system_value("work_dim", 1)
498
499 # Blend constant color values. Float values are clamped.#
500 system_value("blend_const_color_r_float", 1)
501 system_value("blend_const_color_g_float", 1)
502 system_value("blend_const_color_b_float", 1)
503 system_value("blend_const_color_a_float", 1)
504 system_value("blend_const_color_rgba8888_unorm", 1)
505 system_value("blend_const_color_aaaa8888_unorm", 1)
506
507 # Barycentric coordinate intrinsics.
508 #
509 # These set up the barycentric coordinates for a particular interpolation.
510 # The first three are for the simple cases: pixel, centroid, or per-sample
511 # (at gl_SampleID). The next two handle interpolating at a specified
512 # sample location, or interpolating with a vec2 offset,
513 #
514 # The interp_mode index should be either the INTERP_MODE_SMOOTH or
515 # INTERP_MODE_NOPERSPECTIVE enum values.
516 #
517 # The vec2 value produced by these intrinsics is intended for use as the
518 # barycoord source of a load_interpolated_input intrinsic.
519
520 def barycentric(name, src_comp=[]):
521 intrinsic("load_barycentric_" + name, src_comp=src_comp, dest_comp=2,
522 indices=[INTERP_MODE], flags=[CAN_ELIMINATE, CAN_REORDER])
523
524 # no sources. const_index[] = { interp_mode }
525 barycentric("pixel")
526 barycentric("centroid")
527 barycentric("sample")
528 # src[] = { sample_id }. const_index[] = { interp_mode }
529 barycentric("at_sample", [1])
530 # src[] = { offset.xy }. const_index[] = { interp_mode }
531 barycentric("at_offset", [2])
532
533 # Load operations pull data from some piece of GPU memory. All load
534 # operations operate in terms of offsets into some piece of theoretical
535 # memory. Loads from externally visible memory (UBO and SSBO) simply take a
536 # byte offset as a source. Loads from opaque memory (uniforms, inputs, etc.)
537 # take a base+offset pair where the base (const_index[0]) gives the location
538 # of the start of the variable being loaded and and the offset source is a
539 # offset into that variable.
540 #
541 # Uniform load operations have a second "range" index that specifies the
542 # range (starting at base) of the data from which we are loading. If
543 # const_index[1] == 0, then the range is unknown.
544 #
545 # Some load operations such as UBO/SSBO load and per_vertex loads take an
546 # additional source to specify which UBO/SSBO/vertex to load from.
547 #
548 # The exact address type depends on the lowering pass that generates the
549 # load/store intrinsics. Typically, this is vec4 units for things such as
550 # varying slots and float units for fragment shader inputs. UBO and SSBO
551 # offsets are always in bytes.
552
553 def load(name, num_srcs, indices=[], flags=[]):
554 intrinsic("load_" + name, [1] * num_srcs, dest_comp=0, indices=indices,
555 flags=flags)
556
557 # src[] = { offset }. const_index[] = { base, range }
558 load("uniform", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
559 # src[] = { buffer_index, offset }. const_index[] = { align_mul, align_offset }
560 load("ubo", 2, [ALIGN_MUL, ALIGN_OFFSET], flags=[CAN_ELIMINATE, CAN_REORDER])
561 # src[] = { offset }. const_index[] = { base, component }
562 load("input", 1, [BASE, COMPONENT], [CAN_ELIMINATE, CAN_REORDER])
563 # src[] = { vertex, offset }. const_index[] = { base, component }
564 load("per_vertex_input", 2, [BASE, COMPONENT], [CAN_ELIMINATE, CAN_REORDER])
565 # src[] = { barycoord, offset }. const_index[] = { base, component }
566 intrinsic("load_interpolated_input", src_comp=[2, 1], dest_comp=0,
567 indices=[BASE, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER])
568
569 # src[] = { buffer_index, offset }.
570 # const_index[] = { access, align_mul, align_offset }
571 load("ssbo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
572 # src[] = { offset }. const_index[] = { base, component }
573 load("output", 1, [BASE, COMPONENT], flags=[CAN_ELIMINATE])
574 # src[] = { vertex, offset }. const_index[] = { base }
575 load("per_vertex_output", 2, [BASE, COMPONENT], [CAN_ELIMINATE])
576 # src[] = { offset }. const_index[] = { base, align_mul, align_offset }
577 load("shared", 1, [BASE, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE])
578 # src[] = { offset }. const_index[] = { base, range }
579 load("push_constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
580 # src[] = { offset }. const_index[] = { base, range }
581 load("constant", 1, [BASE, RANGE], [CAN_ELIMINATE, CAN_REORDER])
582
583 # Stores work the same way as loads, except now the first source is the value
584 # to store and the second (and possibly third) source specify where to store
585 # the value. SSBO and shared memory stores also have a write mask as
586 # const_index[0].
587
588 def store(name, num_srcs, indices=[], flags=[]):
589 intrinsic("store_" + name, [0] + ([1] * (num_srcs - 1)), indices=indices, flags=flags)
590
591 # src[] = { value, offset }. const_index[] = { base, write_mask, component }
592 store("output", 2, [BASE, WRMASK, COMPONENT])
593 # src[] = { value, vertex, offset }.
594 # const_index[] = { base, write_mask, component }
595 store("per_vertex_output", 3, [BASE, WRMASK, COMPONENT])
596 # src[] = { value, block_index, offset }
597 # const_index[] = { write_mask, access, align_mul, align_offset }
598 store("ssbo", 3, [WRMASK, ACCESS, ALIGN_MUL, ALIGN_OFFSET])
599 # src[] = { value, offset }.
600 # const_index[] = { base, write_mask, align_mul, align_offset }
601 store("shared", 2, [BASE, WRMASK, ALIGN_MUL, ALIGN_OFFSET])