1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/*
29bf215546Sopenharmony_ci * Authors:
30bf215546Sopenharmony_ci *   Keith Whitwell <keithw@vmware.com>
31bf215546Sopenharmony_ci *   Brian Paul
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "program/prog_parameter.h"
36bf215546Sopenharmony_ci#include "program/prog_print.h"
37bf215546Sopenharmony_ci#include "main/shaderapi.h"
38bf215546Sopenharmony_ci#include "pipe/p_context.h"
39bf215546Sopenharmony_ci#include "pipe/p_defines.h"
40bf215546Sopenharmony_ci#include "util/u_inlines.h"
41bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
42bf215546Sopenharmony_ci#include "cso_cache/cso_context.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#include "main/bufferobj.h"
45bf215546Sopenharmony_ci#include "st_debug.h"
46bf215546Sopenharmony_ci#include "st_context.h"
47bf215546Sopenharmony_ci#include "st_atom.h"
48bf215546Sopenharmony_ci#include "st_atom_constbuf.h"
49bf215546Sopenharmony_ci#include "st_program.h"
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* Unbinds the CB0 if it's not used by the current program to avoid leaving
52bf215546Sopenharmony_ci * dangling pointers to old (potentially deleted) shaders in the driver.
53bf215546Sopenharmony_ci */
54bf215546Sopenharmony_cistatic void
55bf215546Sopenharmony_cist_unbind_unused_cb0(struct st_context *st, enum pipe_shader_type shader_type)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   if (st->state.constbuf0_enabled_shader_mask & (1 << shader_type)) {
58bf215546Sopenharmony_ci      struct pipe_context *pipe = st->pipe;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci      pipe->set_constant_buffer(pipe, shader_type, 0, false, NULL);
61bf215546Sopenharmony_ci      st->state.constbuf0_enabled_shader_mask &= ~(1 << shader_type);
62bf215546Sopenharmony_ci   }
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci/**
66bf215546Sopenharmony_ci * Pass the given program parameters to the graphics pipe as a
67bf215546Sopenharmony_ci * constant buffer.
68bf215546Sopenharmony_ci */
69bf215546Sopenharmony_civoid
70bf215546Sopenharmony_cist_upload_constants(struct st_context *st, struct gl_program *prog, gl_shader_stage stage)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   enum pipe_shader_type shader_type = pipe_shader_type_from_mesa(stage);
73bf215546Sopenharmony_ci   if (!prog) {
74bf215546Sopenharmony_ci      st_unbind_unused_cb0(st, shader_type);
75bf215546Sopenharmony_ci      return;
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   struct gl_program_parameter_list *params = prog->Parameters;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   assert(shader_type == PIPE_SHADER_VERTEX ||
81bf215546Sopenharmony_ci          shader_type == PIPE_SHADER_FRAGMENT ||
82bf215546Sopenharmony_ci          shader_type == PIPE_SHADER_GEOMETRY ||
83bf215546Sopenharmony_ci          shader_type == PIPE_SHADER_TESS_CTRL ||
84bf215546Sopenharmony_ci          shader_type == PIPE_SHADER_TESS_EVAL ||
85bf215546Sopenharmony_ci          shader_type == PIPE_SHADER_COMPUTE);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   /* update the ATI constants before rendering */
88bf215546Sopenharmony_ci   if (shader_type == PIPE_SHADER_FRAGMENT && st->fp->ati_fs) {
89bf215546Sopenharmony_ci      struct ati_fragment_shader *ati_fs = st->fp->ati_fs;
90bf215546Sopenharmony_ci      unsigned c;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci      for (c = 0; c < MAX_NUM_FRAGMENT_CONSTANTS_ATI; c++) {
93bf215546Sopenharmony_ci         unsigned offset = params->Parameters[c].ValueOffset;
94bf215546Sopenharmony_ci         if (ati_fs->LocalConstDef & (1 << c))
95bf215546Sopenharmony_ci            memcpy(params->ParameterValues + offset,
96bf215546Sopenharmony_ci                   ati_fs->Constants[c], sizeof(GLfloat) * 4);
97bf215546Sopenharmony_ci         else
98bf215546Sopenharmony_ci            memcpy(params->ParameterValues + offset,
99bf215546Sopenharmony_ci                   st->ctx->ATIFragmentShader.GlobalConstants[c],
100bf215546Sopenharmony_ci                   sizeof(GLfloat) * 4);
101bf215546Sopenharmony_ci      }
102bf215546Sopenharmony_ci   }
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   /* Make all bindless samplers/images bound texture/image units resident in
105bf215546Sopenharmony_ci    * the context.
106bf215546Sopenharmony_ci    */
107bf215546Sopenharmony_ci   st_make_bound_samplers_resident(st, prog);
108bf215546Sopenharmony_ci   st_make_bound_images_resident(st, prog);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   /* update constants */
111bf215546Sopenharmony_ci   if (params && params->NumParameters) {
112bf215546Sopenharmony_ci      struct pipe_constant_buffer cb;
113bf215546Sopenharmony_ci      const uint paramBytes = params->NumParameterValues * sizeof(GLfloat);
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci      _mesa_shader_write_subroutine_indices(st->ctx, stage);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci      cb.buffer = NULL;
118bf215546Sopenharmony_ci      cb.user_buffer = NULL;
119bf215546Sopenharmony_ci      cb.buffer_offset = 0;
120bf215546Sopenharmony_ci      cb.buffer_size = paramBytes;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      if (st->prefer_real_buffer_in_constbuf0) {
123bf215546Sopenharmony_ci         struct pipe_context *pipe = st->pipe;
124bf215546Sopenharmony_ci         uint32_t *ptr;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci         const unsigned alignment = MAX2(
127bf215546Sopenharmony_ci            st->ctx->Const.UniformBufferOffsetAlignment, 64);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci         /* fetch_state always stores 4 components (16 bytes) per matrix row,
130bf215546Sopenharmony_ci          * but matrix rows are sometimes allocated partially, so add 12
131bf215546Sopenharmony_ci          * to compensate for the fetch_state defect.
132bf215546Sopenharmony_ci          */
133bf215546Sopenharmony_ci         u_upload_alloc(pipe->const_uploader, 0, paramBytes + 12,
134bf215546Sopenharmony_ci            alignment, &cb.buffer_offset, &cb.buffer, (void**)&ptr);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci         int uniform_bytes = params->UniformBytes;
137bf215546Sopenharmony_ci         if (uniform_bytes)
138bf215546Sopenharmony_ci            memcpy(ptr, params->ParameterValues, uniform_bytes);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci         /* Upload the constants which come from fixed-function state, such as
141bf215546Sopenharmony_ci          * transformation matrices, fog factors, etc.
142bf215546Sopenharmony_ci          */
143bf215546Sopenharmony_ci         if (params->StateFlags)
144bf215546Sopenharmony_ci            _mesa_upload_state_parameters(st->ctx, params, ptr);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci         u_upload_unmap(pipe->const_uploader);
147bf215546Sopenharmony_ci         pipe->set_constant_buffer(pipe, shader_type, 0, true, &cb);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci         /* Set inlinable constants. This is more involved because state
150bf215546Sopenharmony_ci          * parameters are uploaded directly above instead of being loaded
151bf215546Sopenharmony_ci          * into gl_program_parameter_list. The easiest way to get their values
152bf215546Sopenharmony_ci          * is to load them.
153bf215546Sopenharmony_ci          */
154bf215546Sopenharmony_ci         unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms;
155bf215546Sopenharmony_ci         if (num_inlinable_uniforms) {
156bf215546Sopenharmony_ci            uint32_t values[MAX_INLINABLE_UNIFORMS];
157bf215546Sopenharmony_ci            gl_constant_value *constbuf = params->ParameterValues;
158bf215546Sopenharmony_ci            bool loaded_state_vars = false;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci            for (unsigned i = 0; i < num_inlinable_uniforms; i++) {
161bf215546Sopenharmony_ci               unsigned dw_offset = prog->info.inlinable_uniform_dw_offsets[i];
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci               if (dw_offset * 4 >= uniform_bytes && !loaded_state_vars) {
164bf215546Sopenharmony_ci                  _mesa_load_state_parameters(st->ctx, params);
165bf215546Sopenharmony_ci                  loaded_state_vars = true;
166bf215546Sopenharmony_ci               }
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci               values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u;
169bf215546Sopenharmony_ci            }
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci            pipe->set_inlinable_constants(pipe, shader_type,
172bf215546Sopenharmony_ci                                          prog->info.num_inlinable_uniforms,
173bf215546Sopenharmony_ci                                          values);
174bf215546Sopenharmony_ci         }
175bf215546Sopenharmony_ci      } else {
176bf215546Sopenharmony_ci         struct pipe_context *pipe = st->pipe;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci         cb.user_buffer = params->ParameterValues;
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci         /* Update the constants which come from fixed-function state, such as
181bf215546Sopenharmony_ci          * transformation matrices, fog factors, etc.
182bf215546Sopenharmony_ci          */
183bf215546Sopenharmony_ci         if (params->StateFlags)
184bf215546Sopenharmony_ci            _mesa_load_state_parameters(st->ctx, params);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci         pipe->set_constant_buffer(pipe, shader_type, 0, false, &cb);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci         /* Set inlinable constants. */
189bf215546Sopenharmony_ci         unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms;
190bf215546Sopenharmony_ci         if (num_inlinable_uniforms) {
191bf215546Sopenharmony_ci            uint32_t values[MAX_INLINABLE_UNIFORMS];
192bf215546Sopenharmony_ci            gl_constant_value *constbuf = params->ParameterValues;
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci            for (unsigned i = 0; i < num_inlinable_uniforms; i++)
195bf215546Sopenharmony_ci               values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u;
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci            pipe->set_inlinable_constants(pipe, shader_type,
198bf215546Sopenharmony_ci                                          prog->info.num_inlinable_uniforms,
199bf215546Sopenharmony_ci                                          values);
200bf215546Sopenharmony_ci         }
201bf215546Sopenharmony_ci      }
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci      st->state.constbuf0_enabled_shader_mask |= 1 << shader_type;
204bf215546Sopenharmony_ci   } else {
205bf215546Sopenharmony_ci      st_unbind_unused_cb0(st, shader_type);
206bf215546Sopenharmony_ci   }
207bf215546Sopenharmony_ci}
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci/**
211bf215546Sopenharmony_ci * Vertex shader:
212bf215546Sopenharmony_ci */
213bf215546Sopenharmony_civoid
214bf215546Sopenharmony_cist_update_vs_constants(struct st_context *st)
215bf215546Sopenharmony_ci{
216bf215546Sopenharmony_ci   st_upload_constants(st, st->vp, MESA_SHADER_VERTEX);
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci/**
220bf215546Sopenharmony_ci * Fragment shader:
221bf215546Sopenharmony_ci */
222bf215546Sopenharmony_civoid
223bf215546Sopenharmony_cist_update_fs_constants(struct st_context *st)
224bf215546Sopenharmony_ci{
225bf215546Sopenharmony_ci   st_upload_constants(st, st->fp, MESA_SHADER_FRAGMENT);
226bf215546Sopenharmony_ci}
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci/* Geometry shader:
230bf215546Sopenharmony_ci */
231bf215546Sopenharmony_civoid
232bf215546Sopenharmony_cist_update_gs_constants(struct st_context *st)
233bf215546Sopenharmony_ci{
234bf215546Sopenharmony_ci   st_upload_constants(st, st->gp, MESA_SHADER_GEOMETRY);
235bf215546Sopenharmony_ci}
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci/* Tessellation control shader:
238bf215546Sopenharmony_ci */
239bf215546Sopenharmony_civoid
240bf215546Sopenharmony_cist_update_tcs_constants(struct st_context *st)
241bf215546Sopenharmony_ci{
242bf215546Sopenharmony_ci   st_upload_constants(st, st->tcp, MESA_SHADER_TESS_CTRL);
243bf215546Sopenharmony_ci}
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci/* Tessellation evaluation shader:
246bf215546Sopenharmony_ci */
247bf215546Sopenharmony_civoid
248bf215546Sopenharmony_cist_update_tes_constants(struct st_context *st)
249bf215546Sopenharmony_ci{
250bf215546Sopenharmony_ci   st_upload_constants(st, st->tep, MESA_SHADER_TESS_EVAL);
251bf215546Sopenharmony_ci}
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci/* Compute shader:
254bf215546Sopenharmony_ci */
255bf215546Sopenharmony_civoid
256bf215546Sopenharmony_cist_update_cs_constants(struct st_context *st)
257bf215546Sopenharmony_ci{
258bf215546Sopenharmony_ci   st_upload_constants(st, st->cp, MESA_SHADER_COMPUTE);
259bf215546Sopenharmony_ci}
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_cistatic void
262bf215546Sopenharmony_cist_bind_ubos(struct st_context *st, struct gl_program *prog,
263bf215546Sopenharmony_ci             enum pipe_shader_type shader_type)
264bf215546Sopenharmony_ci{
265bf215546Sopenharmony_ci   unsigned i;
266bf215546Sopenharmony_ci   struct pipe_constant_buffer cb = { 0 };
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci   if (!prog)
269bf215546Sopenharmony_ci      return;
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci   for (i = 0; i < prog->sh.NumUniformBlocks; i++) {
274bf215546Sopenharmony_ci      struct gl_buffer_binding *binding;
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci      binding =
277bf215546Sopenharmony_ci         &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci      cb.buffer = _mesa_get_bufferobj_reference(st->ctx, binding->BufferObject);
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci      if (cb.buffer) {
282bf215546Sopenharmony_ci         cb.buffer_offset = binding->Offset;
283bf215546Sopenharmony_ci         cb.buffer_size = cb.buffer->width0 - binding->Offset;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci         /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
286bf215546Sopenharmony_ci          * Take the minimum just to be sure.
287bf215546Sopenharmony_ci          */
288bf215546Sopenharmony_ci         if (!binding->AutomaticSize)
289bf215546Sopenharmony_ci            cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size);
290bf215546Sopenharmony_ci      }
291bf215546Sopenharmony_ci      else {
292bf215546Sopenharmony_ci         cb.buffer_offset = 0;
293bf215546Sopenharmony_ci         cb.buffer_size = 0;
294bf215546Sopenharmony_ci      }
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci      pipe->set_constant_buffer(pipe, shader_type, 1 + i, true, &cb);
297bf215546Sopenharmony_ci   }
298bf215546Sopenharmony_ci}
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_civoid
301bf215546Sopenharmony_cist_bind_vs_ubos(struct st_context *st)
302bf215546Sopenharmony_ci{
303bf215546Sopenharmony_ci   struct gl_program *prog =
304bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_VERTEX);
307bf215546Sopenharmony_ci}
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_civoid
310bf215546Sopenharmony_cist_bind_fs_ubos(struct st_context *st)
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci   struct gl_program *prog =
313bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_FRAGMENT);
316bf215546Sopenharmony_ci}
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_civoid
319bf215546Sopenharmony_cist_bind_gs_ubos(struct st_context *st)
320bf215546Sopenharmony_ci{
321bf215546Sopenharmony_ci   struct gl_program *prog =
322bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_GEOMETRY);
325bf215546Sopenharmony_ci}
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_civoid
328bf215546Sopenharmony_cist_bind_tcs_ubos(struct st_context *st)
329bf215546Sopenharmony_ci{
330bf215546Sopenharmony_ci   struct gl_program *prog =
331bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_TESS_CTRL);
334bf215546Sopenharmony_ci}
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_civoid
337bf215546Sopenharmony_cist_bind_tes_ubos(struct st_context *st)
338bf215546Sopenharmony_ci{
339bf215546Sopenharmony_ci   struct gl_program *prog =
340bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_TESS_EVAL);
343bf215546Sopenharmony_ci}
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_civoid
346bf215546Sopenharmony_cist_bind_cs_ubos(struct st_context *st)
347bf215546Sopenharmony_ci{
348bf215546Sopenharmony_ci   struct gl_program *prog =
349bf215546Sopenharmony_ci      st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   st_bind_ubos(st, prog, PIPE_SHADER_COMPUTE);
352bf215546Sopenharmony_ci}
353