Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5564 | serge | 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 "util/u_memory.h" |
||
28 | #include "pipe/p_defines.h" |
||
29 | |||
30 | #include "svga_screen.h" |
||
31 | #include "svga_context.h" |
||
32 | #include "svga_state.h" |
||
33 | #include "svga_cmd.h" |
||
34 | #include "svga_tgsi.h" |
||
35 | #include "svga_debug.h" |
||
36 | #include "svga_resource_buffer.h" |
||
37 | |||
38 | #include "svga_hw_reg.h" |
||
39 | |||
40 | |||
41 | /* |
||
42 | * Don't try to send more than 4kb of successive constants. |
||
43 | */ |
||
44 | #define MAX_CONST_REG_COUNT 256 /**< number of float[4] constants */ |
||
45 | |||
46 | /** |
||
47 | * Extra space for svga-specific VS/PS constants (such as texcoord |
||
48 | * scale factors, vertex transformation scale/translation). |
||
49 | */ |
||
50 | #define MAX_EXTRA_CONSTS 32 |
||
51 | |||
52 | /** Guest-backed surface constant buffers must be this size */ |
||
53 | #define GB_CONSTBUF_SIZE (SVGA3D_CONSTREG_MAX) |
||
54 | |||
55 | /** |
||
56 | * Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_* |
||
57 | */ |
||
58 | static unsigned |
||
59 | svga_shader_type(unsigned shader) |
||
60 | { |
||
61 | switch (shader) { |
||
62 | case PIPE_SHADER_VERTEX: |
||
63 | return SVGA3D_SHADERTYPE_VS; |
||
64 | case PIPE_SHADER_FRAGMENT: |
||
65 | return SVGA3D_SHADERTYPE_PS; |
||
66 | default: |
||
67 | assert(!"Unexpected shader type"); |
||
68 | return SVGA3D_SHADERTYPE_VS; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Emit any extra fragment shader constants into the buffer pointed |
||
75 | * to by 'dest'. |
||
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 |
||
79 | */ |
||
80 | static unsigned |
||
81 | svga_get_extra_fs_constants(struct svga_context *svga, float *dest) |
||
82 | { |
||
83 | const struct svga_shader_variant *variant = svga->state.hw_draw.fs; |
||
84 | const struct svga_fs_compile_key *key = &variant->key.fkey; |
||
85 | unsigned count = 0; |
||
86 | |||
87 | /* SVGA_NEW_VS_VARIANT |
||
88 | */ |
||
89 | if (key->num_unnormalized_coords) { |
||
90 | unsigned i; |
||
91 | |||
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; |
||
95 | |||
96 | /* debug/sanity check */ |
||
97 | assert(key->tex[i].width_height_idx == count); |
||
98 | |||
99 | *dest++ = 1.0 / (float)tex->width0; |
||
100 | *dest++ = 1.0 / (float)tex->height0; |
||
101 | *dest++ = 1.0; |
||
102 | *dest++ = 1.0; |
||
103 | |||
104 | count++; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | assert(count <= MAX_EXTRA_CONSTS); |
||
110 | |||
111 | return count; |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Emit any extra vertex shader constants into the buffer pointed |
||
117 | * to by 'dest'. |
||
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 |
||
122 | */ |
||
123 | static unsigned |
||
124 | svga_get_extra_vs_constants(struct svga_context *svga, float *dest) |
||
125 | { |
||
126 | const struct svga_shader_variant *variant = svga->state.hw_draw.vs; |
||
127 | const struct svga_vs_compile_key *key = &variant->key.vkey; |
||
128 | unsigned count = 0; |
||
129 | |||
130 | /* SVGA_NEW_VS_VARIANT |
||
131 | */ |
||
132 | if (key->need_prescale) { |
||
133 | memcpy(dest, svga->state.hw_clear.prescale.scale, 4 * sizeof(float)); |
||
134 | dest += 4; |
||
135 | |||
136 | memcpy(dest, svga->state.hw_clear.prescale.translate, 4 * sizeof(float)); |
||
137 | dest += 4; |
||
138 | |||
139 | count = 2; |
||
140 | } |
||
141 | |||
142 | assert(count <= MAX_EXTRA_CONSTS); |
||
143 | |||
144 | return count; |
||
145 | } |
||
146 | |||
147 | |||
148 | /** |
||
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 |
||
153 | */ |
||
154 | static enum pipe_error |
||
155 | emit_const(struct svga_context *svga, unsigned shader, unsigned i, |
||
156 | const float *value) |
||
157 | { |
||
158 | enum pipe_error ret = PIPE_OK; |
||
159 | |||
160 | assert(shader < PIPE_SHADER_TYPES); |
||
161 | assert(i < SVGA3D_CONSTREG_MAX); |
||
162 | |||
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", |
||
167 | __FUNCTION__, |
||
168 | shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG", |
||
169 | i, |
||
170 | value[0], |
||
171 | value[1], |
||
172 | value[2], |
||
173 | value[3]); |
||
174 | |||
175 | ret = SVGA3D_SetShaderConst( svga->swc, |
||
176 | i, |
||
177 | svga_shader_type(shader), |
||
178 | SVGA3D_CONST_TYPE_FLOAT, |
||
179 | value ); |
||
180 | if (ret != PIPE_OK) |
||
181 | return ret; |
||
182 | |||
183 | memcpy(svga->state.hw_draw.cb[shader][i], value, 4 * sizeof(float)); |
||
184 | } |
||
185 | |||
186 | return ret; |
||
187 | } |
||
188 | |||
189 | |||
190 | /* |
||
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. |
||
194 | */ |
||
195 | static enum pipe_error |
||
196 | emit_const_range(struct svga_context *svga, |
||
197 | unsigned shader, |
||
198 | unsigned offset, |
||
199 | unsigned count, |
||
200 | const float (*values)[4]) |
||
201 | { |
||
202 | unsigned i, j; |
||
203 | enum pipe_error ret; |
||
204 | |||
205 | #ifdef DEBUG |
||
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); |
||
209 | } |
||
210 | #endif |
||
211 | |||
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. |
||
216 | */ |
||
217 | return PIPE_OK; |
||
218 | } |
||
219 | |||
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. |
||
225 | */ |
||
226 | count = SVGA3D_CONSTREG_MAX - offset; |
||
227 | } |
||
228 | |||
229 | i = 0; |
||
230 | while (i < count) { |
||
231 | if (memcmp(svga->state.hw_draw.cb[shader][offset + i], |
||
232 | values[i], |
||
233 | 4 * sizeof(float)) != 0) { |
||
234 | /* Found one dirty constant |
||
235 | */ |
||
236 | if (SVGA_DEBUG & DEBUG_CONSTS) |
||
237 | debug_printf("%s %s %d: %f %f %f %f\n", |
||
238 | __FUNCTION__, |
||
239 | shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG", |
||
240 | offset + i, |
||
241 | values[i][0], |
||
242 | values[i][1], |
||
243 | values[i][2], |
||
244 | values[i][3]); |
||
245 | |||
246 | /* Look for more consecutive dirty constants. |
||
247 | */ |
||
248 | j = i + 1; |
||
249 | while (j < count && |
||
250 | j < i + MAX_CONST_REG_COUNT && |
||
251 | memcmp(svga->state.hw_draw.cb[shader][offset + j], |
||
252 | values[j], |
||
253 | 4 * sizeof(float)) != 0) { |
||
254 | |||
255 | if (SVGA_DEBUG & DEBUG_CONSTS) |
||
256 | debug_printf("%s %s %d: %f %f %f %f\n", |
||
257 | __FUNCTION__, |
||
258 | shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG", |
||
259 | offset + j, |
||
260 | values[j][0], |
||
261 | values[j][1], |
||
262 | values[j][2], |
||
263 | values[j][3]); |
||
264 | |||
265 | ++j; |
||
266 | } |
||
267 | |||
268 | assert(j >= i + 1); |
||
269 | |||
270 | /* Send them all together. |
||
271 | */ |
||
272 | if (svga_have_gb_objects(svga)) { |
||
273 | ret = SVGA3D_SetGBShaderConstsInline(svga->swc, |
||
274 | offset + i, /* start */ |
||
275 | j - i, /* count */ |
||
276 | svga_shader_type(shader), |
||
277 | SVGA3D_CONST_TYPE_FLOAT, |
||
278 | values + i); |
||
279 | } |
||
280 | else { |
||
281 | ret = SVGA3D_SetShaderConsts(svga->swc, |
||
282 | offset + i, j - i, |
||
283 | svga_shader_type(shader), |
||
284 | SVGA3D_CONST_TYPE_FLOAT, |
||
285 | values + i); |
||
286 | } |
||
287 | if (ret != PIPE_OK) { |
||
288 | return ret; |
||
289 | } |
||
290 | |||
291 | /* |
||
292 | * Local copy of the hardware state. |
||
293 | */ |
||
294 | memcpy(svga->state.hw_draw.cb[shader][offset + i], |
||
295 | values[i], |
||
296 | (j - i) * 4 * sizeof(float)); |
||
297 | |||
298 | i = j + 1; |
||
299 | } else { |
||
300 | ++i; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return PIPE_OK; |
||
305 | } |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Emit all the constants in a constant buffer for a shader stage. |
||
310 | */ |
||
311 | static enum pipe_error |
||
312 | emit_consts(struct svga_context *svga, unsigned shader) |
||
313 | { |
||
314 | struct svga_screen *ss = svga_screen(svga->pipe.screen); |
||
315 | struct pipe_transfer *transfer = NULL; |
||
316 | unsigned count; |
||
317 | const float (*data)[4] = NULL; |
||
318 | unsigned i; |
||
319 | enum pipe_error ret = PIPE_OK; |
||
320 | const unsigned offset = 0; |
||
321 | |||
322 | assert(shader < PIPE_SHADER_TYPES); |
||
323 | |||
324 | if (svga->curr.cbufs[shader].buffer == NULL) |
||
325 | goto done; |
||
326 | |||
327 | data = (const float (*)[4])pipe_buffer_map(&svga->pipe, |
||
328 | svga->curr.cbufs[shader].buffer, |
||
329 | PIPE_TRANSFER_READ, |
||
330 | &transfer); |
||
331 | if (data == NULL) { |
||
332 | ret = PIPE_ERROR_OUT_OF_MEMORY; |
||
333 | goto done; |
||
334 | } |
||
335 | |||
336 | /* sanity check */ |
||
337 | assert(svga->curr.cbufs[shader].buffer->width0 >= |
||
338 | svga->curr.cbufs[shader].buffer_size); |
||
339 | |||
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)); |
||
343 | |||
344 | if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) { |
||
345 | ret = emit_const_range( svga, shader, offset, count, data ); |
||
346 | if (ret != PIPE_OK) { |
||
347 | goto done; |
||
348 | } |
||
349 | } else { |
||
350 | for (i = 0; i < count; i++) { |
||
351 | ret = emit_const( svga, shader, offset + i, data[i] ); |
||
352 | if (ret != PIPE_OK) { |
||
353 | goto done; |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | |||
358 | done: |
||
359 | if (data) |
||
360 | pipe_buffer_unmap(&svga->pipe, transfer); |
||
361 | |||
362 | return ret; |
||
363 | } |
||
364 | |||
365 | |||
366 | static enum pipe_error |
||
367 | emit_fs_consts(struct svga_context *svga, unsigned dirty) |
||
368 | { |
||
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; |
||
372 | |||
373 | /* SVGA_NEW_FS_VARIANT |
||
374 | */ |
||
375 | if (variant == NULL) |
||
376 | return PIPE_OK; |
||
377 | |||
378 | /* SVGA_NEW_FS_CONST_BUFFER |
||
379 | */ |
||
380 | ret = emit_consts( svga, PIPE_SHADER_FRAGMENT ); |
||
381 | if (ret != PIPE_OK) |
||
382 | return ret; |
||
383 | |||
384 | /* emit extra shader constants */ |
||
385 | { |
||
386 | unsigned offset = variant->shader->info.file_max[TGSI_FILE_CONSTANT] + 1; |
||
387 | float extras[MAX_EXTRA_CONSTS][4]; |
||
388 | unsigned count, i; |
||
389 | |||
390 | count = svga_get_extra_fs_constants(svga, (float *) extras); |
||
391 | |||
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); |
||
395 | } else { |
||
396 | for (i = 0; i < count; i++) { |
||
397 | ret = emit_const(svga, PIPE_SHADER_FRAGMENT, offset + i, extras[i]); |
||
398 | if (ret != PIPE_OK) |
||
399 | return ret; |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | |||
404 | return ret; |
||
405 | } |
||
406 | |||
407 | |||
408 | struct svga_tracked_state svga_hw_fs_constants = |
||
409 | { |
||
410 | "hw fs params", |
||
411 | (SVGA_NEW_FS_CONST_BUFFER | |
||
412 | SVGA_NEW_FS_VARIANT | |
||
413 | SVGA_NEW_TEXTURE_BINDING), |
||
414 | emit_fs_consts |
||
415 | }; |
||
416 | |||
417 | |||
418 | |||
419 | static enum pipe_error |
||
420 | emit_vs_consts(struct svga_context *svga, unsigned dirty) |
||
421 | { |
||
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; |
||
425 | |||
426 | /* SVGA_NEW_VS_VARIANT |
||
427 | */ |
||
428 | if (variant == NULL) |
||
429 | return PIPE_OK; |
||
430 | |||
431 | /* SVGA_NEW_VS_CONST_BUFFER |
||
432 | */ |
||
433 | ret = emit_consts( svga, PIPE_SHADER_VERTEX ); |
||
434 | if (ret != PIPE_OK) |
||
435 | return ret; |
||
436 | |||
437 | /* emit extra shader constants */ |
||
438 | { |
||
439 | unsigned offset = variant->shader->info.file_max[TGSI_FILE_CONSTANT] + 1; |
||
440 | float extras[MAX_EXTRA_CONSTS][4]; |
||
441 | unsigned count, i; |
||
442 | |||
443 | count = svga_get_extra_vs_constants(svga, (float *) extras); |
||
444 | assert(count <= Elements(extras)); |
||
445 | |||
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); |
||
449 | } else { |
||
450 | for (i = 0; i < count; i++) { |
||
451 | ret = emit_const(svga, PIPE_SHADER_VERTEX, offset + i, extras[i]); |
||
452 | if (ret != PIPE_OK) |
||
453 | return ret; |
||
454 | } |
||
455 | } |
||
456 | } |
||
457 | |||
458 | return ret; |
||
459 | } |
||
460 | |||
461 | |||
462 | struct svga_tracked_state svga_hw_vs_constants = |
||
463 | { |
||
464 | "hw vs params", |
||
465 | (SVGA_NEW_PRESCALE | |
||
466 | SVGA_NEW_VS_CONST_BUFFER | |
||
467 | SVGA_NEW_VS_VARIANT), |
||
468 | emit_vs_consts |
||
469 | };>=>>>>>>>>>=>=>>> |