1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "main/imports.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41 #include "util/u_upload_mgr.h"
42 #include "cso_cache/cso_context.h"
45 #include "st_context.h"
47 #include "st_atom_constbuf.h"
48 #include "st_program.h"
49 #include "st_cb_bufferobjects.h"
52 * Pass the given program parameters to the graphics pipe as a
54 * \param shader_type either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
56 void st_upload_constants( struct st_context
*st
,
57 struct gl_program_parameter_list
*params
,
60 assert(shader_type
== PIPE_SHADER_VERTEX
||
61 shader_type
== PIPE_SHADER_FRAGMENT
||
62 shader_type
== PIPE_SHADER_GEOMETRY
);
64 /* update constants */
65 if (params
&& params
->NumParameters
) {
66 struct pipe_constant_buffer cb
;
67 const uint paramBytes
= params
->NumParameters
* sizeof(GLfloat
) * 4;
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.
74 _mesa_load_state_parameters(st
->ctx
, params
);
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.
80 if (st
->constbuf_uploader
) {
82 cb
.user_buffer
= NULL
;
83 u_upload_data(st
->constbuf_uploader
, 0, paramBytes
,
84 params
->ParameterValues
, &cb
.buffer_offset
, &cb
.buffer
);
85 u_upload_unmap(st
->constbuf_uploader
);
88 cb
.user_buffer
= params
->ParameterValues
;
91 cb
.buffer_size
= paramBytes
;
93 if (ST_DEBUG
& DEBUG_CONSTANTS
) {
94 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
95 __FUNCTION__
, shader_type
, params
->NumParameters
,
97 _mesa_print_parameter_list(params
);
100 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, &cb
);
101 pipe_resource_reference(&cb
.buffer
, NULL
);
103 st
->state
.constants
[shader_type
].ptr
= params
->ParameterValues
;
104 st
->state
.constants
[shader_type
].size
= paramBytes
;
106 else if (st
->state
.constants
[shader_type
].ptr
) {
108 st
->state
.constants
[shader_type
].ptr
= NULL
;
109 st
->state
.constants
[shader_type
].size
= 0;
110 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, NULL
);
118 static void update_vs_constants(struct st_context
*st
)
120 struct st_vertex_program
*vp
= st
->vp
;
121 struct gl_program_parameter_list
*params
= vp
->Base
.Base
.Parameters
;
123 st_upload_constants( st
, params
, PIPE_SHADER_VERTEX
);
127 const struct st_tracked_state st_update_vs_constants
= {
128 "st_update_vs_constants", /* name */
130 _NEW_PROGRAM_CONSTANTS
, /* mesa */
131 ST_NEW_VERTEX_PROGRAM
, /* st */
133 update_vs_constants
/* update */
141 static void update_fs_constants(struct st_context
*st
)
143 struct st_fragment_program
*fp
= st
->fp
;
144 struct gl_program_parameter_list
*params
= fp
->Base
.Base
.Parameters
;
146 st_upload_constants( st
, params
, PIPE_SHADER_FRAGMENT
);
150 const struct st_tracked_state st_update_fs_constants
= {
151 "st_update_fs_constants", /* name */
153 _NEW_PROGRAM_CONSTANTS
, /* mesa */
154 ST_NEW_FRAGMENT_PROGRAM
, /* st */
156 update_fs_constants
/* update */
161 static void update_gs_constants(struct st_context
*st
)
163 struct st_geometry_program
*gp
= st
->gp
;
164 struct gl_program_parameter_list
*params
;
167 params
= gp
->Base
.Base
.Parameters
;
168 st_upload_constants( st
, params
, PIPE_SHADER_GEOMETRY
);
172 const struct st_tracked_state st_update_gs_constants
= {
173 "st_update_gs_constants", /* name */
175 _NEW_PROGRAM_CONSTANTS
, /* mesa */
176 ST_NEW_GEOMETRY_PROGRAM
, /* st */
178 update_gs_constants
/* update */
181 static void st_bind_ubos(struct st_context
*st
,
182 struct gl_shader
*shader
,
183 unsigned shader_type
)
186 struct pipe_constant_buffer cb
= { 0 };
191 for (i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
192 struct gl_uniform_buffer_binding
*binding
;
193 struct st_buffer_object
*st_obj
;
195 binding
= &st
->ctx
->UniformBufferBindings
[shader
->UniformBlocks
[i
].Binding
];
196 st_obj
= st_buffer_object(binding
->BufferObject
);
197 pipe_resource_reference(&cb
.buffer
, st_obj
->buffer
);
199 cb
.buffer_size
= st_obj
->buffer
->width0
- binding
->Offset
;
201 cso_set_constant_buffer(st
->cso_context
, shader_type
, 1 + i
, &cb
);
202 pipe_resource_reference(&cb
.buffer
, NULL
);
206 static void bind_vs_ubos(struct st_context
*st
)
208 struct gl_shader_program
*prog
= st
->ctx
->Shader
.CurrentVertexProgram
;
213 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_VERTEX
], PIPE_SHADER_VERTEX
);
216 const struct st_tracked_state st_bind_vs_ubos
= {
219 (_NEW_PROGRAM
| _NEW_BUFFER_OBJECT
),
220 ST_NEW_VERTEX_PROGRAM
,
225 static void bind_fs_ubos(struct st_context
*st
)
227 struct gl_shader_program
*prog
= st
->ctx
->Shader
.CurrentFragmentProgram
;
232 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
], PIPE_SHADER_FRAGMENT
);
236 const struct st_tracked_state st_bind_fs_ubos
= {
239 (_NEW_PROGRAM
| _NEW_BUFFER_OBJECT
),
240 ST_NEW_FRAGMENT_PROGRAM
,