brw_vs_surface_state.c revision e42b156fac6deed21328b89192faca6f4fe904f1
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/texstore.h"
34#include "shader/prog_parameter.h"
35
36#include "brw_context.h"
37#include "brw_state.h"
38#include "brw_defines.h"
39
40/* Creates a new VS constant buffer reflecting the current VS program's
41 * constants, if needed by the VS program.
42 *
43 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
44 * state atom.
45 */
46static drm_intel_bo *
47brw_vs_update_constant_buffer(struct brw_context *brw)
48{
49   struct intel_context *intel = &brw->intel;
50   struct brw_vertex_program *vp =
51      (struct brw_vertex_program *) brw->vertex_program;
52   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
53   const int size = params->NumParameters * 4 * sizeof(GLfloat);
54   drm_intel_bo *const_buffer;
55   int i;
56
57   /* BRW_NEW_VERTEX_PROGRAM */
58   if (!vp->use_const_buffer)
59      return NULL;
60
61   const_buffer = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
62				     size, 64);
63
64   /* _NEW_PROGRAM_CONSTANTS */
65
66   /* Updates the ParamaterValues[i] pointers for all parameters of the
67    * basic type of PROGRAM_STATE_VAR.
68    */
69   _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
70
71   for (i = 0; i < params->NumParameters; i++) {
72      dri_bo_subdata(const_buffer, i * 4 * sizeof(float), 4 * sizeof(float),
73		     params->ParameterValues[i]);
74   }
75
76   return const_buffer;
77}
78
79/**
80 * Update the surface state for a VS constant buffer.
81 *
82 * Sets brw->vs.surf_bo[surf] and brw->vp->const_buffer.
83 */
84static void
85brw_update_vs_constant_surface( GLcontext *ctx,
86                                GLuint surf)
87{
88   struct brw_context *brw = brw_context(ctx);
89   struct brw_surface_key key;
90   struct brw_vertex_program *vp =
91      (struct brw_vertex_program *) brw->vertex_program;
92   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
93
94   assert(surf == 0);
95
96   /* If we're in this state update atom, we need to update VS constants, so
97    * free the old buffer and create a new one for the new contents.
98    */
99   dri_bo_unreference(vp->const_buffer);
100   vp->const_buffer = brw_vs_update_constant_buffer(brw);
101
102   /* If there's no constant buffer, then no surface BO is needed to point at
103    * it.
104    */
105   if (vp->const_buffer == 0) {
106      drm_intel_bo_unreference(brw->vs.surf_bo[surf]);
107      brw->vs.surf_bo[surf] = NULL;
108      return;
109   }
110
111   memset(&key, 0, sizeof(key));
112
113   key.format = MESA_FORMAT_RGBA_FLOAT32;
114   key.internal_format = GL_RGBA;
115   key.bo = vp->const_buffer;
116   key.depthmode = GL_NONE;
117   key.pitch = params->NumParameters;
118   key.width = params->NumParameters;
119   key.height = 1;
120   key.depth = 1;
121   key.cpp = 16;
122
123   /*
124   printf("%s:\n", __FUNCTION__);
125   printf("  width %d  height %d  depth %d  cpp %d  pitch %d\n",
126          key.width, key.height, key.depth, key.cpp, key.pitch);
127   */
128
129   drm_intel_bo_unreference(brw->vs.surf_bo[surf]);
130   brw->vs.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
131                                            BRW_SS_SURFACE,
132                                            &key, sizeof(key),
133                                            &key.bo, key.bo ? 1 : 0,
134                                            NULL);
135   if (brw->vs.surf_bo[surf] == NULL) {
136      brw->vs.surf_bo[surf] = brw_create_constant_surface(brw, &key);
137   }
138}
139
140
141/**
142 * Constructs the binding table for the VS surface state.
143 */
144static dri_bo *
145brw_vs_get_binding_table(struct brw_context *brw)
146{
147   dri_bo *bind_bo;
148
149   bind_bo = brw_search_cache(&brw->surface_cache, BRW_SS_SURF_BIND,
150			      NULL, 0,
151			      brw->vs.surf_bo, BRW_VS_MAX_SURF,
152			      NULL);
153
154   if (bind_bo == NULL) {
155      GLuint data_size = BRW_VS_MAX_SURF * sizeof(GLuint);
156      uint32_t *data = malloc(data_size);
157      int i;
158
159      for (i = 0; i < BRW_VS_MAX_SURF; i++)
160         if (brw->vs.surf_bo[i])
161            data[i] = brw->vs.surf_bo[i]->offset;
162         else
163            data[i] = 0;
164
165      bind_bo = brw_upload_cache( &brw->surface_cache, BRW_SS_SURF_BIND,
166				  NULL, 0,
167				  brw->vs.surf_bo, BRW_VS_MAX_SURF,
168				  data, data_size,
169				  NULL, NULL);
170
171      /* Emit binding table relocations to surface state */
172      for (i = 0; i < BRW_VS_MAX_SURF; i++) {
173	 if (brw->vs.surf_bo[i] != NULL) {
174	    /* The presumed offsets were set in the data values for
175	     * brw_upload_cache.
176	     */
177	    drm_intel_bo_emit_reloc(bind_bo, i * 4,
178				    brw->vs.surf_bo[i], 0,
179				    I915_GEM_DOMAIN_INSTRUCTION, 0);
180	 }
181      }
182
183      free(data);
184   }
185
186   return bind_bo;
187}
188
189/**
190 * Vertex shader surfaces (constant buffer).
191 *
192 * This consumes the state updates for the constant buffer needing
193 * to be updated, and produces BRW_NEW_NR_VS_SURFACES for the VS unit and
194 * CACHE_NEW_SURF_BIND for the binding table upload.
195 */
196static void prepare_vs_surfaces(struct brw_context *brw )
197{
198   GLcontext *ctx = &brw->intel.ctx;
199   int i;
200   int nr_surfaces = 0;
201
202   brw_update_vs_constant_surface(ctx, SURF_INDEX_VERT_CONST_BUFFER);
203
204   for (i = 0; i < BRW_VS_MAX_SURF; i++) {
205      if (brw->vs.surf_bo[i] != NULL) {
206	 nr_surfaces = i + 1;
207      }
208   }
209
210   if (brw->vs.nr_surfaces != nr_surfaces) {
211      brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
212      brw->vs.nr_surfaces = nr_surfaces;
213   }
214
215   /* Note that we don't end up updating the bind_bo if we don't have a
216    * surface to be pointing at.  This should be relatively harmless, as it
217    * just slightly increases our working set size.
218    */
219   if (brw->vs.nr_surfaces != 0) {
220      dri_bo_unreference(brw->vs.bind_bo);
221      brw->vs.bind_bo = brw_vs_get_binding_table(brw);
222   }
223}
224
225const struct brw_tracked_state brw_vs_surfaces = {
226   .dirty = {
227      .mesa = (_NEW_PROGRAM_CONSTANTS),
228      .brw = (BRW_NEW_VERTEX_PROGRAM),
229      .cache = 0
230   },
231   .prepare = prepare_vs_surfaces,
232};
233
234
235
236