i965: Move VS pull constant upload to emit() time.
[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 "program/prog_parameter.h"
34
35 #include "brw_context.h"
36 #include "brw_state.h"
37
38 /* Creates a new VS constant buffer reflecting the current VS program's
39 * constants, if needed by the VS program.
40 *
41 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
42 * state atom.
43 */
44 static void
45 brw_upload_vs_pull_constants(struct brw_context *brw)
46 {
47 struct gl_context *ctx = &brw->intel.ctx;
48 struct intel_context *intel = &brw->intel;
49 /* BRW_NEW_VERTEX_PROGRAM */
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 int i;
54
55 if (vp->program.IsNVProgram)
56 _mesa_load_tracked_matrices(ctx);
57
58 /* Updates the ParamaterValues[i] pointers for all parameters of the
59 * basic type of PROGRAM_STATE_VAR.
60 */
61 _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
62
63 /* CACHE_NEW_VS_PROG */
64 if (!brw->vs.prog_data->nr_pull_params) {
65 if (brw->vs.const_bo) {
66 drm_intel_bo_unreference(brw->vs.const_bo);
67 brw->vs.const_bo = NULL;
68 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
69 }
70 return;
71 }
72
73 /* _NEW_PROGRAM_CONSTANTS */
74 drm_intel_bo_unreference(brw->vs.const_bo);
75 brw->vs.const_bo = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
76 brw->vs.prog_data->nr_pull_params * 4,
77 64);
78
79 drm_intel_gem_bo_map_gtt(brw->vs.const_bo);
80 for (i = 0; i < brw->vs.prog_data->nr_pull_params; i++) {
81 memcpy(brw->vs.const_bo->virtual + i * 4,
82 brw->vs.prog_data->pull_param[i],
83 4);
84 }
85
86 if (0) {
87 for (i = 0; i < params->NumParameters; i++) {
88 float *row = (float *)brw->vs.const_bo->virtual + i * 4;
89 printf("vs const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
90 i, row[0], row[1], row[2], row[3]);
91 }
92 }
93
94 drm_intel_gem_bo_unmap_gtt(brw->vs.const_bo);
95 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
96 }
97
98 const struct brw_tracked_state brw_vs_constants = {
99 .dirty = {
100 .mesa = (_NEW_PROGRAM_CONSTANTS),
101 .brw = (BRW_NEW_VERTEX_PROGRAM),
102 .cache = CACHE_NEW_VS_PROG,
103 },
104 .emit = brw_upload_vs_pull_constants,
105 };
106
107 /**
108 * Update the surface state for a VS constant buffer.
109 *
110 * Sets brw->vs.surf_bo[surf] and brw->vp->const_buffer.
111 */
112 static void
113 brw_update_vs_constant_surface( struct gl_context *ctx,
114 GLuint surf)
115 {
116 struct brw_context *brw = brw_context(ctx);
117 struct intel_context *intel = &brw->intel;
118 struct brw_vertex_program *vp =
119 (struct brw_vertex_program *) brw->vertex_program;
120 const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
121
122 assert(surf == 0);
123
124 /* If there's no constant buffer, then no surface BO is needed to point at
125 * it.
126 */
127 if (brw->vs.const_bo == NULL) {
128 brw->vs.surf_offset[surf] = 0;
129 return;
130 }
131
132 if (intel->gen >= 7) {
133 gen7_create_constant_surface(brw, brw->vs.const_bo, params->NumParameters,
134 &brw->vs.surf_offset[surf]);
135 } else {
136 brw_create_constant_surface(brw, brw->vs.const_bo, params->NumParameters,
137 &brw->vs.surf_offset[surf]);
138 }
139 }
140
141 /**
142 * Vertex shader surfaces (constant buffer).
143 *
144 * This consumes the state updates for the constant buffer needing
145 * to be updated, and produces BRW_NEW_NR_VS_SURFACES for the VS unit and
146 * CACHE_NEW_SURF_BIND for the binding table upload.
147 */
148 static void
149 brw_upload_vs_surfaces(struct brw_context *brw)
150 {
151 struct gl_context *ctx = &brw->intel.ctx;
152 uint32_t *bind;
153 int i;
154 int nr_surfaces = 0;
155
156 /* BRW_NEW_VS_CONSTBUF */
157 if (brw->vs.const_bo) {
158 nr_surfaces = 1;
159 brw_update_vs_constant_surface(ctx, SURF_INDEX_VERT_CONST_BUFFER);
160 }
161
162 if (nr_surfaces != 0) {
163 bind = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
164 sizeof(uint32_t) * nr_surfaces,
165 32, &brw->vs.bind_bo_offset);
166
167 for (i = 0; i < nr_surfaces; i++) {
168 /* BRW_NEW_VS_CONSTBUF */
169 bind[i] = brw->vs.surf_offset[i];
170 }
171 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
172 } else {
173 if (brw->vs.bind_bo_offset) {
174 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
175 brw->vs.bind_bo_offset = 0;
176 }
177 }
178
179 if (brw->vs.nr_surfaces != nr_surfaces) {
180 brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
181 brw->vs.nr_surfaces = nr_surfaces;
182 }
183 }
184
185 const struct brw_tracked_state brw_vs_surfaces = {
186 .dirty = {
187 .mesa = 0,
188 .brw = (BRW_NEW_VS_CONSTBUF |
189 BRW_NEW_NR_VS_SURFACES |
190 BRW_NEW_BATCH),
191 .cache = 0
192 },
193 .emit = brw_upload_vs_surfaces,
194 };