1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
32 #include "pipe/p_state.h"
33 #include "compiler/shader_enums.h"
38 /* driver param indices: */
39 enum ir3_driver_param
{
40 IR3_DP_VTXID_BASE
= 0,
41 IR3_DP_VTXCNT_MAX
= 1,
42 /* user-clip-plane components, up to 8x vec4's: */
46 IR3_DP_COUNT
= 36 /* must be aligned to vec4 */
49 /* Layout of constant registers:
51 * num_uniform * vec4 - user consts
52 * 4 * vec4 - UBO addresses
53 * if (vertex shader) {
54 * N * vec4 - driver params (IR3_DP_*)
55 * 1 * vec4 - stream-out addresses
58 * TODO this could be made more dynamic, to at least skip sections
59 * that we don't need..
61 #define IR3_UBOS_OFF 0 /* UBOs after user consts */
62 #define IR3_DRIVER_PARAM_OFF 4 /* driver params after UBOs */
63 #define IR3_TFBOS_OFF (IR3_DRIVER_PARAM_OFF + IR3_DP_COUNT/4)
65 /* Configuration key used to identify a shader variant.. different
66 * shader variants can be used to implement features not supported
67 * in hw (two sided color), binning-pass vertex shader, etc.
69 struct ir3_shader_key
{
73 * Combined Vertex/Fragment shader parameters:
75 unsigned ucp_enables
: 8;
77 /* do we need to check {v,f}saturate_{s,t,r}? */
78 unsigned has_per_samp
: 1;
81 * Vertex shader variant parameters:
83 unsigned binning_pass
: 1;
86 * Fragment shader variant parameters:
88 unsigned color_two_side
: 1;
89 unsigned half_precision
: 1;
90 /* used when shader needs to handle flat varyings (a4xx)
91 * for front/back color inputs to frag shader:
93 unsigned rasterflat
: 1;
98 /* bitmask of sampler which needs coords clamped for vertex
101 uint16_t vsaturate_s
, vsaturate_t
, vsaturate_r
;
103 /* bitmask of sampler which needs coords clamped for frag
106 uint16_t fsaturate_s
, fsaturate_t
, fsaturate_r
;
110 ir3_shader_key_equal(struct ir3_shader_key
*a
, struct ir3_shader_key
*b
)
112 /* slow-path if we need to check {v,f}saturate_{s,t,r} */
113 if (a
->has_per_samp
|| b
->has_per_samp
)
114 return memcmp(a
, b
, sizeof(struct ir3_shader_key
)) == 0;
115 return a
->global
== b
->global
;
118 struct ir3_shader_variant
{
121 /* variant id (for debug) */
124 struct ir3_shader_key key
;
126 struct ir3_info info
;
129 /* the instructions length is in units of instruction groups
130 * (4 instructions for a3xx, 16 instructions for a4xx.. each
131 * instruction is 2 dwords):
135 /* the constants length is in units of vec4's, and is the sum of
136 * the uniforms and the built-in compiler constants
141 * + Let the frag shader determine the position/compmask for the
142 * varyings, since it is the place where we know if the varying
143 * is actually used, and if so, which components are used. So
144 * what the hw calls "outloc" is taken from the "inloc" of the
146 * + From the vert shader, we only need the output regid
149 /* for frag shader, pos_regid holds the frag_pos, ie. what is passed
150 * to bary.f instructions
153 bool frag_coord
, frag_face
, color0_mrt
;
155 /* NOTE: for input/outputs, slot is:
156 * gl_vert_attrib - for VS inputs
157 * gl_varying_slot - for VS output / FS input
158 * gl_frag_result - for FS output
161 /* varyings/outputs: */
162 unsigned outputs_count
;
166 } outputs
[16 + 2]; /* +POSITION +PSIZE */
167 bool writes_pos
, writes_psize
;
169 /* attributes (VS) / varyings (FS):
170 * Note that sysval's should come *after* normal inputs.
172 unsigned inputs_count
;
178 /* In theory inloc of fs should match outloc of vs. Or
179 * rather the outloc of the vs is 8 plus the offset passed
180 * to bary.f. Presumably that +8 is to account for
181 * gl_Position/gl_PointSize?
183 * NOTE inloc is currently aligned to 4 (we don't try
184 * to pack varyings). Changing this would likely break
185 * assumptions in few places (like setting up of flat
186 * shading in fd3_program) so be sure to check all the
187 * spots where inloc is used.
190 /* vertex shader specific: */
191 bool sysval
: 1; /* slot is a gl_system_value */
192 /* fragment shader specific: */
193 bool bary
: 1; /* fetched varying (vs one loaded into reg) */
194 bool rasterflat
: 1; /* special handling for emit->rasterflat */
195 enum glsl_interp_qualifier interpolate
;
196 } inputs
[16 + 2]; /* +POSITION +FACE */
198 /* sum of input components (scalar). For frag shaders, it only counts
199 * the varying inputs:
203 /* For frag shaders, the total number of inputs (not scalar,
204 * ie. SP_VS_PARAM_REG.TOTALVSOUTVAR)
208 /* do we have one or more texture sample instructions: */
211 /* do we have kill instructions: */
214 /* const reg # of first immediate, ie. 1 == c1
215 * (not regid, because TGSI thinks in terms of vec4 registers,
216 * not scalar registers)
218 unsigned first_driver_param
;
219 unsigned first_immediate
;
220 unsigned immediates_count
;
225 /* shader variants form a linked list: */
226 struct ir3_shader_variant
*next
;
228 /* replicated here to avoid passing extra ptrs everywhere: */
230 struct ir3_shader
*shader
;
233 typedef struct nir_shader nir_shader
;
238 /* shader id (for debug): */
240 uint32_t variant_count
;
242 struct ir3_compiler
*compiler
;
244 struct pipe_context
*pctx
; /* TODO replace w/ pipe_screen */
246 struct pipe_stream_output_info stream_output
;
248 struct ir3_shader_variant
*variants
;
251 void * ir3_shader_assemble(struct ir3_shader_variant
*v
, uint32_t gpu_id
);
253 struct ir3_shader
* ir3_shader_create(struct pipe_context
*pctx
,
254 const struct pipe_shader_state
*cso
, enum shader_t type
);
255 void ir3_shader_destroy(struct ir3_shader
*shader
);
256 struct ir3_shader_variant
* ir3_shader_variant(struct ir3_shader
*shader
,
257 struct ir3_shader_key key
);
258 void ir3_shader_disasm(struct ir3_shader_variant
*so
, uint32_t *bin
);
260 struct fd_ringbuffer
;
262 void ir3_emit_consts(struct ir3_shader_variant
*v
, struct fd_ringbuffer
*ring
,
263 struct fd_context
*ctx
, const struct pipe_draw_info
*info
, uint32_t dirty
);
265 static inline const char *
266 ir3_shader_stage(struct ir3_shader
*shader
)
268 switch (shader
->type
) {
269 case SHADER_VERTEX
: return "VERT";
270 case SHADER_FRAGMENT
: return "FRAG";
271 case SHADER_COMPUTE
: return "CL";
273 unreachable("invalid type");
282 #include "pipe/p_shader_tokens.h"
285 ir3_find_output(const struct ir3_shader_variant
*so
, gl_varying_slot slot
)
289 for (j
= 0; j
< so
->outputs_count
; j
++)
290 if (so
->outputs
[j
].slot
== slot
)
293 /* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
294 * in the vertex shader.. but the fragment shader doesn't know this
295 * so it will always have both IN.COLOR[n] and IN.BCOLOR[n]. So
296 * at link time if there is no matching OUT.BCOLOR[n], we must map
297 * OUT.COLOR[n] to IN.BCOLOR[n]. And visa versa if there is only
298 * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
300 if (slot
== VARYING_SLOT_BFC0
) {
301 slot
= VARYING_SLOT_COL0
;
302 } else if (slot
== VARYING_SLOT_BFC1
) {
303 slot
= VARYING_SLOT_COL1
;
304 } else if (slot
== VARYING_SLOT_COL0
) {
305 slot
= VARYING_SLOT_BFC0
;
306 } else if (slot
== VARYING_SLOT_COL1
) {
307 slot
= VARYING_SLOT_BFC1
;
312 for (j
= 0; j
< so
->outputs_count
; j
++)
313 if (so
->outputs
[j
].slot
== slot
)
322 ir3_next_varying(const struct ir3_shader_variant
*so
, int i
)
324 while (++i
< so
->inputs_count
)
325 if (so
->inputs
[i
].compmask
&& so
->inputs
[i
].bary
)
330 static inline uint32_t
331 ir3_find_output_regid(const struct ir3_shader_variant
*so
, unsigned slot
)
334 for (j
= 0; j
< so
->outputs_count
; j
++)
335 if (so
->outputs
[j
].slot
== slot
)
336 return so
->outputs
[j
].regid
;
340 #endif /* IR3_SHADER_H_ */