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 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "pipe/p_defines.h"
30 #include "svga_screen.h"
31 #include "svga_context.h"
32 #include "svga_state.h"
34 #include "svga_tgsi.h"
35 #include "svga_debug.h"
36 #include "svga_resource_buffer.h"
38 #include "svga_hw_reg.h"
42 * Don't try to send more than 4kb of successive constants.
44 #define MAX_CONST_REG_COUNT 256 /**< number of float[4] constants */
47 * Extra space for svga-specific VS/PS constants (such as texcoord
48 * scale factors, vertex transformation scale/translation).
50 #define MAX_EXTRA_CONSTS 32
52 /** Guest-backed surface constant buffers must be this size */
53 #define GB_CONSTBUF_SIZE (SVGA3D_CONSTREG_MAX)
56 * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
59 svga_shader_type(unsigned shader
)
62 case PIPE_SHADER_VERTEX
:
63 return SVGA3D_SHADERTYPE_VS
;
64 case PIPE_SHADER_FRAGMENT
:
65 return SVGA3D_SHADERTYPE_PS
;
67 assert(!"Unexpected shader type");
68 return SVGA3D_SHADERTYPE_VS
;
74 * Emit any extra fragment shader constants into the buffer pointed
76 * In particular, these would be the scaling factors needed for handling
77 * unnormalized texture coordinates for texture rectangles.
78 * \return number of float[4] constants put into the dest buffer
81 svga_get_extra_fs_constants(struct svga_context
*svga
, float *dest
)
83 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.fs
;
84 const struct svga_fs_compile_key
*key
= &variant
->key
.fkey
;
87 /* SVGA_NEW_VS_VARIANT
89 if (key
->num_unnormalized_coords
) {
92 for (i
= 0; i
< key
->num_textures
; i
++) {
93 if (key
->tex
[i
].unnormalized
) {
94 struct pipe_resource
*tex
= svga
->curr
.sampler_views
[i
]->texture
;
96 /* debug/sanity check */
97 assert(key
->tex
[i
].width_height_idx
== count
);
99 *dest
++ = 1.0 / (float)tex
->width0
;
100 *dest
++ = 1.0 / (float)tex
->height0
;
109 assert(count
<= MAX_EXTRA_CONSTS
);
116 * Emit any extra vertex shader constants into the buffer pointed
118 * In particular, these would be the scale and bias factors computed
119 * from the framebuffer size which are used to copy with differences in
120 * GL vs D3D coordinate spaces. See svga_tgsi_insn.c for more info.
121 * \return number of float[4] constants put into the dest buffer
124 svga_get_extra_vs_constants(struct svga_context
*svga
, float *dest
)
126 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.vs
;
127 const struct svga_vs_compile_key
*key
= &variant
->key
.vkey
;
130 /* SVGA_NEW_VS_VARIANT
132 if (key
->need_prescale
) {
133 memcpy(dest
, svga
->state
.hw_clear
.prescale
.scale
, 4 * sizeof(float));
136 memcpy(dest
, svga
->state
.hw_clear
.prescale
.translate
, 4 * sizeof(float));
142 assert(count
<= MAX_EXTRA_CONSTS
);
149 * Check and emit one shader constant register.
150 * \param shader PIPE_SHADER_FRAGMENT or PIPE_SHADER_VERTEX
151 * \param i which float[4] constant to change
152 * \param value the new float[4] value
154 static enum pipe_error
155 emit_const(struct svga_context
*svga
, unsigned shader
, unsigned i
,
158 enum pipe_error ret
= PIPE_OK
;
160 assert(shader
< PIPE_SHADER_TYPES
);
161 assert(i
< SVGA3D_CONSTREG_MAX
);
163 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][i
], value
,
164 4 * sizeof(float)) != 0) {
165 if (SVGA_DEBUG
& DEBUG_CONSTS
)
166 debug_printf("%s %s %u: %f %f %f %f\n",
168 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
175 ret
= SVGA3D_SetShaderConst( svga
->swc
,
177 svga_shader_type(shader
),
178 SVGA3D_CONST_TYPE_FLOAT
,
183 memcpy(svga
->state
.hw_draw
.cb
[shader
][i
], value
, 4 * sizeof(float));
191 * Check and emit a range of shader constant registers, trying to coalesce
192 * successive shader constant updates in a single command in order to save
193 * space on the command buffer. This is a HWv8 feature.
195 static enum pipe_error
196 emit_const_range(struct svga_context
*svga
,
200 const float (*values
)[4])
206 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
207 debug_printf("svga: too many constants (offset %u + count %u = %u (max = %u))\n",
208 offset
, count
, offset
+ count
, SVGA3D_CONSTREG_MAX
);
212 if (offset
> SVGA3D_CONSTREG_MAX
) {
213 /* This isn't OK, but if we propagate an error all the way up we'll
214 * just get into more trouble.
215 * XXX note that offset is always zero at this time so this is moot.
220 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
221 /* Just drop the extra constants for now.
222 * Ideally we should not have allowed the app to create a shader
223 * that exceeds our constant buffer size but there's no way to
224 * express that in gallium at this time.
226 count
= SVGA3D_CONSTREG_MAX
- offset
;
231 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
233 4 * sizeof(float)) != 0) {
234 /* Found one dirty constant
236 if (SVGA_DEBUG
& DEBUG_CONSTS
)
237 debug_printf("%s %s %d: %f %f %f %f\n",
239 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
246 /* Look for more consecutive dirty constants.
250 j
< i
+ MAX_CONST_REG_COUNT
&&
251 memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ j
],
253 4 * sizeof(float)) != 0) {
255 if (SVGA_DEBUG
& DEBUG_CONSTS
)
256 debug_printf("%s %s %d: %f %f %f %f\n",
258 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
270 /* Send them all together.
272 if (svga_have_gb_objects(svga
)) {
273 ret
= SVGA3D_SetGBShaderConstsInline(svga
->swc
,
274 offset
+ i
, /* start */
276 svga_shader_type(shader
),
277 SVGA3D_CONST_TYPE_FLOAT
,
281 ret
= SVGA3D_SetShaderConsts(svga
->swc
,
283 svga_shader_type(shader
),
284 SVGA3D_CONST_TYPE_FLOAT
,
287 if (ret
!= PIPE_OK
) {
292 * Local copy of the hardware state.
294 memcpy(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
296 (j
- i
) * 4 * sizeof(float));
309 * Emit all the constants in a constant buffer for a shader stage.
311 static enum pipe_error
312 emit_consts(struct svga_context
*svga
, unsigned shader
)
314 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
315 struct pipe_transfer
*transfer
= NULL
;
317 const float (*data
)[4] = NULL
;
319 enum pipe_error ret
= PIPE_OK
;
320 const unsigned offset
= 0;
322 assert(shader
< PIPE_SHADER_TYPES
);
324 if (svga
->curr
.cbufs
[shader
].buffer
== NULL
)
327 data
= (const float (*)[4])pipe_buffer_map(&svga
->pipe
,
328 svga
->curr
.cbufs
[shader
].buffer
,
332 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
337 assert(svga
->curr
.cbufs
[shader
].buffer
->width0
>=
338 svga
->curr
.cbufs
[shader
].buffer_size
);
340 /* Use/apply the constant buffer size and offsets here */
341 count
= svga
->curr
.cbufs
[shader
].buffer_size
/ (4 * sizeof(float));
342 data
+= svga
->curr
.cbufs
[shader
].buffer_offset
/ (4 * sizeof(float));
344 if (ss
->hw_version
>= SVGA3D_HWVERSION_WS8_B1
) {
345 ret
= emit_const_range( svga
, shader
, offset
, count
, data
);
346 if (ret
!= PIPE_OK
) {
350 for (i
= 0; i
< count
; i
++) {
351 ret
= emit_const( svga
, shader
, offset
+ i
, data
[i
] );
352 if (ret
!= PIPE_OK
) {
360 pipe_buffer_unmap(&svga
->pipe
, transfer
);
366 static enum pipe_error
367 emit_fs_consts(struct svga_context
*svga
, unsigned dirty
)
369 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
370 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.fs
;
371 enum pipe_error ret
= PIPE_OK
;
373 /* SVGA_NEW_FS_VARIANT
378 /* SVGA_NEW_FS_CONST_BUFFER
380 ret
= emit_consts( svga
, PIPE_SHADER_FRAGMENT
);
384 /* emit extra shader constants */
386 unsigned offset
= variant
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
387 float extras
[MAX_EXTRA_CONSTS
][4];
390 count
= svga_get_extra_fs_constants(svga
, (float *) extras
);
392 if (ss
->hw_version
>= SVGA3D_HWVERSION_WS8_B1
) {
393 ret
= emit_const_range(svga
, PIPE_SHADER_FRAGMENT
, offset
, count
,
394 (const float (*) [4])extras
);
396 for (i
= 0; i
< count
; i
++) {
397 ret
= emit_const(svga
, PIPE_SHADER_FRAGMENT
, offset
+ i
, extras
[i
]);
408 struct svga_tracked_state svga_hw_fs_constants
=
411 (SVGA_NEW_FS_CONST_BUFFER
|
412 SVGA_NEW_FS_VARIANT
|
413 SVGA_NEW_TEXTURE_BINDING
),
419 static enum pipe_error
420 emit_vs_consts(struct svga_context
*svga
, unsigned dirty
)
422 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
423 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.vs
;
424 enum pipe_error ret
= PIPE_OK
;
426 /* SVGA_NEW_VS_VARIANT
431 /* SVGA_NEW_VS_CONST_BUFFER
433 ret
= emit_consts( svga
, PIPE_SHADER_VERTEX
);
437 /* emit extra shader constants */
439 unsigned offset
= variant
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
440 float extras
[MAX_EXTRA_CONSTS
][4];
443 count
= svga_get_extra_vs_constants(svga
, (float *) extras
);
444 assert(count
<= Elements(extras
));
446 if (ss
->hw_version
>= SVGA3D_HWVERSION_WS8_B1
) {
447 ret
= emit_const_range(svga
, PIPE_SHADER_VERTEX
, offset
, count
,
448 (const float (*) [4]) extras
);
450 for (i
= 0; i
< count
; i
++) {
451 ret
= emit_const(svga
, PIPE_SHADER_VERTEX
, offset
+ i
, extras
[i
]);
462 struct svga_tracked_state svga_hw_vs_constants
=
466 SVGA_NEW_VS_CONST_BUFFER
|
467 SVGA_NEW_VS_VARIANT
),