brw_vs_surface_state.c revision 1220aba99bc78290bb89ade649719508e3031e4b
1/*
2 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28  * Authors:
29  *   Keith Whitwell <keith@tungstengraphics.com>
30  */
31
32#include "main/mtypes.h"
33#include "main/texformat.h"
34#include "main/texstore.h"
35#include "shader/prog_parameter.h"
36
37#include "brw_context.h"
38#include "brw_state.h"
39#include "brw_defines.h"
40
41/* Creates a new VS constant buffer reflecting the current VS program's
42 * constants, if needed by the VS program.
43 *
44 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
45 * state atom.
46 */
47static drm_intel_bo *
48brw_vs_update_constant_buffer(struct brw_context *brw)
49{
50   struct intel_context *intel = &brw->intel;
51   struct brw_vertex_program *vp =
52      (struct brw_vertex_program *) brw->vertex_program;
53   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
54   const int size = params->NumParameters * 4 * sizeof(GLfloat);
55   drm_intel_bo *const_buffer;
56   int i;
57
58   /* BRW_NEW_VERTEX_PROGRAM */
59   if (!vp->use_const_buffer)
60      return NULL;
61
62   const_buffer = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
63				     size, 64);
64
65   /* _NEW_PROGRAM_CONSTANTS */
66
67   /* Updates the ParamaterValues[i] pointers for all parameters of the
68    * basic type of PROGRAM_STATE_VAR.
69    */
70   _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
71
72   for (i = 0; i < params->NumParameters; i++) {
73      dri_bo_subdata(const_buffer, i * 4 * sizeof(float), 4 * sizeof(float),
74		     params->ParameterValues[i]);
75   }
76
77   return const_buffer;
78}
79
80/**
81 * Update the surface state for a VS constant buffer.
82 *
83 * Sets brw->vs.surf_bo[surf] and brw->vp->const_buffer.
84 */
85static void
86brw_update_vs_constant_surface( GLcontext *ctx,
87                                GLuint surf)
88{
89   struct brw_context *brw = brw_context(ctx);
90   struct brw_surface_key key;
91   struct brw_vertex_program *vp =
92      (struct brw_vertex_program *) brw->vertex_program;
93   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
94
95   assert(surf == 0);
96
97   /* If we're in this state update atom, we need to update VS constants, so
98    * free the old buffer and create a new one for the new contents.
99    */
100   dri_bo_unreference(vp->const_buffer);
101   vp->const_buffer = brw_vs_update_constant_buffer(brw);
102
103   /* If there's no constant buffer, then no surface BO is needed to point at
104    * it.
105    */
106   if (vp->const_buffer == 0) {
107      drm_intel_bo_unreference(brw->vs.surf_bo[surf]);
108      brw->vs.surf_bo[surf] = NULL;
109      return;
110   }
111
112   memset(&key, 0, sizeof(key));
113
114   key.format = MESA_FORMAT_RGBA_FLOAT32;
115   key.internal_format = GL_RGBA;
116   key.bo = vp->const_buffer;
117   key.depthmode = GL_NONE;
118   key.pitch = params->NumParameters;
119   key.width = params->NumParameters;
120   key.height = 1;
121   key.depth = 1;
122   key.cpp = 16;
123
124   /*
125   printf("%s:\n", __FUNCTION__);
126   printf("  width %d  height %d  depth %d  cpp %d  pitch %d\n",
127          key.width, key.height, key.depth, key.cpp, key.pitch);
128   */
129
130   drm_intel_bo_unreference(brw->vs.surf_bo[surf]);
131   brw->vs.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
132                                            BRW_SS_SURFACE,
133                                            &key, sizeof(key),
134                                            &key.bo, key.bo ? 1 : 0,
135                                            NULL);
136   if (brw->vs.surf_bo[surf] == NULL) {
137      brw->vs.surf_bo[surf] = brw_create_constant_surface(brw, &key);
138   }
139}
140
141
142/**
143 * Constructs the binding table for the VS surface state.
144 */
145static dri_bo *
146brw_vs_get_binding_table(struct brw_context *brw)
147{
148   dri_bo *bind_bo;
149
150   bind_bo = brw_search_cache(&brw->surface_cache, BRW_SS_SURF_BIND,
151			      NULL, 0,
152			      brw->vs.surf_bo, BRW_VS_MAX_SURF,
153			      NULL);
154
155   if (bind_bo == NULL) {
156      GLuint data_size = BRW_VS_MAX_SURF * sizeof(GLuint);
157      uint32_t *data = malloc(data_size);
158      int i;
159
160      for (i = 0; i < BRW_VS_MAX_SURF; i++)
161         if (brw->vs.surf_bo[i])
162            data[i] = brw->vs.surf_bo[i]->offset;
163         else
164            data[i] = 0;
165
166      bind_bo = brw_upload_cache( &brw->surface_cache, BRW_SS_SURF_BIND,
167				  NULL, 0,
168				  brw->vs.surf_bo, BRW_VS_MAX_SURF,
169				  data, data_size,
170				  NULL, NULL);
171
172      /* Emit binding table relocations to surface state */
173      for (i = 0; i < BRW_VS_MAX_SURF; i++) {
174	 if (brw->vs.surf_bo[i] != NULL) {
175	    /* The presumed offsets were set in the data values for
176	     * brw_upload_cache.
177	     */
178	    drm_intel_bo_emit_reloc(bind_bo, i * 4,
179				    brw->vs.surf_bo[i], 0,
180				    I915_GEM_DOMAIN_INSTRUCTION, 0);
181	 }
182      }
183
184      free(data);
185   }
186
187   return bind_bo;
188}
189
190/**
191 * Vertex shader surfaces (constant buffer).
192 *
193 * This consumes the state updates for the constant buffer needing
194 * to be updated, and produces BRW_NEW_NR_VS_SURFACES for the VS unit and
195 * CACHE_NEW_SURF_BIND for the binding table upload.
196 */
197static void prepare_vs_surfaces(struct brw_context *brw )
198{
199   GLcontext *ctx = &brw->intel.ctx;
200   int i;
201   int nr_surfaces = 0;
202
203   brw_update_vs_constant_surface(ctx, SURF_INDEX_VERT_CONST_BUFFER);
204
205   for (i = 0; i < BRW_VS_MAX_SURF; i++) {
206      if (brw->vs.surf_bo[i] != NULL) {
207	 nr_surfaces = i + 1;
208      }
209   }
210
211   if (brw->vs.nr_surfaces != nr_surfaces) {
212      brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
213      brw->vs.nr_surfaces = nr_surfaces;
214   }
215
216   /* Note that we don't end up updating the bind_bo if we don't have a
217    * surface to be pointing at.  This should be relatively harmless, as it
218    * just slightly increases our working set size.
219    */
220   if (brw->vs.nr_surfaces != 0) {
221      dri_bo_unreference(brw->vs.bind_bo);
222      brw->vs.bind_bo = brw_vs_get_binding_table(brw);
223   }
224}
225
226const struct brw_tracked_state brw_vs_surfaces = {
227   .dirty = {
228      .mesa = (_NEW_PROGRAM_CONSTANTS),
229      .brw = (BRW_NEW_VERTEX_PROGRAM),
230      .cache = 0
231   },
232   .prepare = prepare_vs_surfaces,
233};
234
235
236
237