2 * Copyright (c) 2012-2015 Etnaviv Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
27 #include "etnaviv_shader.h"
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_debug.h"
32 #include "etnaviv_util.h"
34 #include "tgsi/tgsi_parse.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
38 /* Link vs and fs together: fill in shader_state from vs and fs
39 * as this function is called every time a new fs or vs is bound, the goal is to
40 * do little processing as possible here, and to precompute as much as possible in
41 * the vs/fs shader_object.
43 * XXX we could cache the link result for a certain set of VS/PS; usually a pair
44 * of VS and PS will be used together anyway.
47 etna_link_shaders(struct etna_context
*ctx
, struct compiled_shader_state
*cs
,
48 const struct etna_shader_variant
*vs
, const struct etna_shader_variant
*fs
)
50 struct etna_shader_link_info link
= { };
52 assert(vs
->processor
== PIPE_SHADER_VERTEX
);
53 assert(fs
->processor
== PIPE_SHADER_FRAGMENT
);
56 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS
)) {
62 if (etna_link_shader(&link
, vs
, fs
)) {
63 /* linking failed: some fs inputs do not have corresponding
70 if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS
)) {
71 debug_printf("link result:\n");
72 debug_printf(" vs -> fs comps use pa_attr\n");
74 for (int idx
= 0; idx
< link
.num_varyings
; ++idx
)
75 debug_printf(" t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
76 link
.varyings
[idx
].reg
, idx
+ 1,
77 link
.varyings
[idx
].num_components
, "xyzw",
78 link
.varyings
[idx
].use
[0], link
.varyings
[idx
].use
[1],
79 link
.varyings
[idx
].use
[2], link
.varyings
[idx
].use
[3],
80 link
.varyings
[idx
].pa_attributes
);
83 /* set last_varying_2x flag if the last varying has 1 or 2 components */
84 bool last_varying_2x
= false;
85 if (link
.num_varyings
> 0 && link
.varyings
[link
.num_varyings
- 1].num_components
<= 2)
86 last_varying_2x
= true;
88 cs
->RA_CONTROL
= VIVS_RA_CONTROL_UNK0
|
89 COND(last_varying_2x
, VIVS_RA_CONTROL_LAST_VARYING_2X
);
91 cs
->PA_ATTRIBUTE_ELEMENT_COUNT
= VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link
.num_varyings
);
92 for (int idx
= 0; idx
< link
.num_varyings
; ++idx
)
93 cs
->PA_SHADER_ATTRIBUTES
[idx
] = link
.varyings
[idx
].pa_attributes
;
95 cs
->VS_END_PC
= vs
->code_size
/ 4;
96 cs
->VS_OUTPUT_COUNT
= 1 + link
.num_varyings
; /* position + varyings */
98 /* vs outputs (varyings) */
99 DEFINE_ETNA_BITARRAY(vs_output
, 16, 8) = {0};
101 etna_bitarray_set(vs_output
, 8, varid
++, vs
->vs_pos_out_reg
);
102 for (int idx
= 0; idx
< link
.num_varyings
; ++idx
)
103 etna_bitarray_set(vs_output
, 8, varid
++, link
.varyings
[idx
].reg
);
104 if (vs
->vs_pointsize_out_reg
>= 0)
105 etna_bitarray_set(vs_output
, 8, varid
++, vs
->vs_pointsize_out_reg
); /* pointsize is last */
107 for (int idx
= 0; idx
< ARRAY_SIZE(cs
->VS_OUTPUT
); ++idx
)
108 cs
->VS_OUTPUT
[idx
] = vs_output
[idx
];
110 if (vs
->vs_pointsize_out_reg
!= -1) {
111 /* vertex shader outputs point coordinate, provide extra output and make
115 cs
->VS_OUTPUT_COUNT_PSIZE
= cs
->VS_OUTPUT_COUNT
+ 1;
117 /* vertex shader does not output point coordinate, make sure thate
118 * POINT_SIZE_ENABLE is masked
119 * and no extra output is given */
120 cs
->PA_CONFIG
= ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE
;
121 cs
->VS_OUTPUT_COUNT_PSIZE
= cs
->VS_OUTPUT_COUNT
;
124 cs
->VS_LOAD_BALANCING
= vs
->vs_load_balancing
;
127 cs
->PS_END_PC
= fs
->code_size
/ 4;
128 cs
->PS_OUTPUT_REG
= fs
->ps_color_out_reg
;
130 VIVS_PS_INPUT_COUNT_COUNT(link
.num_varyings
+ 1) | /* Number of inputs plus position */
131 VIVS_PS_INPUT_COUNT_UNK8(fs
->input_count_unk8
);
132 cs
->PS_TEMP_REGISTER_CONTROL
=
133 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs
->num_temps
, link
.num_varyings
+ 1));
134 cs
->PS_CONTROL
= VIVS_PS_CONTROL_UNK1
; /* XXX when can we set BYPASS? */
137 /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
138 * mode, avoids some fumbling in sync_context. */
139 cs
->PS_INPUT_COUNT_MSAA
=
140 VIVS_PS_INPUT_COUNT_COUNT(link
.num_varyings
+ 2) | /* MSAA adds another input */
141 VIVS_PS_INPUT_COUNT_UNK8(fs
->input_count_unk8
);
142 cs
->PS_TEMP_REGISTER_CONTROL_MSAA
=
143 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs
->num_temps
, link
.num_varyings
+ 2));
145 uint32_t total_components
= 0;
146 DEFINE_ETNA_BITARRAY(num_components
, ETNA_NUM_VARYINGS
, 4) = {0};
147 DEFINE_ETNA_BITARRAY(component_use
, 4 * ETNA_NUM_VARYINGS
, 2) = {0};
148 for (int idx
= 0; idx
< link
.num_varyings
; ++idx
) {
149 const struct etna_varying
*varying
= &link
.varyings
[idx
];
151 etna_bitarray_set(num_components
, 4, idx
, varying
->num_components
);
152 for (int comp
= 0; comp
< varying
->num_components
; ++comp
) {
153 etna_bitarray_set(component_use
, 2, total_components
, varying
->use
[comp
]);
154 total_components
+= 1;
158 cs
->GL_VARYING_TOTAL_COMPONENTS
=
159 VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components
, 2));
160 cs
->GL_VARYING_NUM_COMPONENTS
= num_components
[0];
161 cs
->GL_VARYING_COMPONENT_USE
[0] = component_use
[0];
162 cs
->GL_VARYING_COMPONENT_USE
[1] = component_use
[1];
164 /* reference instruction memory */
165 cs
->vs_inst_mem_size
= vs
->code_size
;
166 cs
->VS_INST_MEM
= vs
->code
;
167 cs
->ps_inst_mem_size
= fs
->code_size
;
168 cs
->PS_INST_MEM
= fs
->code
;
174 etna_shader_link(struct etna_context
*ctx
)
176 if (!ctx
->shader
.vs
|| !ctx
->shader
.fs
)
179 /* re-link vs and fs if needed */
180 return etna_link_shaders(ctx
, &ctx
->shader_state
, ctx
->shader
.vs
, ctx
->shader
.fs
);
184 etna_shader_update_vs_inputs(struct etna_context
*ctx
,
185 struct compiled_shader_state
*cs
,
186 const struct etna_shader_variant
*vs
,
187 const struct compiled_vertex_elements_state
*ves
)
189 unsigned num_temps
, cur_temp
, num_vs_inputs
;
194 /* Number of vertex elements determines number of VS inputs. Otherwise,
195 * the GPU crashes. Allocate any unused vertex elements to VS temporary
197 num_vs_inputs
= MAX2(ves
->num_elements
, vs
->infile
.num_reg
);
198 if (num_vs_inputs
!= ves
->num_elements
) {
199 BUG("Number of elements %u does not match the number of VS inputs %zu",
200 ctx
->vertex_elements
->num_elements
, ctx
->shader
.vs
->infile
.num_reg
);
204 cur_temp
= vs
->num_temps
;
205 num_temps
= num_vs_inputs
- vs
->infile
.num_reg
+ cur_temp
;
207 cs
->VS_INPUT_COUNT
= VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs
) |
208 VIVS_VS_INPUT_COUNT_UNK8(vs
->input_count_unk8
);
209 cs
->VS_TEMP_REGISTER_CONTROL
=
210 VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps
);
212 /* vs inputs (attributes) */
213 DEFINE_ETNA_BITARRAY(vs_input
, 16, 8) = {0};
214 for (int idx
= 0; idx
< num_vs_inputs
; ++idx
) {
215 if (idx
< vs
->infile
.num_reg
)
216 etna_bitarray_set(vs_input
, 8, idx
, vs
->infile
.reg
[idx
].reg
);
218 etna_bitarray_set(vs_input
, 8, idx
, cur_temp
++);
221 for (int idx
= 0; idx
< ARRAY_SIZE(cs
->VS_INPUT
); ++idx
)
222 cs
->VS_INPUT
[idx
] = vs_input
[idx
];
227 static inline const char *
228 etna_shader_stage(struct etna_shader_variant
*shader
)
230 switch (shader
->processor
) {
231 case PIPE_SHADER_VERTEX
: return "VERT";
232 case PIPE_SHADER_FRAGMENT
: return "FRAG";
233 case PIPE_SHADER_COMPUTE
: return "CL";
235 unreachable("invalid type");
241 dump_shader_info(struct etna_shader_variant
*shader
, struct pipe_debug_callback
*debug
)
243 if (!unlikely(etna_mesa_debug
& ETNA_DBG_SHADERDB
))
246 pipe_debug_message(debug
, SHADER_INFO
, "\n"
247 "SHADER-DB: %s prog %d: %u instructions %u temps\n"
248 "SHADER-DB: %s prog %d: %u immediates %u consts\n"
249 "SHADER-DB: %s prog %d: %u loops\n",
250 etna_shader_stage(shader
),
254 etna_shader_stage(shader
),
256 shader
->uniforms
.imm_count
,
257 shader
->uniforms
.const_count
,
258 etna_shader_stage(shader
),
264 etna_shader_update_vertex(struct etna_context
*ctx
)
266 return etna_shader_update_vs_inputs(ctx
, &ctx
->shader_state
, ctx
->shader
.vs
,
267 ctx
->vertex_elements
);
270 static struct etna_shader_variant
*
271 create_variant(struct etna_shader
*shader
, struct etna_shader_key key
)
273 struct etna_shader_variant
*v
= CALLOC_STRUCT(etna_shader_variant
);
282 ret
= etna_compile_shader(v
);
284 debug_error("compile failed!");
288 v
->id
= ++shader
->variant_count
;
297 struct etna_shader_variant
*
298 etna_shader_variant(struct etna_shader
*shader
, struct etna_shader_key key
,
299 struct pipe_debug_callback
*debug
)
301 struct etna_shader_variant
*v
;
303 for (v
= shader
->variants
; v
; v
= v
->next
)
304 if (etna_shader_key_equal(&key
, &v
->key
))
307 /* compile new variant if it doesn't exist already */
308 v
= create_variant(shader
, key
);
310 v
->next
= shader
->variants
;
311 shader
->variants
= v
;
312 dump_shader_info(v
, debug
);
319 etna_create_shader_state(struct pipe_context
*pctx
,
320 const struct pipe_shader_state
*pss
)
322 struct etna_context
*ctx
= etna_context(pctx
);
323 struct etna_shader
*shader
= CALLOC_STRUCT(etna_shader
);
330 shader
->specs
= &ctx
->specs
;
331 shader
->tokens
= tgsi_dup_tokens(pss
->tokens
);
333 if (etna_mesa_debug
& ETNA_DBG_SHADERDB
) {
334 /* if shader-db run, create a standard variant immediately
335 * (as otherwise nothing will trigger the shader to be
336 * actually compiled).
338 struct etna_shader_key key
= {};
339 etna_shader_variant(shader
, key
, &ctx
->debug
);
346 etna_delete_shader_state(struct pipe_context
*pctx
, void *ss
)
348 struct etna_shader
*shader
= ss
;
349 struct etna_shader_variant
*v
, *t
;
351 v
= shader
->variants
;
355 etna_destroy_shader(t
);
358 FREE(shader
->tokens
);
363 etna_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
365 struct etna_context
*ctx
= etna_context(pctx
);
367 ctx
->shader
.bind_fs
= hwcso
;
368 ctx
->dirty
|= ETNA_DIRTY_SHADER
;
372 etna_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
374 struct etna_context
*ctx
= etna_context(pctx
);
376 ctx
->shader
.bind_vs
= hwcso
;
377 ctx
->dirty
|= ETNA_DIRTY_SHADER
;
381 etna_shader_init(struct pipe_context
*pctx
)
383 pctx
->create_fs_state
= etna_create_shader_state
;
384 pctx
->bind_fs_state
= etna_bind_fs_state
;
385 pctx
->delete_fs_state
= etna_delete_shader_state
;
386 pctx
->create_vs_state
= etna_create_shader_state
;
387 pctx
->bind_vs_state
= etna_bind_vs_state
;
388 pctx
->delete_vs_state
= etna_delete_shader_state
;