1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/u_inlines.h"
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "util/u_math.h"
29bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "svga_context.h"
32bf215546Sopenharmony_ci#include "svga_resource_buffer.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct svga_constbuf
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   unsigned type;
38bf215546Sopenharmony_ci   float (*data)[4];
39bf215546Sopenharmony_ci   unsigned count;
40bf215546Sopenharmony_ci};
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic void
45bf215546Sopenharmony_cisvga_set_constant_buffer(struct pipe_context *pipe,
46bf215546Sopenharmony_ci                         enum pipe_shader_type shader, uint index,
47bf215546Sopenharmony_ci                         bool take_ownership,
48bf215546Sopenharmony_ci                         const struct pipe_constant_buffer *cb)
49bf215546Sopenharmony_ci{
50bf215546Sopenharmony_ci   struct svga_screen *svgascreen = svga_screen(pipe->screen);
51bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
52bf215546Sopenharmony_ci   struct pipe_resource *buf = cb ? cb->buffer : NULL;
53bf215546Sopenharmony_ci   unsigned buffer_size = 0;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   if (cb) {
56bf215546Sopenharmony_ci      buffer_size = cb->buffer_size;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      if (cb->user_buffer) {
59bf215546Sopenharmony_ci         buf = svga_user_buffer_create(pipe->screen,
60bf215546Sopenharmony_ci                                       (void *) cb->user_buffer,
61bf215546Sopenharmony_ci                                       cb->buffer_size,
62bf215546Sopenharmony_ci                                       PIPE_BIND_CONSTANT_BUFFER);
63bf215546Sopenharmony_ci      }
64bf215546Sopenharmony_ci   }
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   assert(shader < PIPE_SHADER_TYPES);
67bf215546Sopenharmony_ci   assert(index < ARRAY_SIZE(svga->curr.constbufs[shader]));
68bf215546Sopenharmony_ci   assert(index < svgascreen->max_const_buffers);
69bf215546Sopenharmony_ci   (void) svgascreen;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   if (take_ownership) {
72bf215546Sopenharmony_ci      pipe_resource_reference(&svga->curr.constbufs[shader][index].buffer, NULL);
73bf215546Sopenharmony_ci      svga->curr.constbufs[shader][index].buffer = buf;
74bf215546Sopenharmony_ci   } else {
75bf215546Sopenharmony_ci      pipe_resource_reference(&svga->curr.constbufs[shader][index].buffer, buf);
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   /* Make sure the constant buffer size to be updated is within the
79bf215546Sopenharmony_ci    * limit supported by the device.
80bf215546Sopenharmony_ci    */
81bf215546Sopenharmony_ci   svga->curr.constbufs[shader][index].buffer_size =
82bf215546Sopenharmony_ci      MIN2(buffer_size, SVGA_MAX_CONST_BUF_SIZE);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   svga->curr.constbufs[shader][index].buffer_offset = cb ? cb->buffer_offset : 0;
85bf215546Sopenharmony_ci   svga->curr.constbufs[shader][index].user_buffer = NULL; /* not used */
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   if (index == 0) {
88bf215546Sopenharmony_ci      if (shader == PIPE_SHADER_FRAGMENT)
89bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_FS_CONSTS;
90bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_VERTEX)
91bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_VS_CONSTS;
92bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_GEOMETRY)
93bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_GS_CONSTS;
94bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_TESS_CTRL)
95bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TCS_CONSTS;
96bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_TESS_EVAL)
97bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TES_CONSTS;
98bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_COMPUTE)
99bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_CS_CONSTS;
100bf215546Sopenharmony_ci   } else {
101bf215546Sopenharmony_ci      if (shader == PIPE_SHADER_FRAGMENT)
102bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_FS_CONST_BUFFER;
103bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_VERTEX)
104bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_VS_CONST_BUFFER;
105bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_GEOMETRY)
106bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_GS_CONST_BUFFER;
107bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_TESS_CTRL)
108bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TCS_CONST_BUFFER;
109bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_TESS_EVAL)
110bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TES_CONST_BUFFER;
111bf215546Sopenharmony_ci      else if (shader == PIPE_SHADER_COMPUTE)
112bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_CS_CONST_BUFFER;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci      /* update bitmask of dirty const buffers */
115bf215546Sopenharmony_ci      svga->state.dirty_constbufs[shader] |= (1 << index);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci      /* purge any stale rawbuf srv */
118bf215546Sopenharmony_ci      svga_destroy_rawbuf_srv(svga);
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   if (cb && cb->user_buffer) {
122bf215546Sopenharmony_ci      pipe_resource_reference(&buf, NULL);
123bf215546Sopenharmony_ci   }
124bf215546Sopenharmony_ci}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_civoid
128bf215546Sopenharmony_cisvga_init_constbuffer_functions(struct svga_context *svga)
129bf215546Sopenharmony_ci{
130bf215546Sopenharmony_ci   svga->pipe.set_constant_buffer = svga_set_constant_buffer;
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133