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 4kb of successive constants.
42 #define MAX_CONST_REG_COUNT 256 /**< number of float[4] constants */
47 * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
50 svga_shader_type(unsigned shader
)
52 assert(PIPE_SHADER_VERTEX
+ 1 == SVGA3D_SHADERTYPE_VS
);
53 assert(PIPE_SHADER_FRAGMENT
+ 1 == SVGA3D_SHADERTYPE_PS
);
54 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
;
71 assert(shader
< PIPE_SHADER_TYPES
);
72 assert(i
< SVGA3D_CONSTREG_MAX
);
74 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][i
], value
,
75 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));
102 * Check and emit a range of shader constant registers, trying to coalesce
103 * successive shader constant updates in a single command in order to save
104 * space on the command buffer. This is a HWv8 feature.
106 static enum pipe_error
107 emit_const_range(struct svga_context
*svga
,
111 const float (*values
)[4])
117 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
118 debug_printf("svga: too many constants (offset + count = %u)\n",
123 if (offset
> SVGA3D_CONSTREG_MAX
) {
124 /* This isn't OK, but if we propagate an error all the way up we'll
125 * just get into more trouble.
126 * XXX note that offset is always zero at this time so this is moot.
131 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
132 /* Just drop the extra constants for now.
133 * Ideally we should not have allowed the app to create a shader
134 * that exceeds our constant buffer size but there's no way to
135 * express that in gallium at this time.
137 count
= SVGA3D_CONSTREG_MAX
- offset
;
142 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
144 4 * sizeof(float)) != 0) {
145 /* Found one dirty constant
147 if (SVGA_DEBUG
& DEBUG_CONSTS
)
148 debug_printf("%s %s %d: %f %f %f %f\n",
150 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
157 /* Look for more consecutive dirty constants.
161 j
< i
+ MAX_CONST_REG_COUNT
&&
162 memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ j
],
164 4 * sizeof(float)) != 0) {
166 if (SVGA_DEBUG
& DEBUG_CONSTS
)
167 debug_printf("%s %s %d: %f %f %f %f\n",
169 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
181 /* Send them all together.
183 ret
= SVGA3D_SetShaderConsts(svga
->swc
,
185 svga_shader_type(shader
),
186 SVGA3D_CONST_TYPE_FLOAT
,
188 if (ret
!= PIPE_OK
) {
193 * Local copy of the hardware state.
195 memcpy(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
197 (j
- i
) * 4 * sizeof(float));
210 * Emit all the constants in a constant buffer for a shader stage.
212 static enum pipe_error
213 emit_consts(struct svga_context
*svga
, unsigned shader
)
215 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
216 struct pipe_transfer
*transfer
= NULL
;
218 const float (*data
)[4] = NULL
;
220 enum pipe_error ret
= PIPE_OK
;
221 const unsigned offset
= 0;
223 assert(shader
< PIPE_SHADER_TYPES
);
225 if (svga
->curr
.cb
[shader
] == NULL
)
228 count
= svga
->curr
.cb
[shader
]->width0
/ (4 * sizeof(float));
230 data
= (const float (*)[4])pipe_buffer_map(&svga
->pipe
,
231 svga
->curr
.cb
[shader
],
235 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
239 if (ss
->hw_version
>= SVGA3D_HWVERSION_WS8_B1
) {
240 ret
= emit_const_range( svga
, shader
, offset
, count
, data
);
241 if (ret
!= PIPE_OK
) {
245 for (i
= 0; i
< count
; i
++) {
246 ret
= emit_const( svga
, shader
, offset
+ i
, data
[i
] );
247 if (ret
!= PIPE_OK
) {
255 pipe_buffer_unmap(&svga
->pipe
, transfer
);
261 static enum pipe_error
262 emit_fs_consts(struct svga_context
*svga
, unsigned dirty
)
264 const struct svga_shader_result
*result
= svga
->state
.hw_draw
.fs
;
265 const struct svga_fs_compile_key
*key
= &result
->key
.fkey
;
266 enum pipe_error ret
= PIPE_OK
;
268 ret
= emit_consts( svga
, PIPE_SHADER_FRAGMENT
);
272 /* The internally generated fragment shader for xor blending
273 * doesn't have a 'result' struct. It should be fixed to avoid
274 * this special case, but work around it with a NULL check:
276 if (result
!= NULL
&& key
->num_unnormalized_coords
) {
277 unsigned offset
= result
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
280 for (i
= 0; i
< key
->num_textures
; i
++) {
281 if (key
->tex
[i
].unnormalized
) {
282 struct pipe_resource
*tex
= svga
->curr
.sampler_views
[i
]->texture
;
285 data
[0] = 1.0 / (float)tex
->width0
;
286 data
[1] = 1.0 / (float)tex
->height0
;
290 ret
= emit_const( svga
,
291 PIPE_SHADER_FRAGMENT
,
292 key
->tex
[i
].width_height_idx
+ offset
,
299 offset
+= key
->num_unnormalized_coords
;
306 struct svga_tracked_state svga_hw_fs_constants
=
309 (SVGA_NEW_FS_CONST_BUFFER
|
311 SVGA_NEW_TEXTURE_BINDING
),
317 static enum pipe_error
318 emit_vs_consts(struct svga_context
*svga
, unsigned dirty
)
320 const struct svga_shader_result
*result
= svga
->state
.hw_draw
.vs
;
321 const struct svga_vs_compile_key
*key
= &result
->key
.vkey
;
322 enum pipe_error ret
= PIPE_OK
;
325 /* SVGA_NEW_VS_RESULT
330 /* SVGA_NEW_VS_CONST_BUFFER
332 ret
= emit_consts( svga
, PIPE_SHADER_VERTEX
);
336 /* offset = number of constants in the VS const buffer */
337 offset
= result
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
339 /* SVGA_NEW_VS_RESULT
340 * Put the viewport pre-scale/translate values into the const buffer.
342 if (key
->need_prescale
) {
343 ret
= emit_const( svga
, PIPE_SHADER_VERTEX
, offset
++,
344 svga
->state
.hw_clear
.prescale
.scale
);
348 ret
= emit_const( svga
, PIPE_SHADER_VERTEX
, offset
++,
349 svga
->state
.hw_clear
.prescale
.translate
);
358 struct svga_tracked_state svga_hw_vs_constants
=
362 SVGA_NEW_VS_CONST_BUFFER
|