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 "pipe/p_defines.h"
29 #include "svga_screen.h"
30 #include "svga_context.h"
31 #include "svga_state.h"
33 #include "svga_tgsi.h"
34 #include "svga_debug.h"
36 #include "svga_hw_reg.h"
40 * Don't try to send more than 4k of successive constants.
42 #define MAX_CONST_REG_COUNT 256 /* 4k */
45 /***********************************************************************
49 /* Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
51 static int svga_shader_type( unsigned shader
)
53 assert(PIPE_SHADER_VERTEX
+ 1 == SVGA3D_SHADERTYPE_VS
);
54 assert(PIPE_SHADER_FRAGMENT
+ 1 == SVGA3D_SHADERTYPE_PS
);
55 assert(shader
<= PIPE_SHADER_FRAGMENT
);
60 * Check and emit one shader constant register.
61 * \param shader PIPE_SHADER_FRAGMENT or PIPE_SHADER_VERTEX
62 * \param i which float[4] constant to change
63 * \param value the new float[4] value
65 static enum pipe_error
66 emit_const(struct svga_context
*svga
, unsigned shader
, unsigned i
,
69 enum pipe_error ret
= PIPE_OK
;
73 assert(shader
< PIPE_SHADER_TYPES
);
75 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][i
], value
, 4 * sizeof(float)) != 0) {
76 if (SVGA_DEBUG
& DEBUG_CONSTS
)
77 debug_printf("%s %s %u: %f %f %f %f\n",
79 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
86 ret
= SVGA3D_SetShaderConst( svga
->swc
,
88 svga_shader_type(shader
),
89 SVGA3D_CONST_TYPE_FLOAT
,
94 memcpy(svga
->state
.hw_draw
.cb
[shader
][i
], value
, 4 * sizeof(float));
101 * Check and emit a range of shader constant registers, trying to coalesce
102 * successive shader constant updates in a single command in order to save
103 * space on the command buffer. This is a HWv8 feature.
105 static enum pipe_error
emit_const_range( struct svga_context
*svga
,
109 const float (*values
)[4] )
115 if (offset
+ count
> CB_MAX
) {
116 debug_printf("svga: too many constants (offset + count = %u)\n",
121 if (offset
> CB_MAX
) {
122 /* This isn't OK, but if we propagate an error all the way up we'll
123 * just get into more trouble.
124 * XXX note that offset is always zero at this time so this is moot.
129 if (offset
+ count
> CB_MAX
) {
130 /* Just drop the extra constants for now.
131 * Ideally we should not have allowed the app to create a shader
132 * that exceeds our constant buffer size but there's no way to
133 * express that in gallium at this time.
135 count
= CB_MAX
- offset
;
140 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
142 4 * sizeof(float)) != 0) {
145 * Found one dirty constant
148 if (SVGA_DEBUG
& DEBUG_CONSTS
)
149 debug_printf("%s %s %d: %f %f %f %f\n",
151 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
159 * Look for more consecutive dirty constants.
164 j
< i
+ MAX_CONST_REG_COUNT
&&
165 memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ j
],
167 4 * sizeof(float)) != 0) {
169 if (SVGA_DEBUG
& DEBUG_CONSTS
)
170 debug_printf("%s %s %d: %f %f %f %f\n",
172 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
185 * Send them all together.
188 ret
= SVGA3D_SetShaderConsts(svga
->swc
,
190 svga_shader_type(shader
),
191 SVGA3D_CONST_TYPE_FLOAT
,
193 if (ret
!= PIPE_OK
) {
198 * Local copy of the hardware state.
201 memcpy(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
203 (j
- i
) * 4 * sizeof(float));
215 static enum pipe_error
216 emit_consts(struct svga_context
*svga
, unsigned offset
, unsigned shader
)
218 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
219 struct pipe_transfer
*transfer
= NULL
;
221 const float (*data
)[4] = NULL
;
223 enum pipe_error ret
= PIPE_OK
;
225 assert(shader
< PIPE_SHADER_TYPES
);
227 if (svga
->curr
.cb
[shader
] == NULL
)
230 count
= svga
->curr
.cb
[shader
]->width0
/ (4 * sizeof(float));
232 data
= (const float (*)[4])pipe_buffer_map(&svga
->pipe
,
233 svga
->curr
.cb
[shader
],
237 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
241 if (ss
->hw_version
>= SVGA3D_HWVERSION_WS8_B1
) {
242 ret
= emit_const_range( svga
, shader
, offset
, count
, data
);
243 if (ret
!= PIPE_OK
) {
247 for (i
= 0; i
< count
; i
++) {
248 ret
= emit_const( svga
, shader
, offset
+ i
, data
[i
] );
249 if (ret
!= PIPE_OK
) {
257 pipe_buffer_unmap(&svga
->pipe
, transfer
);
263 static enum pipe_error
264 emit_fs_consts(struct svga_context
*svga
, unsigned dirty
)
266 const struct svga_shader_result
*result
= svga
->state
.hw_draw
.fs
;
267 const struct svga_fs_compile_key
*key
= &result
->key
.fkey
;
268 enum pipe_error ret
= PIPE_OK
;
270 ret
= emit_consts( svga
, 0, PIPE_SHADER_FRAGMENT
);
274 /* The internally generated fragment shader for xor blending
275 * doesn't have a 'result' struct. It should be fixed to avoid
276 * this special case, but work around it with a NULL check:
278 if (result
!= NULL
&&
279 key
->num_unnormalized_coords
)
281 unsigned offset
= result
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
284 for (i
= 0; i
< key
->num_textures
; i
++) {
285 if (key
->tex
[i
].unnormalized
) {
286 struct pipe_resource
*tex
= svga
->curr
.sampler_views
[i
]->texture
;
289 data
[0] = 1.0 / (float)tex
->width0
;
290 data
[1] = 1.0 / (float)tex
->height0
;
294 ret
= emit_const( svga
,
295 PIPE_SHADER_FRAGMENT
,
296 key
->tex
[i
].width_height_idx
+ offset
,
303 offset
+= key
->num_unnormalized_coords
;
310 struct svga_tracked_state svga_hw_fs_parameters
=
313 (SVGA_NEW_FS_CONST_BUFFER
|
315 SVGA_NEW_TEXTURE_BINDING
),
319 /***********************************************************************
322 static enum pipe_error
323 emit_vs_consts(struct svga_context
*svga
, unsigned dirty
)
325 const struct svga_shader_result
*result
= svga
->state
.hw_draw
.vs
;
326 const struct svga_vs_compile_key
*key
= &result
->key
.vkey
;
327 enum pipe_error ret
= PIPE_OK
;
330 /* SVGA_NEW_VS_RESULT
335 /* SVGA_NEW_VS_CONST_BUFFER
337 ret
= emit_consts( svga
, 0, PIPE_SHADER_VERTEX
);
341 offset
= result
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
343 /* SVGA_NEW_VS_RESULT
345 if (key
->need_prescale
) {
346 ret
= emit_const( svga
, PIPE_SHADER_VERTEX
, offset
++,
347 svga
->state
.hw_clear
.prescale
.scale
);
351 ret
= emit_const( svga
, PIPE_SHADER_VERTEX
, offset
++,
352 svga
->state
.hw_clear
.prescale
.translate
);
357 /* SVGA_NEW_ZERO_STRIDE
359 if (key
->zero_stride_vertex_elements
) {
360 unsigned i
, curr_zero_stride
= 0;
361 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
) {
362 if (key
->zero_stride_vertex_elements
& (1 << i
)) {
363 ret
= emit_const( svga
, PIPE_SHADER_VERTEX
, offset
++,
364 svga
->curr
.zero_stride_constants
+
365 4 * curr_zero_stride
);
377 struct svga_tracked_state svga_hw_vs_parameters
=
381 SVGA_NEW_VS_CONST_BUFFER
|
382 SVGA_NEW_ZERO_STRIDE
|