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"
37 /* driver param indices: */
38 enum ir3_driver_param
{
39 IR3_DP_VTXID_BASE
= 0,
40 IR3_DP_VTXCNT_MAX
= 1,
43 /* internal semantic used for passing vtxcnt to vertex shader to
44 * implement transform feedback:
46 #define IR3_SEMANTIC_VTXCNT (TGSI_SEMANTIC_COUNT + 0)
48 typedef uint16_t ir3_semantic
; /* semantic name + index */
49 static inline ir3_semantic
50 ir3_semantic_name(uint8_t name
, uint16_t index
)
52 return (name
<< 8) | (index
& 0xff);
55 static inline uint8_t sem2name(ir3_semantic sem
)
60 static inline uint16_t sem2idx(ir3_semantic sem
)
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
{
72 /* do we need to check {v,f}saturate_{s,t,r}? */
73 unsigned has_per_samp
: 1;
76 * Vertex shader variant parameters:
78 unsigned binning_pass
: 1;
81 * Fragment shader variant parameters:
83 unsigned color_two_side
: 1;
84 unsigned half_precision
: 1;
85 /* used when shader needs to handle flat varyings (a4xx),
86 * for TGSI_INTERPOLATE_COLOR:
88 unsigned rasterflat
: 1;
93 /* bitmask of sampler which needs coords clamped for vertex
96 uint16_t vsaturate_s
, vsaturate_t
, vsaturate_r
;
98 /* bitmask of sampler which needs coords clamped for frag
101 uint16_t fsaturate_s
, fsaturate_t
, fsaturate_r
;
105 ir3_shader_key_equal(struct ir3_shader_key
*a
, struct ir3_shader_key
*b
)
107 /* slow-path if we need to check {v,f}saturate_{s,t,r} */
108 if (a
->has_per_samp
|| b
->has_per_samp
)
109 return memcmp(a
, b
, sizeof(struct ir3_shader_key
)) == 0;
110 return a
->global
== b
->global
;
113 struct ir3_shader_variant
{
116 /* variant id (for debug) */
119 struct ir3_shader_key key
;
121 struct ir3_info info
;
124 /* the instructions length is in units of instruction groups
125 * (4 instructions for a3xx, 16 instructions for a4xx.. each
126 * instruction is 2 dwords):
130 /* the constants length is in units of vec4's, and is the sum of
131 * the uniforms and the built-in compiler constants
136 * + Let the frag shader determine the position/compmask for the
137 * varyings, since it is the place where we know if the varying
138 * is actually used, and if so, which components are used. So
139 * what the hw calls "outloc" is taken from the "inloc" of the
141 * + From the vert shader, we only need the output regid
144 /* for frag shader, pos_regid holds the frag_pos, ie. what is passed
145 * to bary.f instructions
148 bool frag_coord
, frag_face
, color0_mrt
;
150 /* varyings/outputs: */
151 unsigned outputs_count
;
153 ir3_semantic semantic
;
155 } outputs
[16 + 2]; /* +POSITION +PSIZE */
156 bool writes_pos
, writes_psize
;
158 /* vertices/inputs: */
159 unsigned inputs_count
;
161 ir3_semantic semantic
;
165 /* In theory inloc of fs should match outloc of vs. Or
166 * rather the outloc of the vs is 8 plus the offset passed
167 * to bary.f. Presumably that +8 is to account for
168 * gl_Position/gl_PointSize?
170 * NOTE inloc is currently aligned to 4 (we don't try
171 * to pack varyings). Changing this would likely break
172 * assumptions in few places (like setting up of flat
173 * shading in fd3_program) so be sure to check all the
174 * spots where inloc is used.
179 } inputs
[16 + 2]; /* +POSITION +FACE */
181 unsigned total_in
; /* sum of inputs (scalar) */
183 /* do we have one or more texture sample instructions: */
186 /* do we have kill instructions: */
189 /* const reg # of first immediate, ie. 1 == c1
190 * (not regid, because TGSI thinks in terms of vec4 registers,
191 * not scalar registers)
193 unsigned first_driver_param
;
194 unsigned first_immediate
;
195 unsigned immediates_count
;
200 /* shader variants form a linked list: */
201 struct ir3_shader_variant
*next
;
203 /* replicated here to avoid passing extra ptrs everywhere: */
205 struct ir3_shader
*shader
;
211 /* shader id (for debug): */
213 uint32_t variant_count
;
215 struct ir3_compiler
*compiler
;
217 struct pipe_context
*pctx
;
218 const struct tgsi_token
*tokens
;
219 struct pipe_stream_output_info stream_output
;
221 struct ir3_shader_variant
*variants
;
223 /* so far, only used for blit_prog shader.. values for
224 * VPC_VARYING_PS_REPL[i].MODE
229 void * ir3_shader_assemble(struct ir3_shader_variant
*v
, uint32_t gpu_id
);
231 struct ir3_shader
* ir3_shader_create(struct pipe_context
*pctx
,
232 const struct pipe_shader_state
*cso
, enum shader_t type
);
233 void ir3_shader_destroy(struct ir3_shader
*shader
);
234 struct ir3_shader_variant
* ir3_shader_variant(struct ir3_shader
*shader
,
235 struct ir3_shader_key key
);
236 void ir3_shader_disasm(struct ir3_shader_variant
*so
, uint32_t *bin
);
238 struct fd_ringbuffer
;
239 void ir3_emit_consts(struct ir3_shader_variant
*v
, struct fd_ringbuffer
*ring
,
240 const struct pipe_draw_info
*info
, uint32_t dirty
);
242 static inline const char *
243 ir3_shader_stage(struct ir3_shader
*shader
)
245 switch (shader
->type
) {
246 case SHADER_VERTEX
: return "VERT";
247 case SHADER_FRAGMENT
: return "FRAG";
248 case SHADER_COMPUTE
: return "CL";
250 unreachable("invalid type");
259 #include "pipe/p_shader_tokens.h"
262 ir3_find_output(const struct ir3_shader_variant
*so
, ir3_semantic semantic
)
266 for (j
= 0; j
< so
->outputs_count
; j
++)
267 if (so
->outputs
[j
].semantic
== semantic
)
270 /* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
271 * in the vertex shader.. but the fragment shader doesn't know this
272 * so it will always have both IN.COLOR[n] and IN.BCOLOR[n]. So
273 * at link time if there is no matching OUT.BCOLOR[n], we must map
274 * OUT.COLOR[n] to IN.BCOLOR[n]. And visa versa if there is only
275 * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
277 if (sem2name(semantic
) == TGSI_SEMANTIC_BCOLOR
) {
278 unsigned idx
= sem2idx(semantic
);
279 semantic
= ir3_semantic_name(TGSI_SEMANTIC_COLOR
, idx
);
280 } else if (sem2name(semantic
) == TGSI_SEMANTIC_COLOR
) {
281 unsigned idx
= sem2idx(semantic
);
282 semantic
= ir3_semantic_name(TGSI_SEMANTIC_BCOLOR
, idx
);
287 for (j
= 0; j
< so
->outputs_count
; j
++)
288 if (so
->outputs
[j
].semantic
== semantic
)
297 ir3_next_varying(const struct ir3_shader_variant
*so
, int i
)
299 while (++i
< so
->inputs_count
)
300 if (so
->inputs
[i
].compmask
&& so
->inputs
[i
].bary
)
305 static inline uint32_t
306 ir3_find_output_regid(const struct ir3_shader_variant
*so
, ir3_semantic semantic
)
309 for (j
= 0; j
< so
->outputs_count
; j
++)
310 if (so
->outputs
[j
].semantic
== semantic
)
311 return so
->outputs
[j
].regid
;
315 #endif /* IR3_SHADER_H_ */