gallium: change set_constant_buffer to be UBO-friendly
[mesa.git] / src / mesa / state_tracker / st_atom_constbuf.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "main/imports.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41
42 #include "st_debug.h"
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47
48
49 /**
50 * Pass the given program parameters to the graphics pipe as a
51 * constant buffer.
52 * \param shader_type either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
53 */
54 void st_upload_constants( struct st_context *st,
55 struct gl_program_parameter_list *params,
56 unsigned shader_type)
57 {
58 struct pipe_context *pipe = st->pipe;
59
60 assert(shader_type == PIPE_SHADER_VERTEX ||
61 shader_type == PIPE_SHADER_FRAGMENT ||
62 shader_type == PIPE_SHADER_GEOMETRY);
63
64 /* update constants */
65 if (params && params->NumParameters) {
66 struct pipe_constant_buffer cb;
67 const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4;
68
69 /* Update the constants which come from fixed-function state, such as
70 * transformation matrices, fog factors, etc. The rest of the values in
71 * the parameters list are explicitly set by the user with glUniform,
72 * glProgramParameter(), etc.
73 */
74 _mesa_load_state_parameters(st->ctx, params);
75
76 /* We always need to get a new buffer, to keep the drivers simple and
77 * avoid gratuitous rendering synchronization.
78 * Let's use a user buffer to avoid an unnecessary copy.
79 */
80 cb.buffer = pipe_user_buffer_create(pipe->screen,
81 params->ParameterValues,
82 paramBytes,
83 PIPE_BIND_CONSTANT_BUFFER);
84 cb.buffer_offset = 0;
85 cb.buffer_size = paramBytes;
86
87 if (ST_DEBUG & DEBUG_CONSTANTS) {
88 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
89 __FUNCTION__, shader_type, params->NumParameters,
90 params->StateFlags);
91 _mesa_print_parameter_list(params);
92 }
93
94 st->pipe->set_constant_buffer(st->pipe, shader_type, 0, &cb);
95 pipe_resource_reference(&cb.buffer, NULL);
96
97 st->state.constants[shader_type].ptr = params->ParameterValues;
98 st->state.constants[shader_type].size = paramBytes;
99 }
100 else if (st->state.constants[shader_type].ptr) {
101 st->state.constants[shader_type].ptr = NULL;
102 st->state.constants[shader_type].size = 0;
103 st->pipe->set_constant_buffer(st->pipe, shader_type, 0, NULL);
104 }
105 }
106
107
108 /**
109 * Vertex shader:
110 */
111 static void update_vs_constants(struct st_context *st )
112 {
113 struct st_vertex_program *vp = st->vp;
114 struct gl_program_parameter_list *params = vp->Base.Base.Parameters;
115
116 st_upload_constants( st, params, PIPE_SHADER_VERTEX );
117 }
118
119
120 const struct st_tracked_state st_update_vs_constants = {
121 "st_update_vs_constants", /* name */
122 { /* dirty */
123 (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS), /* mesa */
124 ST_NEW_VERTEX_PROGRAM, /* st */
125 },
126 update_vs_constants /* update */
127 };
128
129
130
131 /**
132 * Fragment shader:
133 */
134 static void update_fs_constants(struct st_context *st )
135 {
136 struct st_fragment_program *fp = st->fp;
137 struct gl_program_parameter_list *params = fp->Base.Base.Parameters;
138
139 st_upload_constants( st, params, PIPE_SHADER_FRAGMENT );
140 }
141
142
143 const struct st_tracked_state st_update_fs_constants = {
144 "st_update_fs_constants", /* name */
145 { /* dirty */
146 (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS), /* mesa */
147 ST_NEW_FRAGMENT_PROGRAM, /* st */
148 },
149 update_fs_constants /* update */
150 };
151
152 /* Geometry shader:
153 */
154 static void update_gs_constants(struct st_context *st )
155 {
156 struct st_geometry_program *gp = st->gp;
157 struct gl_program_parameter_list *params;
158
159 if (gp) {
160 params = gp->Base.Base.Parameters;
161 st_upload_constants( st, params, PIPE_SHADER_GEOMETRY );
162 }
163 }
164
165 const struct st_tracked_state st_update_gs_constants = {
166 "st_update_gs_constants", /* name */
167 { /* dirty */
168 (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS), /* mesa */
169 ST_NEW_GEOMETRY_PROGRAM, /* st */
170 },
171 update_gs_constants /* update */
172 };