2 /**********************************************************
3 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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
25 **********************************************************/
27 #include "util/format/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"
33 #include "svga_screen.h"
34 #include "svga_context.h"
35 #include "svga_state.h"
37 #include "svga_tgsi.h"
38 #include "svga_debug.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_shader.h"
42 #include "svga_hw_reg.h"
46 * Don't try to send more than 4kb of successive constants.
48 #define MAX_CONST_REG_COUNT 256 /**< number of float[4] constants */
51 * Extra space for svga-specific VS/PS constants (such as texcoord
52 * scale factors, vertex transformation scale/translation).
54 #define MAX_EXTRA_CONSTS 32
56 /** Guest-backed surface constant buffers must be this size */
57 #define GB_CONSTBUF_SIZE (SVGA3D_CONSTREG_MAX)
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
66 svga_get_extra_constants_common(const struct svga_context
*svga
,
67 const struct svga_shader_variant
*variant
,
68 enum pipe_shader_type shader
, float *dest
)
70 uint32_t *dest_u
= (uint32_t *) dest
; // uint version of dest
74 for (i
= 0; i
< variant
->key
.num_textures
; i
++) {
75 const struct pipe_sampler_view
*sv
= svga
->curr
.sampler_views
[shader
][i
];
77 const struct pipe_resource
*tex
= sv
->texture
;
78 /* Scaling factors needed for handling unnormalized texture coordinates
79 * for texture rectangles.
81 if (variant
->key
.tex
[i
].unnormalized
) {
82 /* debug/sanity check */
83 assert(variant
->key
.tex
[i
].width_height_idx
== count
);
85 *dest
++ = 1.0f
/ (float) tex
->width0
;
86 *dest
++ = 1.0f
/ (float) tex
->height0
;
93 /* Store the sizes for texture buffers.
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
;
112 * Emit any extra fragment shader constants into the buffer pointed
114 * \return number of float[4] constants put into the dest buffer
117 svga_get_extra_fs_constants(const struct svga_context
*svga
, float *dest
)
119 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.fs
;
122 count
+= svga_get_extra_constants_common(svga
, variant
,
123 PIPE_SHADER_FRAGMENT
, dest
);
125 assert(count
<= MAX_EXTRA_CONSTS
);
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'.
136 svga_get_prescale_constants(const struct svga_context
*svga
, float **dest
,
137 const struct svga_prescale
*prescale
)
139 memcpy(*dest
, prescale
->scale
, 4 * sizeof(float));
142 memcpy(*dest
, prescale
->translate
, 4 * sizeof(float));
149 * Emit extra constants needed for point sprite emulation.
152 svga_get_pt_sprite_constants(const struct svga_context
*svga
, float **dest
)
154 const struct svga_screen
*screen
= svga_screen(svga
->pipe
.screen
);
157 dst
[0] = 1.0 / (svga
->curr
.viewport
[0].scale
[0] * 2);
158 dst
[1] = 1.0 / (svga
->curr
.viewport
[0].scale
[1] * 2);
159 dst
[2] = svga
->curr
.rast
->pointsize
;
160 dst
[3] = screen
->maxPointSize
;
166 * Emit user-defined clip plane coefficients into the buffer pointed to
167 * by '*dest'. The updated buffer pointer will be returned in 'dest'.
170 svga_get_clip_plane_constants(const struct svga_context
*svga
,
171 const struct svga_shader_variant
*variant
,
177 if (svga_have_vgpu10(svga
)) {
178 /* append user-defined clip plane coefficients onto constant buffer */
179 unsigned clip_planes
= variant
->key
.clip_plane_enable
;
180 while (clip_planes
) {
181 int i
= u_bit_scan(&clip_planes
);
182 COPY_4V(*dest
, svga
->curr
.clip
.ucp
[i
]);
192 * Emit any extra vertex shader constants into the buffer pointed
194 * In particular, these would be the scale and bias factors computed
195 * from the framebuffer size which are used to copy with differences in
196 * GL vs D3D coordinate spaces. See svga_tgsi_insn.c for more info.
197 * \return number of float[4] constants put into the dest buffer
200 svga_get_extra_vs_constants(const struct svga_context
*svga
, float *dest
)
202 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.vs
;
205 /* SVGA_NEW_VS_VARIANT
207 if (variant
->key
.vs
.need_prescale
) {
208 count
+= svga_get_prescale_constants(svga
, &dest
,
209 &svga
->state
.hw_clear
.prescale
[0]);
212 if (variant
->key
.vs
.undo_viewport
) {
213 /* Used to convert window coords back to NDC coords */
214 dest
[0] = 1.0f
/ svga
->curr
.viewport
[0].scale
[0];
215 dest
[1] = 1.0f
/ svga
->curr
.viewport
[0].scale
[1];
216 dest
[2] = -svga
->curr
.viewport
[0].translate
[0];
217 dest
[3] = -svga
->curr
.viewport
[0].translate
[1];
222 /* Bias to be added to VertexID */
223 if (variant
->key
.vs
.need_vertex_id_bias
) {
224 uint32_t *dest_u
= (uint32_t *) dest
; // uint version of dest
225 dest_u
[0] = svga
->curr
.vertex_id_bias
;
234 count
+= svga_get_clip_plane_constants(svga
, variant
, &dest
);
236 /* common constants */
237 count
+= svga_get_extra_constants_common(svga
, variant
,
238 PIPE_SHADER_VERTEX
, dest
);
240 assert(count
<= MAX_EXTRA_CONSTS
);
246 * Emit any extra geometry shader constants into the buffer pointed
250 svga_get_extra_gs_constants(const struct svga_context
*svga
, float *dest
)
252 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.gs
;
255 /* SVGA_NEW_GS_VARIANT
258 /* Constants for point sprite
259 * These are used in the transformed gs that supports point sprite.
260 * They need to be added before the prescale constants.
262 if (variant
->key
.gs
.wide_point
) {
263 count
+= svga_get_pt_sprite_constants(svga
, &dest
);
266 if (variant
->key
.gs
.need_prescale
) {
267 unsigned i
, num_prescale
= 1;
269 /* If prescale is needed and the geometry shader writes to viewport
270 * index, then prescale for all viewports will be added to the
273 if (variant
->key
.gs
.writes_viewport_index
)
274 num_prescale
= svga
->state
.hw_clear
.num_prescale
;
276 for (i
= 0; i
< num_prescale
; i
++) {
278 svga_get_prescale_constants(svga
, &dest
,
279 &svga
->state
.hw_clear
.prescale
[i
]);
284 count
+= svga_get_clip_plane_constants(svga
, variant
, &dest
);
286 /* common constants */
287 count
+= svga_get_extra_constants_common(svga
, variant
,
288 PIPE_SHADER_GEOMETRY
, dest
);
290 assert(count
<= MAX_EXTRA_CONSTS
);
296 * Emit any extra tessellation control shader constants into the
297 * buffer pointed to by 'dest'.
300 svga_get_extra_tcs_constants(struct svga_context
*svga
, float *dest
)
302 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tcs
;
306 count
+= svga_get_clip_plane_constants(svga
, variant
, &dest
);
308 /* common constants */
309 count
+= svga_get_extra_constants_common(svga
, variant
,
310 PIPE_SHADER_TESS_CTRL
,
313 assert(count
<= MAX_EXTRA_CONSTS
);
319 * Emit any extra tessellation evaluation shader constants into
320 * the buffer pointed to by 'dest'.
323 svga_get_extra_tes_constants(struct svga_context
*svga
, float *dest
)
325 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tes
;
328 if (variant
->key
.tes
.need_prescale
) {
329 count
+= svga_get_prescale_constants(svga
, &dest
,
330 &svga
->state
.hw_clear
.prescale
[0]);
334 count
+= svga_get_clip_plane_constants(svga
, variant
, &dest
);
336 /* common constants */
337 count
+= svga_get_extra_constants_common(svga
, variant
,
338 PIPE_SHADER_TESS_EVAL
,
341 assert(count
<= MAX_EXTRA_CONSTS
);
347 * Emit any extra compute shader constants into
348 * the buffer pointed to by 'dest'.
351 svga_get_extra_cs_constants(struct svga_context
*svga
, float *dest
)
353 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.cs
;
356 /* common constants */
357 count
+= svga_get_extra_constants_common(svga
, variant
,
361 assert(count
<= MAX_EXTRA_CONSTS
);
367 * Check and emit a range of shader constant registers, trying to coalesce
368 * successive shader constant updates in a single command in order to save
369 * space on the command buffer. This is a HWv8 feature.
371 static enum pipe_error
372 emit_const_range(struct svga_context
*svga
,
373 enum pipe_shader_type shader
,
376 const float (*values
)[4])
381 assert(shader
== PIPE_SHADER_VERTEX
||
382 shader
== PIPE_SHADER_FRAGMENT
);
383 assert(!svga_have_vgpu10(svga
));
386 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
387 debug_printf("svga: too many constants (offset %u + count %u = %u (max = %u))\n",
388 offset
, count
, offset
+ count
, SVGA3D_CONSTREG_MAX
);
392 if (offset
> SVGA3D_CONSTREG_MAX
) {
393 /* This isn't OK, but if we propagate an error all the way up we'll
394 * just get into more trouble.
395 * XXX note that offset is always zero at this time so this is moot.
400 if (offset
+ count
> SVGA3D_CONSTREG_MAX
) {
401 /* Just drop the extra constants for now.
402 * Ideally we should not have allowed the app to create a shader
403 * that exceeds our constant buffer size but there's no way to
404 * express that in gallium at this time.
406 count
= SVGA3D_CONSTREG_MAX
- offset
;
411 if (memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
413 4 * sizeof(float)) != 0) {
414 /* Found one dirty constant
416 if (SVGA_DEBUG
& DEBUG_CONSTS
)
417 debug_printf("%s %s %d: %f %f %f %f\n",
419 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
426 /* Look for more consecutive dirty constants.
430 j
< i
+ MAX_CONST_REG_COUNT
&&
431 memcmp(svga
->state
.hw_draw
.cb
[shader
][offset
+ j
],
433 4 * sizeof(float)) != 0) {
435 if (SVGA_DEBUG
& DEBUG_CONSTS
)
436 debug_printf("%s %s %d: %f %f %f %f\n",
438 shader
== PIPE_SHADER_VERTEX
? "VERT" : "FRAG",
450 /* Send them all together.
452 if (svga_have_gb_objects(svga
)) {
453 ret
= SVGA3D_SetGBShaderConstsInline(svga
->swc
,
454 offset
+ i
, /* start */
456 svga_shader_type(shader
),
457 SVGA3D_CONST_TYPE_FLOAT
,
461 ret
= SVGA3D_SetShaderConsts(svga
->swc
,
463 svga_shader_type(shader
),
464 SVGA3D_CONST_TYPE_FLOAT
,
467 if (ret
!= PIPE_OK
) {
472 * Local copy of the hardware state.
474 memcpy(svga
->state
.hw_draw
.cb
[shader
][offset
+ i
],
476 (j
- i
) * 4 * sizeof(float));
480 svga
->hud
.num_const_updates
++;
492 * Emit all the constants in a constant buffer for a shader stage.
493 * On VGPU10, emit_consts_vgpu10 is used instead.
495 static enum pipe_error
496 emit_consts_vgpu9(struct svga_context
*svga
, enum pipe_shader_type shader
)
498 const struct pipe_constant_buffer
*cbuf
;
499 struct pipe_transfer
*transfer
= NULL
;
501 const float (*data
)[4] = NULL
;
502 enum pipe_error ret
= PIPE_OK
;
503 const unsigned offset
= 0;
505 assert(shader
< PIPE_SHADER_TYPES
);
506 assert(!svga_have_vgpu10(svga
));
507 /* Only one constant buffer per shader is supported before VGPU10.
508 * This is only an approximate check against that.
510 assert(svga
->curr
.constbufs
[shader
][1].buffer
== NULL
);
512 cbuf
= &svga
->curr
.constbufs
[shader
][0];
514 if (svga
->curr
.constbufs
[shader
][0].buffer
) {
515 /* emit user-provided constants */
516 data
= (const float (*)[4])
517 pipe_buffer_map(&svga
->pipe
, svga
->curr
.constbufs
[shader
][0].buffer
,
518 PIPE_TRANSFER_READ
, &transfer
);
520 return PIPE_ERROR_OUT_OF_MEMORY
;
524 assert(cbuf
->buffer
->width0
>= cbuf
->buffer_size
);
526 /* Use/apply the constant buffer size and offsets here */
527 count
= cbuf
->buffer_size
/ (4 * sizeof(float));
528 data
+= cbuf
->buffer_offset
/ (4 * sizeof(float));
530 ret
= emit_const_range( svga
, shader
, offset
, count
, data
);
532 pipe_buffer_unmap(&svga
->pipe
, transfer
);
534 if (ret
!= PIPE_OK
) {
539 /* emit extra shader constants */
541 const struct svga_shader_variant
*variant
= NULL
;
543 float extras
[MAX_EXTRA_CONSTS
][4];
547 case PIPE_SHADER_VERTEX
:
548 variant
= svga
->state
.hw_draw
.vs
;
549 count
= svga_get_extra_vs_constants(svga
, (float *) extras
);
551 case PIPE_SHADER_FRAGMENT
:
552 variant
= svga
->state
.hw_draw
.fs
;
553 count
= svga_get_extra_fs_constants(svga
, (float *) extras
);
556 assert(!"Unexpected shader type");
561 offset
= variant
->shader
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
562 assert(count
<= ARRAY_SIZE(extras
));
565 ret
= emit_const_range(svga
, shader
, offset
, count
,
566 (const float (*) [4])extras
);
575 * A helper function to emit a constant buffer binding at the
576 * specified slot for the specified shader type
578 static enum pipe_error
579 emit_constbuf(struct svga_context
*svga
,
581 enum pipe_shader_type shader
,
582 unsigned buffer_offset
,
583 unsigned buffer_size
,
585 unsigned extra_buffer_offset
,
586 unsigned extra_buffer_size
,
587 const void *extra_buffer
)
589 struct svga_buffer
*sbuf
= svga_buffer((struct pipe_resource
*)buffer
);
590 struct pipe_resource
*dst_buffer
= NULL
;
591 enum pipe_error ret
= PIPE_OK
;
592 struct pipe_transfer
*src_transfer
;
593 struct svga_winsys_surface
*dst_handle
= NULL
;
594 unsigned new_buf_size
= 0;
595 unsigned alloc_buf_size
;
596 unsigned offset
= 0;;
597 void *src_map
= NULL
, *dst_map
;
599 if ((sbuf
&& sbuf
->swbuf
) || extra_buffer
) {
601 /* buffer here is a user-space buffer so mapping it is really cheap. */
602 if (buffer_size
> 0) {
603 src_map
= pipe_buffer_map_range(&svga
->pipe
,
604 (struct pipe_resource
*)buffer
,
605 buffer_offset
, buffer_size
,
606 PIPE_TRANSFER_READ
, &src_transfer
);
609 return PIPE_ERROR_OUT_OF_MEMORY
;
613 new_buf_size
= MAX2(buffer_size
, extra_buffer_offset
) + extra_buffer_size
;
615 /* According to the DX10 spec, the constant buffer size must be
616 * in multiples of 16.
618 new_buf_size
= align(new_buf_size
, 16);
620 /* Constant buffer size in the upload buffer must be in multiples of 256.
621 * In order to maximize the chance of merging the upload buffer chunks
622 * when svga_buffer_add_range() is called,
623 * the allocate buffer size needs to be in multiples of 256 as well.
624 * Otherwise, since there is gap between each dirty range of the upload buffer,
625 * each dirty range will end up in its own UPDATE_GB_IMAGE command.
627 alloc_buf_size
= align(new_buf_size
, CONST0_UPLOAD_ALIGNMENT
);
629 u_upload_alloc(svga
->const0_upload
, 0, alloc_buf_size
,
630 CONST0_UPLOAD_ALIGNMENT
, &offset
,
631 &dst_buffer
, &dst_map
);
635 pipe_buffer_unmap(&svga
->pipe
, src_transfer
);
636 return PIPE_ERROR_OUT_OF_MEMORY
;
639 /* Initialize the allocated buffer slot to 0 to ensure the padding is
642 memset(dst_map
, 0, alloc_buf_size
);
645 memcpy(dst_map
, src_map
, buffer_size
);
646 pipe_buffer_unmap(&svga
->pipe
, src_transfer
);
649 if (extra_buffer_size
) {
650 assert(extra_buffer_offset
+ extra_buffer_size
<= new_buf_size
);
651 memcpy((char *) dst_map
+ extra_buffer_offset
, extra_buffer
,
655 /* Get winsys handle for the constant buffer */
656 if (svga
->state
.hw_draw
.const0_buffer
== dst_buffer
&&
657 svga
->state
.hw_draw
.const0_handle
) {
658 /* re-reference already mapped buffer */
659 dst_handle
= svga
->state
.hw_draw
.const0_handle
;
662 /* we must unmap the buffer before getting the winsys handle */
663 u_upload_unmap(svga
->const0_upload
);
665 dst_handle
= svga_buffer_handle(svga
, dst_buffer
,
666 PIPE_BIND_CONSTANT_BUFFER
);
668 pipe_resource_reference(&dst_buffer
, NULL
);
669 return PIPE_ERROR_OUT_OF_MEMORY
;
674 dst_handle
= svga_buffer_handle(svga
, &sbuf
->b
.b
, PIPE_BIND_CONSTANT_BUFFER
);
675 new_buf_size
= align(buffer_size
, 16);
676 offset
= buffer_offset
;
679 assert(new_buf_size
% 16 == 0);
681 const struct svga_screen
*screen
= svga_screen(svga
->pipe
.screen
);
682 const struct svga_winsys_screen
*sws
= screen
->sws
;
684 /* Issue the SetSingleConstantBuffer command */
685 if (!sws
->have_constant_buffer_offset_cmd
||
686 svga
->state
.hw_draw
.constbufoffsets
[shader
][slot
].handle
!= dst_handle
||
687 svga
->state
.hw_draw
.constbufoffsets
[shader
][slot
].size
!= new_buf_size
) {
688 ret
= SVGA3D_vgpu10_SetSingleConstantBuffer(svga
->swc
,
690 svga_shader_type(shader
),
695 else if (dst_handle
){
696 unsigned command
= SVGA_3D_CMD_DX_SET_VS_CONSTANT_BUFFER_OFFSET
+ shader
;
697 ret
= SVGA3D_vgpu10_SetConstantBufferOffset(svga
->swc
,
703 if (ret
!= PIPE_OK
) {
704 pipe_resource_reference(&dst_buffer
, NULL
);
708 /* save the upload buffer / handle for next time */
709 if (dst_buffer
!= buffer
&& dst_buffer
) {
710 pipe_resource_reference(&svga
->state
.hw_draw
.const0_buffer
, dst_buffer
);
711 svga
->state
.hw_draw
.const0_handle
= dst_handle
;
714 /* Save this const buffer until it's replaced in the future.
715 * Otherwise, all references to the buffer will go away after the
716 * command buffer is submitted, it'll get recycled and we will have
717 * incorrect constant buffer bindings.
719 pipe_resource_reference(&svga
->state
.hw_draw
.constbuf
[shader
][slot
], dst_buffer
);
720 svga
->state
.hw_draw
.constbufoffsets
[shader
][slot
].handle
= dst_handle
;
721 svga
->state
.hw_draw
.constbufoffsets
[shader
][slot
].size
= new_buf_size
;
723 pipe_resource_reference(&dst_buffer
, NULL
);
730 static enum pipe_error
731 emit_consts_vgpu10(struct svga_context
*svga
, enum pipe_shader_type shader
)
733 const struct pipe_constant_buffer
*cbuf
;
734 enum pipe_error ret
= PIPE_OK
;
735 float extras
[MAX_EXTRA_CONSTS
][4];
736 unsigned extra_count
, extra_size
, extra_offset
;
737 const struct svga_shader_variant
*variant
;
739 assert(shader
== PIPE_SHADER_VERTEX
||
740 shader
== PIPE_SHADER_GEOMETRY
||
741 shader
== PIPE_SHADER_FRAGMENT
||
742 shader
== PIPE_SHADER_TESS_CTRL
||
743 shader
== PIPE_SHADER_TESS_EVAL
||
744 shader
== PIPE_SHADER_COMPUTE
);
746 cbuf
= &svga
->curr
.constbufs
[shader
][0];
749 case PIPE_SHADER_VERTEX
:
750 variant
= svga
->state
.hw_draw
.vs
;
751 extra_count
= svga_get_extra_vs_constants(svga
, (float *) extras
);
753 case PIPE_SHADER_FRAGMENT
:
754 variant
= svga
->state
.hw_draw
.fs
;
755 extra_count
= svga_get_extra_fs_constants(svga
, (float *) extras
);
757 case PIPE_SHADER_GEOMETRY
:
758 variant
= svga
->state
.hw_draw
.gs
;
759 extra_count
= svga_get_extra_gs_constants(svga
, (float *) extras
);
761 case PIPE_SHADER_TESS_CTRL
:
762 variant
= svga
->state
.hw_draw
.tcs
;
763 extra_count
= svga_get_extra_tcs_constants(svga
, (float *) extras
);
765 case PIPE_SHADER_TESS_EVAL
:
766 variant
= svga
->state
.hw_draw
.tes
;
767 extra_count
= svga_get_extra_tes_constants(svga
, (float *) extras
);
769 case PIPE_SHADER_COMPUTE
:
770 variant
= svga
->state
.hw_draw
.cs
;
771 extra_count
= svga_get_extra_cs_constants(svga
, (float *) extras
);
774 assert(!"Unexpected shader type");
775 /* Don't return an error code since we don't want to keep re-trying
776 * this function and getting stuck in an infinite loop.
783 cbuf
= &svga
->curr
.constbufs
[shader
][0];
785 /* Compute extra constants size and offset in bytes */
786 extra_size
= extra_count
* 4 * sizeof(float);
787 extra_offset
= 4 * sizeof(float) * variant
->extra_const_start
;
789 if (cbuf
->buffer_size
+ extra_size
== 0)
790 return PIPE_OK
; /* nothing to do */
792 ret
= emit_constbuf(svga
, 0, shader
,
793 cbuf
->buffer_offset
, cbuf
->buffer_size
, cbuf
->buffer
,
794 extra_offset
, extra_size
, extras
);
798 svga
->state
.hw_draw
.default_constbuf_size
[shader
] =
799 svga
->state
.hw_draw
.constbufoffsets
[shader
][0].size
;
801 svga
->hud
.num_const_updates
++;
807 static enum pipe_error
808 emit_constbuf_vgpu10(struct svga_context
*svga
, enum pipe_shader_type shader
)
810 enum pipe_error ret
= PIPE_OK
;
811 unsigned dirty_constbufs
;
812 unsigned enabled_constbufs
;
814 enabled_constbufs
= svga
->state
.hw_draw
.enabled_constbufs
[shader
] | 1u;
815 dirty_constbufs
= (svga
->state
.dirty_constbufs
[shader
]|enabled_constbufs
) & ~1u;
817 while (dirty_constbufs
) {
818 unsigned index
= u_bit_scan(&dirty_constbufs
);
819 unsigned offset
= svga
->curr
.constbufs
[shader
][index
].buffer_offset
;
820 unsigned size
= svga
->curr
.constbufs
[shader
][index
].buffer_size
;
821 struct svga_buffer
*buffer
=
822 svga_buffer(svga
->curr
.constbufs
[shader
][index
].buffer
);
825 enabled_constbufs
|= 1 << index
;
828 enabled_constbufs
&= ~(1 << index
);
833 if (size
% 16 != 0) {
834 /* GL's buffer range sizes can be any number of bytes but the
835 * SVGA3D device requires a multiple of 16 bytes.
837 const unsigned total_size
= buffer
->b
.b
.width0
;
839 if (offset
+ align(size
, 16) <= total_size
) {
840 /* round up size to multiple of 16 */
841 size
= align(size
, 16);
844 /* round down to mulitple of 16 (this may cause rendering problems
845 * but should avoid a device error).
851 assert(size
% 16 == 0);
853 ret
= emit_constbuf(svga
, index
, shader
, offset
, size
, buffer
,
858 svga
->hud
.num_const_buf_updates
++;
861 svga
->state
.hw_draw
.enabled_constbufs
[shader
] = enabled_constbufs
;
862 svga
->state
.dirty_constbufs
[shader
] = 0;
867 static enum pipe_error
868 emit_fs_consts(struct svga_context
*svga
, uint64_t dirty
)
870 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.fs
;
871 enum pipe_error ret
= PIPE_OK
;
873 /* SVGA_NEW_FS_VARIANT
878 /* SVGA_NEW_FS_CONSTS
880 if (svga_have_vgpu10(svga
)) {
881 ret
= emit_consts_vgpu10(svga
, PIPE_SHADER_FRAGMENT
);
884 ret
= emit_consts_vgpu9(svga
, PIPE_SHADER_FRAGMENT
);
890 static enum pipe_error
891 emit_fs_constbuf(struct svga_context
*svga
, uint64_t dirty
)
893 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.fs
;
894 enum pipe_error ret
= PIPE_OK
;
896 /* SVGA_NEW_FS_VARIANT
901 /* SVGA_NEW_FS_CONSTBUF
903 assert(svga_have_vgpu10(svga
));
904 ret
= emit_constbuf_vgpu10(svga
, PIPE_SHADER_FRAGMENT
);
909 struct svga_tracked_state svga_hw_fs_constants
=
912 (SVGA_NEW_FS_CONSTS
|
913 SVGA_NEW_FS_VARIANT
|
914 SVGA_NEW_TEXTURE_CONSTS
),
919 struct svga_tracked_state svga_hw_fs_constbufs
=
922 SVGA_NEW_FS_CONST_BUFFER
,
927 static enum pipe_error
928 emit_vs_consts(struct svga_context
*svga
, uint64_t dirty
)
930 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.vs
;
931 enum pipe_error ret
= PIPE_OK
;
933 /* SVGA_NEW_VS_VARIANT
938 /* SVGA_NEW_VS_CONST_BUFFER
940 if (svga_have_vgpu10(svga
)) {
941 ret
= emit_consts_vgpu10(svga
, PIPE_SHADER_VERTEX
);
944 ret
= emit_consts_vgpu9(svga
, PIPE_SHADER_VERTEX
);
951 static enum pipe_error
952 emit_vs_constbuf(struct svga_context
*svga
, uint64_t dirty
)
954 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.vs
;
955 enum pipe_error ret
= PIPE_OK
;
957 /* SVGA_NEW_FS_VARIANT
962 /* SVGA_NEW_FS_CONSTBUF
964 assert(svga_have_vgpu10(svga
));
965 ret
= emit_constbuf_vgpu10(svga
, PIPE_SHADER_VERTEX
);
971 struct svga_tracked_state svga_hw_vs_constants
=
976 SVGA_NEW_VS_VARIANT
|
977 SVGA_NEW_TEXTURE_CONSTS
),
982 struct svga_tracked_state svga_hw_vs_constbufs
=
985 SVGA_NEW_VS_CONST_BUFFER
,
990 static enum pipe_error
991 emit_gs_consts(struct svga_context
*svga
, uint64_t dirty
)
993 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.gs
;
994 enum pipe_error ret
= PIPE_OK
;
996 /* SVGA_NEW_GS_VARIANT
1001 /* SVGA_NEW_GS_CONST_BUFFER
1003 assert(svga_have_vgpu10(svga
));
1006 * If only the rasterizer state has changed and the current geometry
1007 * shader does not emit wide points, then there is no reason to
1008 * re-emit the GS constants, so skip it.
1010 if (dirty
== SVGA_NEW_RAST
&& !variant
->key
.gs
.wide_point
)
1013 ret
= emit_consts_vgpu10(svga
, PIPE_SHADER_GEOMETRY
);
1019 static enum pipe_error
1020 emit_gs_constbuf(struct svga_context
*svga
, uint64_t dirty
)
1022 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.gs
;
1023 enum pipe_error ret
= PIPE_OK
;
1025 /* SVGA_NEW_GS_VARIANT
1030 /* SVGA_NEW_GS_CONSTBUF
1032 assert(svga_have_vgpu10(svga
));
1033 ret
= emit_constbuf_vgpu10(svga
, PIPE_SHADER_GEOMETRY
);
1039 struct svga_tracked_state svga_hw_gs_constants
=
1042 (SVGA_NEW_PRESCALE
|
1043 SVGA_NEW_GS_CONSTS
|
1045 SVGA_NEW_GS_VARIANT
|
1046 SVGA_NEW_TEXTURE_CONSTS
),
1051 struct svga_tracked_state svga_hw_gs_constbufs
=
1054 SVGA_NEW_GS_CONST_BUFFER
,
1060 * Emit constant buffer for tessellation control shader
1062 static enum pipe_error
1063 emit_tcs_consts(struct svga_context
*svga
, uint64_t dirty
)
1065 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tcs
;
1066 enum pipe_error ret
= PIPE_OK
;
1068 assert(svga_have_sm5(svga
));
1070 /* SVGA_NEW_TCS_VARIANT */
1074 /* SVGA_NEW_TCS_CONST_BUFFER */
1076 ret
= emit_consts_vgpu10(svga
, PIPE_SHADER_TESS_CTRL
);
1082 static enum pipe_error
1083 emit_tcs_constbuf(struct svga_context
*svga
, uint64_t dirty
)
1085 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tcs
;
1086 enum pipe_error ret
= PIPE_OK
;
1088 /* SVGA_NEW_TCS_VARIANT
1093 /* SVGA_NEW_TCS_CONSTBUF
1095 assert(svga_have_vgpu10(svga
));
1096 ret
= emit_constbuf_vgpu10(svga
, PIPE_SHADER_TESS_CTRL
);
1102 struct svga_tracked_state svga_hw_tcs_constants
=
1105 (SVGA_NEW_TCS_CONSTS
|
1106 SVGA_NEW_TCS_VARIANT
),
1111 struct svga_tracked_state svga_hw_tcs_constbufs
=
1114 SVGA_NEW_TCS_CONST_BUFFER
,
1120 * Emit constant buffer for tessellation evaluation shader
1122 static enum pipe_error
1123 emit_tes_consts(struct svga_context
*svga
, uint64_t dirty
)
1125 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tes
;
1126 enum pipe_error ret
= PIPE_OK
;
1128 assert(svga_have_sm5(svga
));
1130 /* SVGA_NEW_TES_VARIANT */
1134 ret
= emit_consts_vgpu10(svga
, PIPE_SHADER_TESS_EVAL
);
1140 static enum pipe_error
1141 emit_tes_constbuf(struct svga_context
*svga
, uint64_t dirty
)
1143 const struct svga_shader_variant
*variant
= svga
->state
.hw_draw
.tes
;
1144 enum pipe_error ret
= PIPE_OK
;
1146 /* SVGA_NEW_TES_VARIANT
1151 /* SVGA_NEW_TES_CONSTBUF
1153 assert(svga_have_vgpu10(svga
));
1154 ret
= emit_constbuf_vgpu10(svga
, PIPE_SHADER_TESS_EVAL
);
1160 struct svga_tracked_state svga_hw_tes_constants
=
1163 (SVGA_NEW_PRESCALE
|
1164 SVGA_NEW_TES_CONSTS
|
1165 SVGA_NEW_TES_VARIANT
),
1170 struct svga_tracked_state svga_hw_tes_constbufs
=
1173 SVGA_NEW_TES_CONST_BUFFER
,