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