svga: Fix potential buffer overflow in rs draw state.
[mesa.git] / src / gallium / drivers / svga / svga_state_constants.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28
29 #include "svga_screen.h"
30 #include "svga_context.h"
31 #include "svga_state.h"
32 #include "svga_cmd.h"
33 #include "svga_tgsi.h"
34 #include "svga_debug.h"
35
36 #include "svga_hw_reg.h"
37
38
39 /*
40 * Don't try to send more than 4k of successive constants.
41 */
42 #define MAX_CONST_REG_COUNT 256 /* 4k */
43
44
45 /***********************************************************************
46 * Hardware update
47 */
48
49 /* Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
50 */
51 static int svga_shader_type( unsigned shader )
52 {
53 assert(PIPE_SHADER_VERTEX + 1 == SVGA3D_SHADERTYPE_VS);
54 assert(PIPE_SHADER_FRAGMENT + 1 == SVGA3D_SHADERTYPE_PS);
55 assert(shader <= PIPE_SHADER_FRAGMENT);
56 return shader + 1;
57 }
58
59 /*
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
64 */
65 static enum pipe_error
66 emit_const(struct svga_context *svga, unsigned shader, unsigned i,
67 const float *value)
68 {
69 enum pipe_error ret = PIPE_OK;
70
71 assert(shader < PIPE_SHADER_TYPES);
72
73 assert(i < SVGA3D_CONSTREG_MAX);
74
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",
78 __FUNCTION__,
79 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
80 i,
81 value[0],
82 value[1],
83 value[2],
84 value[3]);
85
86 ret = SVGA3D_SetShaderConst( svga->swc,
87 i,
88 svga_shader_type(shader),
89 SVGA3D_CONST_TYPE_FLOAT,
90 value );
91 if (ret != PIPE_OK)
92 return ret;
93
94 memcpy(svga->state.hw_draw.cb[shader][i], value, 4 * sizeof(float));
95 }
96
97 return ret;
98 }
99
100 /*
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.
104 */
105 static enum pipe_error emit_const_range( struct svga_context *svga,
106 unsigned shader,
107 unsigned offset,
108 unsigned count,
109 const float (*values)[4] )
110 {
111 unsigned i, j;
112 enum pipe_error ret;
113
114 #ifdef DEBUG
115 if (offset + count > SVGA3D_CONSTREG_MAX) {
116 debug_printf("svga: too many constants (offset + count = %u)\n",
117 offset + count);
118 }
119 #endif
120
121 if (offset > SVGA3D_CONSTREG_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.
125 */
126 return PIPE_OK;
127 }
128
129 if (offset + count > SVGA3D_CONSTREG_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.
134 */
135 count = SVGA3D_CONSTREG_MAX - offset;
136 }
137
138 i = 0;
139 while (i < count) {
140 if (memcmp(svga->state.hw_draw.cb[shader][offset + i],
141 values[i],
142 4 * sizeof(float)) != 0) {
143
144 /*
145 * Found one dirty constant
146 */
147
148 if (SVGA_DEBUG & DEBUG_CONSTS)
149 debug_printf("%s %s %d: %f %f %f %f\n",
150 __FUNCTION__,
151 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
152 offset + i,
153 values[i][0],
154 values[i][1],
155 values[i][2],
156 values[i][3]);
157
158 /*
159 * Look for more consecutive dirty constants.
160 */
161
162 j = i + 1;
163 while (j < count &&
164 j < i + MAX_CONST_REG_COUNT &&
165 memcmp(svga->state.hw_draw.cb[shader][offset + j],
166 values[j],
167 4 * sizeof(float)) != 0) {
168
169 if (SVGA_DEBUG & DEBUG_CONSTS)
170 debug_printf("%s %s %d: %f %f %f %f\n",
171 __FUNCTION__,
172 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
173 offset + j,
174 values[j][0],
175 values[j][1],
176 values[j][2],
177 values[j][3]);
178
179 ++j;
180 }
181
182 assert(j >= i + 1);
183
184 /*
185 * Send them all together.
186 */
187
188 ret = SVGA3D_SetShaderConsts(svga->swc,
189 offset + i, j - i,
190 svga_shader_type(shader),
191 SVGA3D_CONST_TYPE_FLOAT,
192 values + i);
193 if (ret != PIPE_OK) {
194 return ret;
195 }
196
197 /*
198 * Local copy of the hardware state.
199 */
200
201 memcpy(svga->state.hw_draw.cb[shader][offset + i],
202 values[i],
203 (j - i) * 4 * sizeof(float));
204
205 i = j + 1;
206 } else {
207 ++i;
208 }
209 }
210
211 return PIPE_OK;
212 }
213
214
215 /**
216 * Emit all the constants in a constant buffer for a shader stage.
217 */
218 static enum pipe_error
219 emit_consts(struct svga_context *svga, unsigned shader)
220 {
221 struct svga_screen *ss = svga_screen(svga->pipe.screen);
222 struct pipe_transfer *transfer = NULL;
223 unsigned count;
224 const float (*data)[4] = NULL;
225 unsigned i;
226 enum pipe_error ret = PIPE_OK;
227 const unsigned offset = 0;
228
229 assert(shader < PIPE_SHADER_TYPES);
230
231 if (svga->curr.cb[shader] == NULL)
232 goto done;
233
234 count = svga->curr.cb[shader]->width0 / (4 * sizeof(float));
235
236 data = (const float (*)[4])pipe_buffer_map(&svga->pipe,
237 svga->curr.cb[shader],
238 PIPE_TRANSFER_READ,
239 &transfer);
240 if (data == NULL) {
241 ret = PIPE_ERROR_OUT_OF_MEMORY;
242 goto done;
243 }
244
245 if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) {
246 ret = emit_const_range( svga, shader, offset, count, data );
247 if (ret != PIPE_OK) {
248 goto done;
249 }
250 } else {
251 for (i = 0; i < count; i++) {
252 ret = emit_const( svga, shader, offset + i, data[i] );
253 if (ret != PIPE_OK) {
254 goto done;
255 }
256 }
257 }
258
259 done:
260 if (data)
261 pipe_buffer_unmap(&svga->pipe, transfer);
262
263 return ret;
264 }
265
266
267 static enum pipe_error
268 emit_fs_consts(struct svga_context *svga, unsigned dirty)
269 {
270 const struct svga_shader_result *result = svga->state.hw_draw.fs;
271 const struct svga_fs_compile_key *key = &result->key.fkey;
272 enum pipe_error ret = PIPE_OK;
273
274 ret = emit_consts( svga, PIPE_SHADER_FRAGMENT );
275 if (ret != PIPE_OK)
276 return ret;
277
278 /* The internally generated fragment shader for xor blending
279 * doesn't have a 'result' struct. It should be fixed to avoid
280 * this special case, but work around it with a NULL check:
281 */
282 if (result != NULL &&
283 key->num_unnormalized_coords)
284 {
285 unsigned offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
286 int i;
287
288 for (i = 0; i < key->num_textures; i++) {
289 if (key->tex[i].unnormalized) {
290 struct pipe_resource *tex = svga->curr.sampler_views[i]->texture;
291 float data[4];
292
293 data[0] = 1.0 / (float)tex->width0;
294 data[1] = 1.0 / (float)tex->height0;
295 data[2] = 1.0;
296 data[3] = 1.0;
297
298 ret = emit_const( svga,
299 PIPE_SHADER_FRAGMENT,
300 key->tex[i].width_height_idx + offset,
301 data );
302 if (ret != PIPE_OK)
303 return ret;
304 }
305 }
306
307 offset += key->num_unnormalized_coords;
308 }
309
310 return 0;
311 }
312
313
314 struct svga_tracked_state svga_hw_fs_parameters =
315 {
316 "hw fs params",
317 (SVGA_NEW_FS_CONST_BUFFER |
318 SVGA_NEW_FS_RESULT |
319 SVGA_NEW_TEXTURE_BINDING),
320 emit_fs_consts
321 };
322
323 /***********************************************************************
324 */
325
326 static enum pipe_error
327 emit_vs_consts(struct svga_context *svga, unsigned dirty)
328 {
329 const struct svga_shader_result *result = svga->state.hw_draw.vs;
330 const struct svga_vs_compile_key *key = &result->key.vkey;
331 enum pipe_error ret = PIPE_OK;
332 unsigned offset;
333
334 /* SVGA_NEW_VS_RESULT
335 */
336 if (result == NULL)
337 return PIPE_OK;
338
339 /* SVGA_NEW_VS_CONST_BUFFER
340 */
341 ret = emit_consts( svga, PIPE_SHADER_VERTEX );
342 if (ret != PIPE_OK)
343 return ret;
344
345 offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
346
347 /* SVGA_NEW_VS_RESULT
348 */
349 if (key->need_prescale) {
350 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
351 svga->state.hw_clear.prescale.scale );
352 if (ret != PIPE_OK)
353 return ret;
354
355 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
356 svga->state.hw_clear.prescale.translate );
357 if (ret != PIPE_OK)
358 return ret;
359 }
360
361 /* SVGA_NEW_ZERO_STRIDE
362 */
363 if (key->zero_stride_vertex_elements) {
364 unsigned i, curr_zero_stride = 0;
365 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) {
366 if (key->zero_stride_vertex_elements & (1 << i)) {
367 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
368 svga->curr.zero_stride_constants +
369 4 * curr_zero_stride );
370 if (ret != PIPE_OK)
371 return ret;
372 ++curr_zero_stride;
373 }
374 }
375 }
376
377 return PIPE_OK;
378 }
379
380
381 struct svga_tracked_state svga_hw_vs_parameters =
382 {
383 "hw vs params",
384 (SVGA_NEW_PRESCALE |
385 SVGA_NEW_VS_CONST_BUFFER |
386 SVGA_NEW_ZERO_STRIDE |
387 SVGA_NEW_VS_RESULT),
388 emit_vs_consts
389 };
390