1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "util/u_inlines.h"
9 #include "pipe/p_defines.h"
10 #include "util/u_math.h"
11
12 #include "svga_context.h"
13 #include "svga_resource_buffer.h"
14
15
16 struct svga_constbuf
17 {
18 unsigned type;
19 float (*data)[4];
20 unsigned count;
21 };
22
23
24
25 static void
svga_set_constant_buffer(struct pipe_context * pipe,enum pipe_shader_type shader,uint index,bool take_ownership,const struct pipe_constant_buffer * cb)26 svga_set_constant_buffer(struct pipe_context *pipe,
27 enum pipe_shader_type shader, uint index,
28 bool take_ownership,
29 const struct pipe_constant_buffer *cb)
30 {
31 struct svga_screen *svgascreen = svga_screen(pipe->screen);
32 struct svga_context *svga = svga_context(pipe);
33 struct pipe_resource *buf = cb ? cb->buffer : NULL;
34 unsigned buffer_size = 0;
35
36 if (cb) {
37 buffer_size = cb->buffer_size;
38
39 if (cb->user_buffer) {
40 buf = svga_user_buffer_create(pipe->screen,
41 (void *) cb->user_buffer,
42 cb->buffer_size,
43 PIPE_BIND_CONSTANT_BUFFER);
44 }
45 }
46
47 assert(shader < PIPE_SHADER_TYPES);
48 assert(index < ARRAY_SIZE(svga->curr.constbufs[shader]));
49 assert(index < svgascreen->max_const_buffers);
50 (void) svgascreen;
51
52 if (take_ownership) {
53 pipe_resource_reference(&svga->curr.constbufs[shader][index].buffer, NULL);
54 svga->curr.constbufs[shader][index].buffer = buf;
55 } else {
56 pipe_resource_reference(&svga->curr.constbufs[shader][index].buffer, buf);
57 }
58
59 /* Make sure the constant buffer size to be updated is within the
60 * limit supported by the device.
61 */
62 svga->curr.constbufs[shader][index].buffer_size =
63 MIN2(buffer_size, SVGA_MAX_CONST_BUF_SIZE);
64
65 svga->curr.constbufs[shader][index].buffer_offset = cb ? cb->buffer_offset : 0;
66 svga->curr.constbufs[shader][index].user_buffer = NULL; /* not used */
67
68 if (index == 0) {
69 if (shader == PIPE_SHADER_FRAGMENT)
70 svga->dirty |= SVGA_NEW_FS_CONSTS;
71 else if (shader == PIPE_SHADER_VERTEX)
72 svga->dirty |= SVGA_NEW_VS_CONSTS;
73 else if (shader == PIPE_SHADER_GEOMETRY)
74 svga->dirty |= SVGA_NEW_GS_CONSTS;
75 else if (shader == PIPE_SHADER_TESS_CTRL)
76 svga->dirty |= SVGA_NEW_TCS_CONSTS;
77 else if (shader == PIPE_SHADER_TESS_EVAL)
78 svga->dirty |= SVGA_NEW_TES_CONSTS;
79 else if (shader == PIPE_SHADER_COMPUTE)
80 svga->dirty |= SVGA_NEW_CS_CONSTS;
81 } else {
82 if (shader == PIPE_SHADER_FRAGMENT)
83 svga->dirty |= SVGA_NEW_FS_CONST_BUFFER;
84 else if (shader == PIPE_SHADER_VERTEX)
85 svga->dirty |= SVGA_NEW_VS_CONST_BUFFER;
86 else if (shader == PIPE_SHADER_GEOMETRY)
87 svga->dirty |= SVGA_NEW_GS_CONST_BUFFER;
88 else if (shader == PIPE_SHADER_TESS_CTRL)
89 svga->dirty |= SVGA_NEW_TCS_CONST_BUFFER;
90 else if (shader == PIPE_SHADER_TESS_EVAL)
91 svga->dirty |= SVGA_NEW_TES_CONST_BUFFER;
92 else if (shader == PIPE_SHADER_COMPUTE)
93 svga->dirty |= SVGA_NEW_CS_CONST_BUFFER;
94
95 /* update bitmask of dirty const buffers */
96 svga->state.dirty_constbufs[shader] |= (1 << index);
97
98 /* purge any stale rawbuf srv */
99 svga_destroy_rawbuf_srv(svga);
100 }
101
102 if (cb && cb->user_buffer) {
103 pipe_resource_reference(&buf, NULL);
104 }
105 }
106
107
108 void
svga_init_constbuffer_functions(struct svga_context * svga)109 svga_init_constbuffer_functions(struct svga_context *svga)
110 {
111 svga->pipe.set_constant_buffer = svga_set_constant_buffer;
112 }
113
114