1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
27 #include "pipe/p_compiler.h"
28 #include "pipe/p_shader_tokens.h"
29 #include "pipe/p_defines.h"
30 #include "tgsi/tgsi_parse.h"
31 #include "tgsi/tgsi_dump.h"
32 #include "tgsi/tgsi_scan.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/u_bitmask.h"
37 #include "svgadump/svga_shader_dump.h"
39 #include "svga_context.h"
40 #include "svga_tgsi.h"
41 #include "svga_tgsi_emit.h"
42 #include "svga_debug.h"
44 #include "svga_hw_reg.h"
45 #include "svga3d_shaderdefs.h"
48 /* Sinkhole used only in error conditions.
50 static char err_buf
[128];
54 svga_destroy_shader_emitter(struct svga_shader_emitter
*emit
)
56 if (emit
->buf
!= err_buf
)
63 svga_shader_expand(struct svga_shader_emitter
*emit
)
66 unsigned newsize
= emit
->size
* 2;
68 if (emit
->buf
!= err_buf
)
69 new_buf
= REALLOC(emit
->buf
, emit
->size
, newsize
);
73 if (new_buf
== NULL
) {
76 emit
->size
= sizeof(err_buf
);
81 emit
->ptr
= new_buf
+ (emit
->ptr
- emit
->buf
);
88 reserve(struct svga_shader_emitter
*emit
, unsigned nr_dwords
)
90 if (emit
->ptr
- emit
->buf
+ nr_dwords
* sizeof(unsigned) >= emit
->size
) {
91 if (!svga_shader_expand(emit
)) {
101 svga_shader_emit_dword(struct svga_shader_emitter
* emit
, unsigned dword
)
103 if (!reserve(emit
, 1))
106 *(unsigned *) emit
->ptr
= dword
;
107 emit
->ptr
+= sizeof dword
;
113 svga_shader_emit_dwords(struct svga_shader_emitter
* emit
,
114 const unsigned *dwords
, unsigned nr
)
116 if (!reserve(emit
, nr
))
119 memcpy(emit
->ptr
, dwords
, nr
* sizeof *dwords
);
120 emit
->ptr
+= nr
* sizeof *dwords
;
126 svga_shader_emit_opcode(struct svga_shader_emitter
* emit
, unsigned opcode
)
128 SVGA3dShaderInstToken
*here
;
130 if (!reserve(emit
, 1))
133 here
= (SVGA3dShaderInstToken
*) emit
->ptr
;
134 here
->value
= opcode
;
136 if (emit
->insn_offset
) {
137 SVGA3dShaderInstToken
*prev
=
138 (SVGA3dShaderInstToken
*) (emit
->buf
+ emit
->insn_offset
);
139 prev
->size
= (here
- prev
) - 1;
142 emit
->insn_offset
= emit
->ptr
- emit
->buf
;
143 emit
->ptr
+= sizeof(unsigned);
149 svga_shader_emit_header(struct svga_shader_emitter
*emit
)
151 SVGA3dShaderVersion header
;
153 memset(&header
, 0, sizeof header
);
155 switch (emit
->unit
) {
156 case PIPE_SHADER_FRAGMENT
:
157 header
.value
= SVGA3D_PS_30
;
159 case PIPE_SHADER_VERTEX
:
160 header
.value
= SVGA3D_VS_30
;
164 return svga_shader_emit_dword(emit
, header
.value
);
169 * Use the shader info to generate a bitmask indicating which generic
170 * inputs are used by the shader. A set bit indicates that GENERIC[i]
174 svga_get_generic_inputs_mask(const struct tgsi_shader_info
*info
)
176 unsigned i
, mask
= 0x0;
178 for (i
= 0; i
< info
->num_inputs
; i
++) {
179 if (info
->input_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
180 unsigned j
= info
->input_semantic_index
[i
];
181 assert(j
< sizeof(mask
) * 8);
191 * Given a mask of used generic variables (as returned by the above functions)
192 * fill in a table which maps those indexes to small integers.
193 * This table is used by the remap_generic_index() function in
194 * svga_tgsi_decl_sm30.c
195 * Example: if generics_mask = binary(1010) it means that GENERIC[1] and
196 * GENERIC[3] are used. The remap_table will contain:
199 * The remaining table entries will be filled in with the next unused
200 * generic index (in this example, 2).
203 svga_remap_generics(unsigned generics_mask
,
204 int8_t remap_table
[MAX_GENERIC_VARYING
])
206 /* Note texcoord[0] is reserved so start at 1 */
207 unsigned count
= 1, i
;
209 for (i
= 0; i
< MAX_GENERIC_VARYING
; i
++) {
213 /* for each bit set in generic_mask */
214 while (generics_mask
) {
215 unsigned index
= ffs(generics_mask
) - 1;
216 remap_table
[index
] = count
++;
217 generics_mask
&= ~(1 << index
);
223 * Use the generic remap table to map a TGSI generic varying variable
224 * index to a small integer. If the remapping table doesn't have a
225 * valid value for the given index (the table entry is -1) it means
226 * the fragment shader doesn't use that VS output. Just allocate
227 * the next free value in that case. Alternately, we could cull
228 * VS instructions that write to register, or replace the register
229 * with a dummy temp register.
230 * XXX TODO: we should do one of the later as it would save precious
231 * texcoord registers.
234 svga_remap_generic_index(int8_t remap_table
[MAX_GENERIC_VARYING
],
237 assert(generic_index
< MAX_GENERIC_VARYING
);
239 if (generic_index
>= MAX_GENERIC_VARYING
) {
240 /* just don't return a random/garbage value */
241 generic_index
= MAX_GENERIC_VARYING
- 1;
244 if (remap_table
[generic_index
] == -1) {
245 /* This is a VS output that has no matching PS input. Find a
249 for (i
= 0; i
< MAX_GENERIC_VARYING
; i
++) {
250 max
= MAX2(max
, remap_table
[i
]);
252 remap_table
[generic_index
] = max
+ 1;
255 return remap_table
[generic_index
];
260 * Parse TGSI shader and translate to SVGA/DX9 serialized
263 * In this function SVGA shader is emitted to an in-memory buffer that
264 * can be dynamically grown. Once we've finished and know how large
265 * it is, it will be copied to a hardware buffer for upload.
267 static struct svga_shader_variant
*
268 svga_tgsi_translate(const struct svga_shader
*shader
,
269 const struct svga_compile_key
*key
, unsigned unit
)
271 struct svga_shader_variant
*variant
= NULL
;
272 struct svga_shader_emitter emit
;
274 memset(&emit
, 0, sizeof(emit
));
277 emit
.buf
= MALLOC(emit
.size
);
278 if (emit
.buf
== NULL
) {
286 tgsi_scan_shader(shader
->tokens
, &emit
.info
);
288 emit
.imm_start
= emit
.info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
290 if (unit
== PIPE_SHADER_FRAGMENT
)
291 emit
.imm_start
+= key
->fkey
.num_unnormalized_coords
;
293 if (unit
== PIPE_SHADER_VERTEX
) {
294 emit
.imm_start
+= key
->vkey
.need_prescale
? 2 : 0;
297 emit
.nr_hw_float_const
=
298 (emit
.imm_start
+ emit
.info
.file_max
[TGSI_FILE_IMMEDIATE
] + 1);
300 emit
.nr_hw_temp
= emit
.info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
302 if (emit
.nr_hw_temp
>= SVGA3D_TEMPREG_MAX
) {
303 debug_printf("svga: too many temporary registers (%u)\n",
308 emit
.in_main_func
= TRUE
;
310 if (!svga_shader_emit_header(&emit
)) {
311 debug_printf("svga: emit header failed\n");
315 if (!svga_shader_emit_instructions(&emit
, shader
->tokens
)) {
316 debug_printf("svga: emit instructions failed\n");
320 variant
= CALLOC_STRUCT(svga_shader_variant
);
324 variant
->shader
= shader
;
325 variant
->tokens
= (const unsigned *) emit
.buf
;
326 variant
->nr_tokens
= (emit
.ptr
- emit
.buf
) / sizeof(unsigned);
327 memcpy(&variant
->key
, key
, sizeof(*key
));
328 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
330 if (SVGA_DEBUG
& DEBUG_TGSI
) {
331 debug_printf("#####################################\n");
332 debug_printf("Shader %u below\n", shader
->id
);
333 tgsi_dump(shader
->tokens
, 0);
334 if (SVGA_DEBUG
& DEBUG_TGSI
) {
335 debug_printf("Shader %u compiled below\n", shader
->id
);
336 svga_shader_dump(variant
->tokens
, variant
->nr_tokens
, FALSE
);
338 debug_printf("#####################################\n");
350 struct svga_shader_variant
*
351 svga_translate_fragment_program(const struct svga_fragment_shader
*fs
,
352 const struct svga_fs_compile_key
*fkey
)
354 struct svga_compile_key key
;
356 memset(&key
, 0, sizeof(key
));
358 memcpy(&key
.fkey
, fkey
, sizeof *fkey
);
360 memcpy(key
.generic_remap_table
, fs
->generic_remap_table
,
361 sizeof(fs
->generic_remap_table
));
363 return svga_tgsi_translate(&fs
->base
, &key
, PIPE_SHADER_FRAGMENT
);
367 struct svga_shader_variant
*
368 svga_translate_vertex_program(const struct svga_vertex_shader
*vs
,
369 const struct svga_vs_compile_key
*vkey
)
371 struct svga_compile_key key
;
373 memset(&key
, 0, sizeof(key
));
375 memcpy(&key
.vkey
, vkey
, sizeof *vkey
);
377 /* Note: we could alternately store the remap table in the vkey but
378 * that would make it larger. We just regenerate it here instead.
380 svga_remap_generics(vkey
->fs_generic_inputs
, key
.generic_remap_table
);
382 return svga_tgsi_translate(&vs
->base
, &key
, PIPE_SHADER_VERTEX
);