1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
34 #include "main/imports.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "main/shaderapi.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
55 void st_upload_constants(struct st_context
*st
, struct gl_program
*prog
)
57 gl_shader_stage stage
= prog
->info
.stage
;
58 struct gl_program_parameter_list
*params
= prog
->Parameters
;
59 enum pipe_shader_type shader_type
= pipe_shader_type_from_mesa(stage
);
61 assert(shader_type
== PIPE_SHADER_VERTEX
||
62 shader_type
== PIPE_SHADER_FRAGMENT
||
63 shader_type
== PIPE_SHADER_GEOMETRY
||
64 shader_type
== PIPE_SHADER_TESS_CTRL
||
65 shader_type
== PIPE_SHADER_TESS_EVAL
||
66 shader_type
== PIPE_SHADER_COMPUTE
);
68 /* update the ATI constants before rendering */
69 if (shader_type
== PIPE_SHADER_FRAGMENT
&& st
->fp
->ati_fs
) {
70 struct ati_fragment_shader
*ati_fs
= st
->fp
->ati_fs
;
73 for (c
= 0; c
< MAX_NUM_FRAGMENT_CONSTANTS_ATI
; c
++) {
74 if (ati_fs
->LocalConstDef
& (1 << c
))
75 memcpy(params
->ParameterValues
[c
],
76 ati_fs
->Constants
[c
], sizeof(GLfloat
) * 4);
78 memcpy(params
->ParameterValues
[c
],
79 st
->ctx
->ATIFragmentShader
.GlobalConstants
[c
], sizeof(GLfloat
) * 4);
83 /* Make all bindless samplers/images bound texture/image units resident in
86 st_make_bound_samplers_resident(st
, prog
);
87 st_make_bound_images_resident(st
, prog
);
89 /* update constants */
90 if (params
&& params
->NumParameters
) {
91 struct pipe_constant_buffer cb
;
92 const uint paramBytes
= params
->NumParameters
* sizeof(GLfloat
) * 4;
94 /* Update the constants which come from fixed-function state, such as
95 * transformation matrices, fog factors, etc. The rest of the values in
96 * the parameters list are explicitly set by the user with glUniform,
97 * glProgramParameter(), etc.
99 if (params
->StateFlags
)
100 _mesa_load_state_parameters(st
->ctx
, params
);
102 _mesa_shader_write_subroutine_indices(st
->ctx
, stage
);
104 /* We always need to get a new buffer, to keep the drivers simple and
105 * avoid gratuitous rendering synchronization.
106 * Let's use a user buffer to avoid an unnecessary copy.
108 if (!st
->has_user_constbuf
) {
110 cb
.user_buffer
= NULL
;
111 u_upload_data(st
->pipe
->const_uploader
, 0, paramBytes
,
112 st
->ctx
->Const
.UniformBufferOffsetAlignment
,
113 params
->ParameterValues
, &cb
.buffer_offset
, &cb
.buffer
);
114 u_upload_unmap(st
->pipe
->const_uploader
);
117 cb
.user_buffer
= params
->ParameterValues
;
118 cb
.buffer_offset
= 0;
120 cb
.buffer_size
= paramBytes
;
122 if (ST_DEBUG
& DEBUG_CONSTANTS
) {
123 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
124 __func__
, shader_type
, params
->NumParameters
,
126 _mesa_print_parameter_list(params
);
129 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, &cb
);
130 pipe_resource_reference(&cb
.buffer
, NULL
);
132 st
->state
.constants
[shader_type
].ptr
= params
->ParameterValues
;
133 st
->state
.constants
[shader_type
].size
= paramBytes
;
135 else if (st
->state
.constants
[shader_type
].ptr
) {
137 st
->state
.constants
[shader_type
].ptr
= NULL
;
138 st
->state
.constants
[shader_type
].size
= 0;
139 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, NULL
);
147 void st_update_vs_constants(struct st_context
*st
)
149 st_upload_constants(st
, &st
->vp
->Base
);
155 void st_update_fs_constants(struct st_context
*st
)
157 st_upload_constants(st
, &st
->fp
->Base
);
163 void st_update_gs_constants(struct st_context
*st
)
165 struct st_common_program
*gp
= st
->gp
;
168 st_upload_constants(st
, &gp
->Base
);
171 /* Tessellation control shader:
173 void st_update_tcs_constants(struct st_context
*st
)
175 struct st_common_program
*tcp
= st
->tcp
;
178 st_upload_constants(st
, &tcp
->Base
);
181 /* Tessellation evaluation shader:
183 void st_update_tes_constants(struct st_context
*st
)
185 struct st_common_program
*tep
= st
->tep
;
188 st_upload_constants(st
, &tep
->Base
);
193 void st_update_cs_constants(struct st_context
*st
)
195 struct st_compute_program
*cp
= st
->cp
;
198 st_upload_constants(st
, &cp
->Base
);
201 static void st_bind_ubos(struct st_context
*st
, struct gl_program
*prog
,
202 unsigned shader_type
)
205 struct pipe_constant_buffer cb
= { 0 };
210 for (i
= 0; i
< prog
->info
.num_ubos
; i
++) {
211 struct gl_uniform_buffer_binding
*binding
;
212 struct st_buffer_object
*st_obj
;
215 &st
->ctx
->UniformBufferBindings
[prog
->sh
.UniformBlocks
[i
]->Binding
];
216 st_obj
= st_buffer_object(binding
->BufferObject
);
218 cb
.buffer
= st_obj
->buffer
;
221 cb
.buffer_offset
= binding
->Offset
;
222 cb
.buffer_size
= cb
.buffer
->width0
- binding
->Offset
;
224 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
225 * Take the minimum just to be sure.
227 if (!binding
->AutomaticSize
)
228 cb
.buffer_size
= MIN2(cb
.buffer_size
, (unsigned) binding
->Size
);
231 cb
.buffer_offset
= 0;
235 cso_set_constant_buffer(st
->cso_context
, shader_type
, 1 + i
, &cb
);
239 void st_bind_vs_ubos(struct st_context
*st
)
241 struct gl_program
*prog
=
242 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
244 st_bind_ubos(st
, prog
, PIPE_SHADER_VERTEX
);
247 void st_bind_fs_ubos(struct st_context
*st
)
249 struct gl_program
*prog
=
250 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
252 st_bind_ubos(st
, prog
, PIPE_SHADER_FRAGMENT
);
255 void st_bind_gs_ubos(struct st_context
*st
)
257 struct gl_program
*prog
=
258 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
260 st_bind_ubos(st
, prog
, PIPE_SHADER_GEOMETRY
);
263 void st_bind_tcs_ubos(struct st_context
*st
)
265 struct gl_program
*prog
=
266 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
268 st_bind_ubos(st
, prog
, PIPE_SHADER_TESS_CTRL
);
271 void st_bind_tes_ubos(struct st_context
*st
)
273 struct gl_program
*prog
=
274 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
276 st_bind_ubos(st
, prog
, PIPE_SHADER_TESS_EVAL
);
279 void st_bind_cs_ubos(struct st_context
*st
)
281 struct gl_program
*prog
=
282 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
284 st_bind_ubos(st
, prog
, PIPE_SHADER_COMPUTE
);