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