2cf41134bd6c328f3b7699891db1ac9c7057a751
[mesa.git] / src / gallium / drivers / svga / svga_state_constants.c
1
2 /**********************************************************
3 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 **********************************************************/
26
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_defines.h"
31 #include "util/u_upload_mgr.h"
32
33 #include "svga_screen.h"
34 #include "svga_context.h"
35 #include "svga_state.h"
36 #include "svga_cmd.h"
37 #include "svga_tgsi.h"
38 #include "svga_debug.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_shader.h"
41
42 #include "svga_hw_reg.h"
43
44
45 /*
46 * Don't try to send more than 4kb of successive constants.
47 */
48 #define MAX_CONST_REG_COUNT 256 /**< number of float[4] constants */
49
50 /**
51 * Extra space for svga-specific VS/PS constants (such as texcoord
52 * scale factors, vertex transformation scale/translation).
53 */
54 #define MAX_EXTRA_CONSTS 32
55
56 /** Guest-backed surface constant buffers must be this size */
57 #define GB_CONSTBUF_SIZE (SVGA3D_CONSTREG_MAX)
58
59
60 /**
61 * Emit any extra shader-type-independent shader constants into the buffer
62 * pointed to by 'dest'.
63 * \return number of float[4] constants put into the 'dest' buffer
64 */
65 static unsigned
66 svga_get_extra_constants_common(struct svga_context *svga,
67 const struct svga_shader_variant *variant,
68 unsigned shader, float *dest)
69 {
70 uint32_t *dest_u = (uint32_t *) dest; // uint version of dest
71 unsigned i;
72 unsigned count = 0;
73
74 for (i = 0; i < variant->key.num_textures; i++) {
75 struct pipe_sampler_view *sv = svga->curr.sampler_views[shader][i];
76 if (sv) {
77 struct pipe_resource *tex = sv->texture;
78 /* Scaling factors needed for handling unnormalized texture coordinates
79 * for texture rectangles.
80 */
81 if (variant->key.tex[i].unnormalized) {
82 /* debug/sanity check */
83 assert(variant->key.tex[i].width_height_idx == count);
84
85 *dest++ = 1.0 / (float)tex->width0;
86 *dest++ = 1.0 / (float)tex->height0;
87 *dest++ = 1.0;
88 *dest++ = 1.0;
89
90 count++;
91 }
92
93 /* Store the sizes for texture buffers.
94 */
95 if (tex->target == PIPE_BUFFER) {
96 unsigned bytes_per_element = util_format_get_blocksize(sv->format);
97 *dest_u++ = tex->width0 / bytes_per_element;
98 *dest_u++ = 1;
99 *dest_u++ = 1;
100 *dest_u++ = 1;
101
102 count++;
103 }
104 }
105 }
106
107 return count;
108 }
109
110
111 /**
112 * Emit any extra fragment shader constants into the buffer pointed
113 * to by 'dest'.
114 * \return number of float[4] constants put into the dest buffer
115 */
116 static unsigned
117 svga_get_extra_fs_constants(struct svga_context *svga, float *dest)
118 {
119 const struct svga_shader_variant *variant = svga->state.hw_draw.fs;
120 unsigned count = 0;
121
122 count += svga_get_extra_constants_common(svga, variant,
123 PIPE_SHADER_FRAGMENT, dest);
124
125 assert(count <= MAX_EXTRA_CONSTS);
126
127 return count;
128 }
129
130 /**
131 * Emit extra constants needed for prescale computation into the
132 * the buffer pointed to by '*dest'. The updated buffer pointer
133 * will be returned in 'dest'.
134 */
135 static unsigned
136 svga_get_prescale_constants(struct svga_context *svga, float **dest)
137 {
138 memcpy(*dest, svga->state.hw_clear.prescale.scale, 4 * sizeof(float));
139 *dest += 4;
140
141 memcpy(*dest, svga->state.hw_clear.prescale.translate, 4 * sizeof(float));
142 *dest += 4;
143
144 return 2;
145 }
146
147 /**
148 * Emit extra constants needed for point sprite emulation.
149 */
150 static unsigned
151 svga_get_pt_sprite_constants(struct svga_context *svga, float **dest)
152 {
153 struct svga_screen *screen = svga_screen(svga->pipe.screen);
154 float *dst = *dest;
155
156 dst[0] = 1.0 / (svga->curr.viewport.scale[0] * 2);
157 dst[1] = 1.0 / (svga->curr.viewport.scale[1] * 2);
158 dst[2] = svga->curr.rast->pointsize;
159 dst[3] = screen->maxPointSize;
160 *dest = *dest + 4;
161 return 1;
162 }
163
164 /**
165 * Emit user-defined clip plane coefficients into the buffer pointed to
166 * by '*dest'. The updated buffer pointer will be returned in 'dest'.
167 */
168 static unsigned
169 svga_get_clip_plane_constants(struct svga_context *svga,
170 const struct svga_shader_variant *variant,
171 float **dest)
172 {
173 unsigned count = 0;
174
175 /* SVGA_NEW_CLIP */
176 if (svga_have_vgpu10(svga)) {
177 /* append user-defined clip plane coefficients onto constant buffer */
178 unsigned clip_planes = variant->key.clip_plane_enable;
179 while (clip_planes) {
180 int i = u_bit_scan(&clip_planes);
181 COPY_4V(*dest, svga->curr.clip.ucp[i]);
182 *dest += 4;
183 count += 1;
184 }
185 }
186 return count;
187 }
188
189 /**
190 * Emit any extra vertex shader constants into the buffer pointed
191 * to by 'dest'.
192 * In particular, these would be the scale and bias factors computed
193 * from the framebuffer size which are used to copy with differences in
194 * GL vs D3D coordinate spaces. See svga_tgsi_insn.c for more info.
195 * \return number of float[4] constants put into the dest buffer
196 */
197 static unsigned
198 svga_get_extra_vs_constants(struct svga_context *svga, float *dest)
199 {
200 const struct svga_shader_variant *variant = svga->state.hw_draw.vs;
201 unsigned count = 0;
202
203 /* SVGA_NEW_VS_VARIANT
204 */
205 if (variant->key.vs.need_prescale) {
206 count += svga_get_prescale_constants(svga, &dest);
207 }
208
209 if (variant->key.vs.undo_viewport) {
210 /* Used to convert window coords back to NDC coords */
211 dest[0] = 1.0f / svga->curr.viewport.scale[0];
212 dest[1] = 1.0f / svga->curr.viewport.scale[1];
213 dest[2] = -svga->curr.viewport.translate[0];
214 dest[3] = -svga->curr.viewport.translate[1];
215 dest += 4;
216 count += 1;
217 }
218
219 /* SVGA_NEW_CLIP */
220 count += svga_get_clip_plane_constants(svga, variant, &dest);
221
222 /* common constants */
223 count += svga_get_extra_constants_common(svga, variant,
224 PIPE_SHADER_VERTEX, dest);
225
226 assert(count <= MAX_EXTRA_CONSTS);
227
228 return count;
229 }
230
231 /**
232 * Emit any extra geometry shader constants into the buffer pointed
233 * to by 'dest'.
234 */
235 static unsigned
236 svga_get_extra_gs_constants(struct svga_context *svga, float *dest)
237 {
238 const struct svga_shader_variant *variant = svga->state.hw_draw.gs;
239 unsigned count = 0;
240
241 /* SVGA_NEW_GS_VARIANT
242 */
243
244 /* Constants for point sprite
245 * These are used in the transformed gs that supports point sprite.
246 * They need to be added before the prescale constants.
247 */
248 if (variant->key.gs.wide_point) {
249 count += svga_get_pt_sprite_constants(svga, &dest);
250 }
251
252 if (variant->key.gs.need_prescale) {
253 count += svga_get_prescale_constants(svga, &dest);
254 }
255
256 /* SVGA_NEW_CLIP */
257 count += svga_get_clip_plane_constants(svga, variant, &dest);
258
259 /* common constants */
260 count += svga_get_extra_constants_common(svga, variant,
261 PIPE_SHADER_GEOMETRY, dest);
262
263 assert(count <= MAX_EXTRA_CONSTS);
264 return count;
265 }
266
267 /**
268 * Check and emit one shader constant register.
269 * \param shader PIPE_SHADER_FRAGMENT or PIPE_SHADER_VERTEX
270 * \param i which float[4] constant to change
271 * \param value the new float[4] value
272 */
273 static enum pipe_error
274 emit_const(struct svga_context *svga, unsigned shader, unsigned i,
275 const float *value)
276 {
277 enum pipe_error ret = PIPE_OK;
278
279 assert(shader < PIPE_SHADER_TYPES);
280 assert(i < SVGA3D_CONSTREG_MAX);
281 assert(!svga_have_vgpu10(svga));
282
283 if (memcmp(svga->state.hw_draw.cb[shader][i], value,
284 4 * sizeof(float)) != 0) {
285 if (SVGA_DEBUG & DEBUG_CONSTS)
286 debug_printf("%s %s %u: %f %f %f %f\n",
287 __FUNCTION__,
288 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
289 i,
290 value[0],
291 value[1],
292 value[2],
293 value[3]);
294
295 ret = SVGA3D_SetShaderConst( svga->swc,
296 i,
297 svga_shader_type(shader),
298 SVGA3D_CONST_TYPE_FLOAT,
299 value );
300 if (ret != PIPE_OK)
301 return ret;
302
303 memcpy(svga->state.hw_draw.cb[shader][i], value, 4 * sizeof(float));
304 }
305
306 return ret;
307 }
308
309
310 /*
311 * Check and emit a range of shader constant registers, trying to coalesce
312 * successive shader constant updates in a single command in order to save
313 * space on the command buffer. This is a HWv8 feature.
314 */
315 static enum pipe_error
316 emit_const_range(struct svga_context *svga,
317 unsigned shader,
318 unsigned offset,
319 unsigned count,
320 const float (*values)[4])
321 {
322 unsigned i, j;
323 enum pipe_error ret;
324
325 assert(shader == PIPE_SHADER_VERTEX ||
326 shader == PIPE_SHADER_FRAGMENT);
327 assert(!svga_have_vgpu10(svga));
328
329 #ifdef DEBUG
330 if (offset + count > SVGA3D_CONSTREG_MAX) {
331 debug_printf("svga: too many constants (offset %u + count %u = %u (max = %u))\n",
332 offset, count, offset + count, SVGA3D_CONSTREG_MAX);
333 }
334 #endif
335
336 if (offset > SVGA3D_CONSTREG_MAX) {
337 /* This isn't OK, but if we propagate an error all the way up we'll
338 * just get into more trouble.
339 * XXX note that offset is always zero at this time so this is moot.
340 */
341 return PIPE_OK;
342 }
343
344 if (offset + count > SVGA3D_CONSTREG_MAX) {
345 /* Just drop the extra constants for now.
346 * Ideally we should not have allowed the app to create a shader
347 * that exceeds our constant buffer size but there's no way to
348 * express that in gallium at this time.
349 */
350 count = SVGA3D_CONSTREG_MAX - offset;
351 }
352
353 i = 0;
354 while (i < count) {
355 if (memcmp(svga->state.hw_draw.cb[shader][offset + i],
356 values[i],
357 4 * sizeof(float)) != 0) {
358 /* Found one dirty constant
359 */
360 if (SVGA_DEBUG & DEBUG_CONSTS)
361 debug_printf("%s %s %d: %f %f %f %f\n",
362 __FUNCTION__,
363 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
364 offset + i,
365 values[i][0],
366 values[i][1],
367 values[i][2],
368 values[i][3]);
369
370 /* Look for more consecutive dirty constants.
371 */
372 j = i + 1;
373 while (j < count &&
374 j < i + MAX_CONST_REG_COUNT &&
375 memcmp(svga->state.hw_draw.cb[shader][offset + j],
376 values[j],
377 4 * sizeof(float)) != 0) {
378
379 if (SVGA_DEBUG & DEBUG_CONSTS)
380 debug_printf("%s %s %d: %f %f %f %f\n",
381 __FUNCTION__,
382 shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
383 offset + j,
384 values[j][0],
385 values[j][1],
386 values[j][2],
387 values[j][3]);
388
389 ++j;
390 }
391
392 assert(j >= i + 1);
393
394 /* Send them all together.
395 */
396 if (svga_have_gb_objects(svga)) {
397 ret = SVGA3D_SetGBShaderConstsInline(svga->swc,
398 offset + i, /* start */
399 j - i, /* count */
400 svga_shader_type(shader),
401 SVGA3D_CONST_TYPE_FLOAT,
402 values + i);
403 }
404 else {
405 ret = SVGA3D_SetShaderConsts(svga->swc,
406 offset + i, j - i,
407 svga_shader_type(shader),
408 SVGA3D_CONST_TYPE_FLOAT,
409 values + i);
410 }
411 if (ret != PIPE_OK) {
412 return ret;
413 }
414
415 /*
416 * Local copy of the hardware state.
417 */
418 memcpy(svga->state.hw_draw.cb[shader][offset + i],
419 values[i],
420 (j - i) * 4 * sizeof(float));
421
422 i = j + 1;
423 } else {
424 ++i;
425 }
426 }
427
428 return PIPE_OK;
429 }
430
431
432 /**
433 * Emit all the constants in a constant buffer for a shader stage.
434 * On VGPU10, emit_consts_vgpu10 is used instead.
435 */
436 static enum pipe_error
437 emit_consts_vgpu9(struct svga_context *svga, unsigned shader)
438 {
439 const struct pipe_constant_buffer *cbuf;
440 struct svga_screen *ss = svga_screen(svga->pipe.screen);
441 struct pipe_transfer *transfer = NULL;
442 unsigned count;
443 const float (*data)[4] = NULL;
444 unsigned i;
445 enum pipe_error ret = PIPE_OK;
446 const unsigned offset = 0;
447
448 assert(shader < PIPE_SHADER_TYPES);
449 assert(!svga_have_vgpu10(svga));
450 /* Only one constant buffer per shader is supported before VGPU10.
451 * This is only an approximate check against that.
452 */
453 assert(svga->curr.constbufs[shader][1].buffer == NULL);
454
455 cbuf = &svga->curr.constbufs[shader][0];
456
457 if (svga->curr.constbufs[shader][0].buffer) {
458 /* emit user-provided constants */
459 data = (const float (*)[4])
460 pipe_buffer_map(&svga->pipe, svga->curr.constbufs[shader][0].buffer,
461 PIPE_TRANSFER_READ, &transfer);
462 if (!data) {
463 return PIPE_ERROR_OUT_OF_MEMORY;
464 }
465
466 /* sanity check */
467 assert(cbuf->buffer->width0 >=
468 cbuf->buffer_size);
469
470 /* Use/apply the constant buffer size and offsets here */
471 count = cbuf->buffer_size / (4 * sizeof(float));
472 data += cbuf->buffer_offset / (4 * sizeof(float));
473
474 if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) {
475 ret = emit_const_range( svga, shader, offset, count, data );
476 }
477 else {
478 for (i = 0; i < count; i++) {
479 ret = emit_const( svga, shader, offset + i, data[i] );
480 if (ret != PIPE_OK) {
481 break;
482 }
483 }
484 }
485
486 pipe_buffer_unmap(&svga->pipe, transfer);
487
488 if (ret != PIPE_OK) {
489 return ret;
490 }
491 }
492
493 /* emit extra shader constants */
494 {
495 const struct svga_shader_variant *variant = NULL;
496 unsigned offset;
497 float extras[MAX_EXTRA_CONSTS][4];
498 unsigned count, i;
499
500 switch (shader) {
501 case PIPE_SHADER_VERTEX:
502 variant = svga->state.hw_draw.vs;
503 count = svga_get_extra_vs_constants(svga, (float *) extras);
504 break;
505 case PIPE_SHADER_FRAGMENT:
506 variant = svga->state.hw_draw.fs;
507 count = svga_get_extra_fs_constants(svga, (float *) extras);
508 break;
509 default:
510 assert(!"Unexpected shader type");
511 count = 0;
512 }
513
514 assert(variant);
515 offset = variant->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
516 assert(count <= Elements(extras));
517
518 if (count > 0) {
519 if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) {
520 ret = emit_const_range(svga, shader, offset, count,
521 (const float (*) [4])extras);
522 }
523 else {
524 for (i = 0; i < count; i++) {
525 ret = emit_const(svga, shader, offset + i, extras[i]);
526 if (ret != PIPE_OK)
527 return ret;
528 }
529 }
530 }
531 }
532
533 return ret;
534 }
535
536
537
538 static enum pipe_error
539 emit_constbuf_vgpu10(struct svga_context *svga, unsigned shader)
540 {
541 const struct pipe_constant_buffer *cbuf;
542 struct pipe_resource *dst_buffer = NULL;
543 enum pipe_error ret = PIPE_OK;
544 struct pipe_transfer *src_transfer;
545 struct svga_winsys_surface *dst_handle;
546 float extras[MAX_EXTRA_CONSTS][4];
547 unsigned extra_count, extra_size, extra_offset;
548 unsigned new_buf_size;
549 void *src_map = NULL, *dst_map;
550 unsigned offset;
551 const struct svga_shader_variant *variant;
552
553 assert(shader == PIPE_SHADER_VERTEX ||
554 shader == PIPE_SHADER_GEOMETRY ||
555 shader == PIPE_SHADER_FRAGMENT);
556
557 cbuf = &svga->curr.constbufs[shader][0];
558
559 switch (shader) {
560 case PIPE_SHADER_VERTEX:
561 variant = svga->state.hw_draw.vs;
562 extra_count = svga_get_extra_vs_constants(svga, (float *) extras);
563 break;
564 case PIPE_SHADER_FRAGMENT:
565 variant = svga->state.hw_draw.fs;
566 extra_count = svga_get_extra_fs_constants(svga, (float *) extras);
567 break;
568 case PIPE_SHADER_GEOMETRY:
569 variant = svga->state.hw_draw.gs;
570 extra_count = svga_get_extra_gs_constants(svga, (float *) extras);
571 break;
572 default:
573 assert(!"Unexpected shader type");
574 /* Don't return an error code since we don't want to keep re-trying
575 * this function and getting stuck in an infinite loop.
576 */
577 return PIPE_OK;
578 }
579
580 assert(variant);
581
582 /* Compute extra constants size and offset in bytes */
583 extra_size = extra_count * 4 * sizeof(float);
584 extra_offset = 4 * sizeof(float) * variant->extra_const_start;
585
586 if (cbuf->buffer_size + extra_size == 0)
587 return PIPE_OK; /* nothing to do */
588
589 /* Typically, the cbuf->buffer here is a user-space buffer so mapping
590 * it is really cheap. If we ever get real HW buffers for constants
591 * we should void mapping and instead use a ResourceCopy command.
592 */
593 if (cbuf->buffer_size > 0) {
594 src_map = pipe_buffer_map_range(&svga->pipe, cbuf->buffer,
595 cbuf->buffer_offset, cbuf->buffer_size,
596 PIPE_TRANSFER_READ, &src_transfer);
597 assert(src_map);
598 if (!src_map) {
599 return PIPE_ERROR_OUT_OF_MEMORY;
600 }
601 }
602
603 /* The new/dest buffer's size must be large enough to hold the original,
604 * user-specified constants, plus the extra constants.
605 * The size of the original constant buffer _should_ agree with what the
606 * shader is expecting, but it might not (it's not enforced anywhere by
607 * gallium).
608 */
609 new_buf_size = MAX2(cbuf->buffer_size, extra_offset) + extra_size;
610
611 /* According to the DX10 spec, the constant buffer size must be
612 * in multiples of 16.
613 */
614 new_buf_size = align(new_buf_size, 16);
615
616 u_upload_alloc(svga->const0_upload, 0, new_buf_size, &offset,
617 &dst_buffer, &dst_map);
618 if (!dst_map) {
619 if (src_map)
620 pipe_buffer_unmap(&svga->pipe, src_transfer);
621 return PIPE_ERROR_OUT_OF_MEMORY;
622 }
623
624 if (src_map) {
625 memcpy(dst_map, src_map, cbuf->buffer_size);
626 pipe_buffer_unmap(&svga->pipe, src_transfer);
627 }
628
629 if (extra_size) {
630 assert(extra_offset + extra_size <= new_buf_size);
631 memcpy((char *) dst_map + extra_offset, extras, extra_size);
632 }
633 u_upload_unmap(svga->const0_upload);
634
635 /* Issue the SetSingleConstantBuffer command */
636 dst_handle = svga_buffer_handle(svga, dst_buffer);
637 if (!dst_handle) {
638 pipe_resource_reference(&dst_buffer, NULL);
639 return PIPE_ERROR_OUT_OF_MEMORY;
640 }
641
642 assert(new_buf_size % 16 == 0);
643 ret = SVGA3D_vgpu10_SetSingleConstantBuffer(svga->swc,
644 0, /* index */
645 svga_shader_type(shader),
646 dst_handle,
647 offset,
648 new_buf_size);
649
650 if (ret != PIPE_OK) {
651 pipe_resource_reference(&dst_buffer, NULL);
652 return ret;
653 }
654
655 /* Save this const buffer until it's replaced in the future.
656 * Otherwise, all references to the buffer will go away after the
657 * command buffer is submitted, it'll get recycled and we will have
658 * incorrect constant buffer bindings.
659 */
660 pipe_resource_reference(&svga->state.hw_draw.constbuf[shader], dst_buffer);
661
662 svga->state.hw_draw.default_constbuf_size[shader] = new_buf_size;
663
664 pipe_resource_reference(&dst_buffer, NULL);
665
666 return ret;
667 }
668
669
670 static enum pipe_error
671 emit_consts_vgpu10(struct svga_context *svga, unsigned shader)
672 {
673 enum pipe_error ret;
674 unsigned dirty_constbufs;
675 unsigned enabled_constbufs;
676
677 /* Emit 0th constant buffer (with extra constants) */
678 ret = emit_constbuf_vgpu10(svga, shader);
679 if (ret != PIPE_OK) {
680 return ret;
681 }
682
683 enabled_constbufs = svga->state.hw_draw.enabled_constbufs[shader] | 1u;
684
685 /* Emit other constant buffers (UBOs) */
686 dirty_constbufs = svga->state.dirty_constbufs[shader] & ~1u;
687
688 while (dirty_constbufs) {
689 unsigned index = u_bit_scan(&dirty_constbufs);
690 unsigned offset = svga->curr.constbufs[shader][index].buffer_offset;
691 unsigned size = svga->curr.constbufs[shader][index].buffer_size;
692 struct svga_buffer *buffer =
693 svga_buffer(svga->curr.constbufs[shader][index].buffer);
694 struct svga_winsys_surface *handle;
695
696 if (buffer) {
697 handle = svga_buffer_handle(svga, &buffer->b.b);
698 enabled_constbufs |= 1 << index;
699 }
700 else {
701 handle = NULL;
702 enabled_constbufs &= ~(1 << index);
703 assert(offset == 0);
704 assert(size == 0);
705 }
706
707 if (size % 16 != 0) {
708 /* GL's buffer range sizes can be any number of bytes but the
709 * SVGA3D device requires a multiple of 16 bytes.
710 */
711 const unsigned total_size = buffer->b.b.width0;
712
713 if (offset + align(size, 16) <= total_size) {
714 /* round up size to multiple of 16 */
715 size = align(size, 16);
716 }
717 else {
718 /* round down to mulitple of 16 (this may cause rendering problems
719 * but should avoid a device error).
720 */
721 size &= ~15;
722 }
723 }
724
725 assert(size % 16 == 0);
726 ret = SVGA3D_vgpu10_SetSingleConstantBuffer(svga->swc,
727 index,
728 svga_shader_type(shader),
729 handle,
730 offset,
731 size);
732 if (ret != PIPE_OK)
733 return ret;
734 }
735
736 svga->state.hw_draw.enabled_constbufs[shader] = enabled_constbufs;
737 svga->state.dirty_constbufs[shader] = 0;
738
739 return ret;
740 }
741
742 static enum pipe_error
743 emit_fs_consts(struct svga_context *svga, unsigned dirty)
744 {
745 const struct svga_shader_variant *variant = svga->state.hw_draw.fs;
746 enum pipe_error ret = PIPE_OK;
747
748 /* SVGA_NEW_FS_VARIANT
749 */
750 if (!variant)
751 return PIPE_OK;
752
753 /* SVGA_NEW_FS_CONST_BUFFER
754 */
755 if (svga_have_vgpu10(svga)) {
756 ret = emit_consts_vgpu10(svga, PIPE_SHADER_FRAGMENT);
757 }
758 else {
759 ret = emit_consts_vgpu9(svga, PIPE_SHADER_FRAGMENT);
760 }
761
762 return ret;
763 }
764
765
766 struct svga_tracked_state svga_hw_fs_constants =
767 {
768 "hw fs params",
769 (SVGA_NEW_FS_CONST_BUFFER |
770 SVGA_NEW_FS_VARIANT |
771 SVGA_NEW_TEXTURE_BINDING),
772 emit_fs_consts
773 };
774
775
776
777 static enum pipe_error
778 emit_vs_consts(struct svga_context *svga, unsigned dirty)
779 {
780 const struct svga_shader_variant *variant = svga->state.hw_draw.vs;
781 enum pipe_error ret = PIPE_OK;
782
783 /* SVGA_NEW_VS_VARIANT
784 */
785 if (!variant)
786 return PIPE_OK;
787
788 /* SVGA_NEW_VS_CONST_BUFFER
789 */
790 if (svga_have_vgpu10(svga)) {
791 ret = emit_consts_vgpu10(svga, PIPE_SHADER_VERTEX);
792 }
793 else {
794 ret = emit_consts_vgpu9(svga, PIPE_SHADER_VERTEX);
795 }
796
797 return ret;
798 }
799
800
801 struct svga_tracked_state svga_hw_vs_constants =
802 {
803 "hw vs params",
804 (SVGA_NEW_PRESCALE |
805 SVGA_NEW_VS_CONST_BUFFER |
806 SVGA_NEW_VS_VARIANT),
807 emit_vs_consts
808 };
809
810
811 static enum pipe_error
812 emit_gs_consts(struct svga_context *svga, unsigned dirty)
813 {
814 const struct svga_shader_variant *variant = svga->state.hw_draw.gs;
815 enum pipe_error ret = PIPE_OK;
816
817 /* SVGA_NEW_GS_VARIANT
818 */
819 if (!variant)
820 return PIPE_OK;
821
822 /* SVGA_NEW_GS_CONST_BUFFER
823 */
824 if (svga_have_vgpu10(svga)) {
825 /**
826 * If only the rasterizer state has changed and the current geometry
827 * shader does not emit wide points, then there is no reason to
828 * re-emit the GS constants, so skip it.
829 */
830 if (dirty == SVGA_NEW_RAST && !variant->key.gs.wide_point)
831 return PIPE_OK;
832
833 ret = emit_consts_vgpu10(svga, PIPE_SHADER_GEOMETRY);
834 }
835
836 return ret;
837 }
838
839
840 struct svga_tracked_state svga_hw_gs_constants =
841 {
842 "hw gs params",
843 (SVGA_NEW_GS_CONST_BUFFER |
844 SVGA_NEW_RAST |
845 SVGA_NEW_GS_VARIANT),
846 emit_gs_consts
847 };