i965: Update comments in brw_vec4_upload_binding_table().
[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
39 void
40 brw_upload_vec4_pull_constants(struct brw_context *brw,
41 GLbitfield brw_new_constbuf,
42 const struct gl_program *prog,
43 struct brw_stage_state *stage_state,
44 const struct brw_vec4_prog_data *prog_data)
45 {
46 int i;
47
48 /* Updates the ParamaterValues[i] pointers for all parameters of the
49 * basic type of PROGRAM_STATE_VAR.
50 */
51 _mesa_load_state_parameters(&brw->ctx, prog->Parameters);
52
53 if (!prog_data->nr_pull_params) {
54 if (stage_state->const_bo) {
55 drm_intel_bo_unreference(stage_state->const_bo);
56 stage_state->const_bo = NULL;
57 stage_state->surf_offset[SURF_INDEX_VEC4_CONST_BUFFER] = 0;
58 brw->state.dirty.brw |= brw_new_constbuf;
59 }
60 return;
61 }
62
63 /* _NEW_PROGRAM_CONSTANTS */
64 drm_intel_bo_unreference(stage_state->const_bo);
65 uint32_t size = prog_data->nr_pull_params * 4;
66 stage_state->const_bo = drm_intel_bo_alloc(brw->bufmgr, "vec4_const_buffer",
67 size, 64);
68
69 drm_intel_gem_bo_map_gtt(stage_state->const_bo);
70
71 for (i = 0; i < prog_data->nr_pull_params; i++) {
72 memcpy(stage_state->const_bo->virtual + i * 4,
73 prog_data->pull_param[i],
74 4);
75 }
76
77 if (0) {
78 for (i = 0; i < ALIGN(prog_data->nr_pull_params, 4) / 4; i++) {
79 float *row = (float *)stage_state->const_bo->virtual + i * 4;
80 printf("const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
81 i, row[0], row[1], row[2], row[3]);
82 }
83 }
84
85 drm_intel_gem_bo_unmap_gtt(stage_state->const_bo);
86
87 const int surf = SURF_INDEX_VEC4_CONST_BUFFER;
88 brw->vtbl.create_constant_surface(brw, stage_state->const_bo, 0, size,
89 &stage_state->surf_offset[surf], false);
90
91 brw->state.dirty.brw |= brw_new_constbuf;
92 }
93
94
95 /* Creates a new VS constant buffer reflecting the current VS program's
96 * constants, if needed by the VS program.
97 *
98 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
99 * state atom.
100 */
101 static void
102 brw_upload_vs_pull_constants(struct brw_context *brw)
103 {
104 struct brw_stage_state *stage_state = &brw->vs.base;
105
106 /* BRW_NEW_VERTEX_PROGRAM */
107 struct brw_vertex_program *vp =
108 (struct brw_vertex_program *) brw->vertex_program;
109
110 /* CACHE_NEW_VS_PROG */
111 const struct brw_vec4_prog_data *prog_data = &brw->vs.prog_data->base;
112
113 /* _NEW_PROGRAM_CONSTANTS */
114 brw_upload_vec4_pull_constants(brw, BRW_NEW_VS_CONSTBUF, &vp->program.Base,
115 stage_state, prog_data);
116 }
117
118 const struct brw_tracked_state brw_vs_pull_constants = {
119 .dirty = {
120 .mesa = (_NEW_PROGRAM_CONSTANTS),
121 .brw = (BRW_NEW_BATCH | BRW_NEW_VERTEX_PROGRAM),
122 .cache = CACHE_NEW_VS_PROG,
123 },
124 .emit = brw_upload_vs_pull_constants,
125 };
126
127 static void
128 brw_upload_vs_ubo_surfaces(struct brw_context *brw)
129 {
130 struct brw_stage_state *stage_state = &brw->vs.base;
131
132 struct gl_context *ctx = &brw->ctx;
133 /* _NEW_PROGRAM */
134 struct gl_shader_program *prog = ctx->Shader.CurrentVertexProgram;
135
136 if (!prog)
137 return;
138
139 brw_upload_ubo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
140 &stage_state->surf_offset[SURF_INDEX_VEC4_UBO(0)]);
141 }
142
143 const struct brw_tracked_state brw_vs_ubo_surfaces = {
144 .dirty = {
145 .mesa = _NEW_PROGRAM,
146 .brw = BRW_NEW_BATCH | BRW_NEW_UNIFORM_BUFFER,
147 .cache = 0,
148 },
149 .emit = brw_upload_vs_ubo_surfaces,
150 };
151
152
153 void
154 brw_vec4_upload_binding_table(struct brw_context *brw,
155 GLbitfield brw_new_binding_table,
156 struct brw_stage_state *stage_state,
157 const struct brw_vec4_prog_data *prog_data)
158 {
159 uint32_t *bind;
160 int i;
161
162 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
163 gen7_create_shader_time_surface(brw, &stage_state->surf_offset[SURF_INDEX_VEC4_SHADER_TIME]);
164 }
165
166 /* If there are no surfaces, skip making the binding table altogether. */
167 const unsigned entries = prog_data->binding_table_size;
168 if (entries == 0) {
169 if (stage_state->bind_bo_offset != 0) {
170 brw->state.dirty.brw |= brw_new_binding_table;
171 stage_state->bind_bo_offset = 0;
172 }
173 return;
174 }
175
176 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
177 sizeof(uint32_t) * entries,
178 32, &stage_state->bind_bo_offset);
179
180 /* BRW_NEW_SURFACES and BRW_NEW_*_CONSTBUF */
181 for (i = 0; i < entries; i++) {
182 bind[i] = stage_state->surf_offset[i];
183 }
184
185 brw->state.dirty.brw |= brw_new_binding_table;
186 }
187
188
189 /**
190 * Constructs the binding table for the WM surface state, which maps unit
191 * numbers to surface state objects.
192 */
193 static void
194 brw_vs_upload_binding_table(struct brw_context *brw)
195 {
196 struct brw_stage_state *stage_state = &brw->vs.base;
197 /* CACHE_NEW_VS_PROG */
198 const struct brw_vec4_prog_data *prog_data = &brw->vs.prog_data->base;
199
200 /* BRW_NEW_SURFACES and BRW_NEW_VS_CONSTBUF */
201 brw_vec4_upload_binding_table(brw, BRW_NEW_VS_BINDING_TABLE, stage_state,
202 prog_data);
203 }
204
205 const struct brw_tracked_state brw_vs_binding_table = {
206 .dirty = {
207 .mesa = 0,
208 .brw = (BRW_NEW_BATCH |
209 BRW_NEW_VS_CONSTBUF |
210 BRW_NEW_SURFACES),
211 .cache = CACHE_NEW_VS_PROG
212 },
213 .emit = brw_vs_upload_binding_table,
214 };