i965: generalize brw_vs_pull_constants in preparation for GS.
[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 * Constructs the binding table for the WM surface state, which maps unit
154 * numbers to surface state objects.
155 */
156 static void
157 brw_vs_upload_binding_table(struct brw_context *brw)
158 {
159 struct brw_stage_state *stage_state = &brw->vs.base;
160 uint32_t *bind;
161 int i;
162
163 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
164 gen7_create_shader_time_surface(brw, &stage_state->surf_offset[SURF_INDEX_VEC4_SHADER_TIME]);
165 }
166
167 /* CACHE_NEW_VS_PROG: Skip making a binding table if we don't use textures or
168 * pull constants.
169 */
170 const unsigned entries = brw->vs.prog_data->base.binding_table_size;
171 if (entries == 0) {
172 if (stage_state->bind_bo_offset != 0) {
173 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
174 stage_state->bind_bo_offset = 0;
175 }
176 return;
177 }
178
179 /* Might want to calculate nr_surfaces first, to avoid taking up so much
180 * space for the binding table.
181 */
182 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
183 sizeof(uint32_t) * entries,
184 32, &stage_state->bind_bo_offset);
185
186 /* BRW_NEW_SURFACES and BRW_NEW_VS_CONSTBUF */
187 for (i = 0; i < entries; i++) {
188 bind[i] = stage_state->surf_offset[i];
189 }
190
191 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
192 }
193
194 const struct brw_tracked_state brw_vs_binding_table = {
195 .dirty = {
196 .mesa = 0,
197 .brw = (BRW_NEW_BATCH |
198 BRW_NEW_VS_CONSTBUF |
199 BRW_NEW_SURFACES),
200 .cache = CACHE_NEW_VS_PROG
201 },
202 .emit = brw_vs_upload_binding_table,
203 };