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