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