1 /**********************************************************
2 * Copyright 2008-2012 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 **********************************************************/
26 #include "util/u_bitmask.h"
27 #include "util/u_memory.h"
28 #include "util/u_format.h"
29 #include "svga_context.h"
31 #include "svga_format.h"
32 #include "svga_shader.h"
33 #include "svga_resource_texture.h"
37 * This bit isn't really used anywhere. It only serves to help
38 * generate a unique "signature" for the vertex shader output bitmask.
39 * Shader input/output signatures are used to resolve shader linking
42 #define FOG_GENERIC_BIT (((uint64_t) 1) << 63)
46 * Use the shader info to generate a bitmask indicating which generic
47 * inputs are used by the shader. A set bit indicates that GENERIC[i]
51 svga_get_generic_inputs_mask(const struct tgsi_shader_info
*info
)
56 for (i
= 0; i
< info
->num_inputs
; i
++) {
57 if (info
->input_semantic_name
[i
] == TGSI_SEMANTIC_GENERIC
) {
58 unsigned j
= info
->input_semantic_index
[i
];
59 assert(j
< sizeof(mask
) * 8);
60 mask
|= ((uint64_t) 1) << j
;
69 * Scan shader info to return a bitmask of written outputs.
72 svga_get_generic_outputs_mask(const struct tgsi_shader_info
*info
)
77 for (i
= 0; i
< info
->num_outputs
; i
++) {
78 switch (info
->output_semantic_name
[i
]) {
79 case TGSI_SEMANTIC_GENERIC
:
81 unsigned j
= info
->output_semantic_index
[i
];
82 assert(j
< sizeof(mask
) * 8);
83 mask
|= ((uint64_t) 1) << j
;
86 case TGSI_SEMANTIC_FOG
:
87 mask
|= FOG_GENERIC_BIT
;
98 * Given a mask of used generic variables (as returned by the above functions)
99 * fill in a table which maps those indexes to small integers.
100 * This table is used by the remap_generic_index() function in
101 * svga_tgsi_decl_sm30.c
102 * Example: if generics_mask = binary(1010) it means that GENERIC[1] and
103 * GENERIC[3] are used. The remap_table will contain:
106 * The remaining table entries will be filled in with the next unused
107 * generic index (in this example, 2).
110 svga_remap_generics(uint64_t generics_mask
,
111 int8_t remap_table
[MAX_GENERIC_VARYING
])
113 /* Note texcoord[0] is reserved so start at 1 */
114 unsigned count
= 1, i
;
116 for (i
= 0; i
< MAX_GENERIC_VARYING
; i
++) {
120 /* for each bit set in generic_mask */
121 while (generics_mask
) {
122 unsigned index
= ffsll(generics_mask
) - 1;
123 remap_table
[index
] = count
++;
124 generics_mask
&= ~((uint64_t) 1 << index
);
130 * Use the generic remap table to map a TGSI generic varying variable
131 * index to a small integer. If the remapping table doesn't have a
132 * valid value for the given index (the table entry is -1) it means
133 * the fragment shader doesn't use that VS output. Just allocate
134 * the next free value in that case. Alternately, we could cull
135 * VS instructions that write to register, or replace the register
136 * with a dummy temp register.
137 * XXX TODO: we should do one of the later as it would save precious
138 * texcoord registers.
141 svga_remap_generic_index(int8_t remap_table
[MAX_GENERIC_VARYING
],
144 assert(generic_index
< MAX_GENERIC_VARYING
);
146 if (generic_index
>= MAX_GENERIC_VARYING
) {
147 /* just don't return a random/garbage value */
148 generic_index
= MAX_GENERIC_VARYING
- 1;
151 if (remap_table
[generic_index
] == -1) {
152 /* This is a VS output that has no matching PS input. Find a
156 for (i
= 0; i
< MAX_GENERIC_VARYING
; i
++) {
157 max
= MAX2(max
, remap_table
[i
]);
159 remap_table
[generic_index
] = max
+ 1;
162 return remap_table
[generic_index
];
165 static const enum pipe_swizzle copy_alpha
[PIPE_SWIZZLE_MAX
] = {
175 static const enum pipe_swizzle set_alpha
[PIPE_SWIZZLE_MAX
] = {
186 * Initialize the shader-neutral fields of svga_compile_key from context
187 * state. This is basically the texture-related state.
190 svga_init_shader_key_common(const struct svga_context
*svga
,
191 enum pipe_shader_type shader
,
192 struct svga_compile_key
*key
)
196 assert(shader
< ARRAY_SIZE(svga
->curr
.num_sampler_views
));
198 /* In case the number of samplers and sampler_views doesn't match,
199 * loop over the lower of the two counts.
201 key
->num_textures
= MAX2(svga
->curr
.num_sampler_views
[shader
],
202 svga
->curr
.num_samplers
[shader
]);
204 for (i
= 0; i
< key
->num_textures
; i
++) {
205 struct pipe_sampler_view
*view
= svga
->curr
.sampler_views
[shader
][i
];
206 const struct svga_sampler_state
*sampler
= svga
->curr
.sampler
[shader
][i
];
208 assert(view
->texture
);
209 assert(view
->texture
->target
< (1 << 4)); /* texture_target:4 */
211 /* 1D/2D array textures with one slice and cube map array textures
212 * with one cube are treated as non-arrays by the SVGA3D device.
213 * Set the is_array flag only if we know that we have more than 1
214 * element. This will be used to select shader instruction/resource
215 * types during shader translation.
217 switch (view
->texture
->target
) {
218 case PIPE_TEXTURE_1D_ARRAY
:
219 case PIPE_TEXTURE_2D_ARRAY
:
220 key
->tex
[i
].is_array
= view
->texture
->array_size
> 1;
222 case PIPE_TEXTURE_CUBE_ARRAY
:
223 key
->tex
[i
].is_array
= view
->texture
->array_size
> 6;
226 ; /* nothing / silence compiler warning */
229 assert(view
->texture
->nr_samples
< (1 << 5)); /* 5-bit field */
230 key
->tex
[i
].num_samples
= view
->texture
->nr_samples
;
232 /* If we have a non-alpha view into an svga3d surface with an
233 * alpha channel, then explicitly set the alpha channel to 1
234 * when sampling. Note that we need to check the
235 * actual device format to cover also imported surface cases.
237 const enum pipe_swizzle
*swizzle_tab
=
238 (view
->texture
->target
!= PIPE_BUFFER
&&
239 !util_format_has_alpha(view
->format
) &&
240 svga_texture_device_format_has_alpha(view
->texture
)) ?
241 set_alpha
: copy_alpha
;
243 if (view
->texture
->format
== PIPE_FORMAT_DXT1_RGB
||
244 view
->texture
->format
== PIPE_FORMAT_DXT1_SRGB
)
245 swizzle_tab
= set_alpha
;
247 key
->tex
[i
].swizzle_r
= swizzle_tab
[view
->swizzle_r
];
248 key
->tex
[i
].swizzle_g
= swizzle_tab
[view
->swizzle_g
];
249 key
->tex
[i
].swizzle_b
= swizzle_tab
[view
->swizzle_b
];
250 key
->tex
[i
].swizzle_a
= swizzle_tab
[view
->swizzle_a
];
254 if (!sampler
->normalized_coords
) {
255 assert(idx
< (1 << 5)); /* width_height_idx:5 bitfield */
256 key
->tex
[i
].width_height_idx
= idx
++;
257 key
->tex
[i
].unnormalized
= TRUE
;
258 ++key
->num_unnormalized_coords
;
260 if (sampler
->magfilter
== SVGA3D_TEX_FILTER_NEAREST
||
261 sampler
->minfilter
== SVGA3D_TEX_FILTER_NEAREST
) {
262 key
->tex
[i
].texel_bias
= TRUE
;
270 /** Search for a compiled shader variant with the same compile key */
271 struct svga_shader_variant
*
272 svga_search_shader_key(const struct svga_shader
*shader
,
273 const struct svga_compile_key
*key
)
275 struct svga_shader_variant
*variant
= shader
->variants
;
279 for ( ; variant
; variant
= variant
->next
) {
280 if (svga_compile_keys_equal(key
, &variant
->key
))
286 /** Search for a shader with the same token key */
288 svga_search_shader_token_key(struct svga_shader
*pshader
,
289 const struct svga_token_key
*key
)
291 struct svga_shader
*shader
= pshader
;
295 for ( ; shader
; shader
= shader
->next
) {
296 if (memcmp(key
, &shader
->token_key
, sizeof(struct svga_token_key
)) == 0)
303 * Helper function to define a gb shader for non-vgpu10 device
305 static enum pipe_error
306 define_gb_shader_vgpu9(struct svga_context
*svga
,
307 SVGA3dShaderType type
,
308 struct svga_shader_variant
*variant
,
311 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
315 * Create gb memory for the shader and upload the shader code.
316 * Kernel module will allocate an id for the shader and issue
317 * the DefineGBShader command.
319 variant
->gb_shader
= sws
->shader_create(sws
, type
,
320 variant
->tokens
, codeLen
);
322 if (!variant
->gb_shader
)
323 return PIPE_ERROR_OUT_OF_MEMORY
;
325 ret
= SVGA3D_BindGBShader(svga
->swc
, variant
->gb_shader
);
331 * Helper function to define a gb shader for vgpu10 device
333 static enum pipe_error
334 define_gb_shader_vgpu10(struct svga_context
*svga
,
335 SVGA3dShaderType type
,
336 struct svga_shader_variant
*variant
,
339 struct svga_winsys_context
*swc
= svga
->swc
;
343 * Shaders in VGPU10 enabled device reside in the device COTable.
344 * SVGA driver will allocate an integer ID for the shader and
345 * issue DXDefineShader and DXBindShader commands.
347 variant
->id
= util_bitmask_add(svga
->shader_id_bm
);
348 if (variant
->id
== UTIL_BITMASK_INVALID_INDEX
) {
349 return PIPE_ERROR_OUT_OF_MEMORY
;
352 /* Create gb memory for the shader and upload the shader code */
353 variant
->gb_shader
= swc
->shader_create(swc
,
355 variant
->tokens
, codeLen
);
357 if (!variant
->gb_shader
) {
358 /* Free the shader ID */
359 assert(variant
->id
!= UTIL_BITMASK_INVALID_INDEX
);
360 goto fail_no_allocation
;
364 * Since we don't want to do any flush within state emission to avoid
365 * partial state in a command buffer, it's important to make sure that
366 * there is enough room to send both the DXDefineShader & DXBindShader
367 * commands in the same command buffer. So let's send both
368 * commands in one command reservation. If it fails, we'll undo
369 * the shader creation and return an error.
371 ret
= SVGA3D_vgpu10_DefineAndBindShader(swc
, variant
->gb_shader
,
372 variant
->id
, type
, codeLen
);
380 swc
->shader_destroy(swc
, variant
->gb_shader
);
381 variant
->gb_shader
= NULL
;
384 util_bitmask_clear(svga
->shader_id_bm
, variant
->id
);
385 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
387 return PIPE_ERROR_OUT_OF_MEMORY
;
391 * Issue the SVGA3D commands to define a new shader.
392 * \param variant contains the shader tokens, etc. The result->id field will
396 svga_define_shader(struct svga_context
*svga
,
397 SVGA3dShaderType type
,
398 struct svga_shader_variant
*variant
)
400 unsigned codeLen
= variant
->nr_tokens
* sizeof(variant
->tokens
[0]);
403 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DEFINESHADER
);
405 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
407 if (svga_have_gb_objects(svga
)) {
408 if (svga_have_vgpu10(svga
))
409 ret
= define_gb_shader_vgpu10(svga
, type
, variant
, codeLen
);
411 ret
= define_gb_shader_vgpu9(svga
, type
, variant
, codeLen
);
414 /* Allocate an integer ID for the shader */
415 variant
->id
= util_bitmask_add(svga
->shader_id_bm
);
416 if (variant
->id
== UTIL_BITMASK_INVALID_INDEX
) {
417 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
421 /* Issue SVGA3D device command to define the shader */
422 ret
= SVGA3D_DefineShader(svga
->swc
,
427 if (ret
!= PIPE_OK
) {
429 assert(variant
->id
!= UTIL_BITMASK_INVALID_INDEX
);
430 util_bitmask_clear(svga
->shader_id_bm
, variant
->id
);
431 variant
->id
= UTIL_BITMASK_INVALID_INDEX
;
436 SVGA_STATS_TIME_POP(svga_sws(svga
));
442 * Issue the SVGA3D commands to set/bind a shader.
443 * \param result the shader to bind.
446 svga_set_shader(struct svga_context
*svga
,
447 SVGA3dShaderType type
,
448 struct svga_shader_variant
*variant
)
451 unsigned id
= variant
? variant
->id
: SVGA3D_INVALID_ID
;
453 assert(type
== SVGA3D_SHADERTYPE_VS
||
454 type
== SVGA3D_SHADERTYPE_GS
||
455 type
== SVGA3D_SHADERTYPE_PS
);
457 if (svga_have_gb_objects(svga
)) {
458 struct svga_winsys_gb_shader
*gbshader
=
459 variant
? variant
->gb_shader
: NULL
;
461 if (svga_have_vgpu10(svga
))
462 ret
= SVGA3D_vgpu10_SetShader(svga
->swc
, type
, gbshader
, id
);
464 ret
= SVGA3D_SetGBShader(svga
->swc
, type
, gbshader
);
467 ret
= SVGA3D_SetShader(svga
->swc
, type
, id
);
474 struct svga_shader_variant
*
475 svga_new_shader_variant(struct svga_context
*svga
)
477 svga
->hud
.num_shaders
++;
478 return CALLOC_STRUCT(svga_shader_variant
);
483 svga_destroy_shader_variant(struct svga_context
*svga
,
484 SVGA3dShaderType type
,
485 struct svga_shader_variant
*variant
)
487 enum pipe_error ret
= PIPE_OK
;
489 if (svga_have_gb_objects(svga
) && variant
->gb_shader
) {
490 if (svga_have_vgpu10(svga
)) {
491 struct svga_winsys_context
*swc
= svga
->swc
;
492 swc
->shader_destroy(swc
, variant
->gb_shader
);
493 ret
= SVGA3D_vgpu10_DestroyShader(svga
->swc
, variant
->id
);
494 if (ret
!= PIPE_OK
) {
495 /* flush and try again */
496 svga_context_flush(svga
, NULL
);
497 ret
= SVGA3D_vgpu10_DestroyShader(svga
->swc
, variant
->id
);
499 util_bitmask_clear(svga
->shader_id_bm
, variant
->id
);
502 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
503 sws
->shader_destroy(sws
, variant
->gb_shader
);
505 variant
->gb_shader
= NULL
;
508 if (variant
->id
!= UTIL_BITMASK_INVALID_INDEX
) {
509 ret
= SVGA3D_DestroyShader(svga
->swc
, variant
->id
, type
);
510 if (ret
!= PIPE_OK
) {
511 /* flush and try again */
512 svga_context_flush(svga
, NULL
);
513 ret
= SVGA3D_DestroyShader(svga
->swc
, variant
->id
, type
);
514 assert(ret
== PIPE_OK
);
516 util_bitmask_clear(svga
->shader_id_bm
, variant
->id
);
520 FREE((unsigned *)variant
->tokens
);
523 svga
->hud
.num_shaders
--;
530 * Called at the beginning of every new command buffer to ensure that
531 * shaders are properly paged-in. Instead of sending the SetShader
532 * command, this function sends a private allocation command to
533 * page in a shader. This avoids emitting redundant state to the device
534 * just to page in a resource.
537 svga_rebind_shaders(struct svga_context
*svga
)
539 struct svga_winsys_context
*swc
= svga
->swc
;
540 struct svga_hw_draw_state
*hw
= &svga
->state
.hw_draw
;
543 assert(svga_have_vgpu10(svga
));
546 * If the underlying winsys layer does not need resource rebinding,
547 * just clear the rebind flags and return.
549 if (swc
->resource_rebind
== NULL
) {
550 svga
->rebind
.flags
.vs
= 0;
551 svga
->rebind
.flags
.gs
= 0;
552 svga
->rebind
.flags
.fs
= 0;
557 if (svga
->rebind
.flags
.vs
&& hw
->vs
&& hw
->vs
->gb_shader
) {
558 ret
= swc
->resource_rebind(swc
, NULL
, hw
->vs
->gb_shader
, SVGA_RELOC_READ
);
562 svga
->rebind
.flags
.vs
= 0;
564 if (svga
->rebind
.flags
.gs
&& hw
->gs
&& hw
->gs
->gb_shader
) {
565 ret
= swc
->resource_rebind(swc
, NULL
, hw
->gs
->gb_shader
, SVGA_RELOC_READ
);
569 svga
->rebind
.flags
.gs
= 0;
571 if (svga
->rebind
.flags
.fs
&& hw
->fs
&& hw
->fs
->gb_shader
) {
572 ret
= swc
->resource_rebind(swc
, NULL
, hw
->fs
->gb_shader
, SVGA_RELOC_READ
);
576 svga
->rebind
.flags
.fs
= 0;