mesa: add & use a new driver flag for UBO updates instead of _NEW_BUFFER_OBJECT
[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 intel_context *intel = &brw->intel;
48 /* BRW_NEW_VERTEX_PROGRAM */
49 struct brw_vertex_program *vp =
50 (struct brw_vertex_program *) brw->vertex_program;
51 int i;
52
53 /* Updates the ParamaterValues[i] pointers for all parameters of the
54 * basic type of PROGRAM_STATE_VAR.
55 */
56 _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
57
58 /* CACHE_NEW_VS_PROG */
59 if (!brw->vs.prog_data->base.nr_pull_params) {
60 if (brw->vs.const_bo) {
61 drm_intel_bo_unreference(brw->vs.const_bo);
62 brw->vs.const_bo = NULL;
63 brw->vs.surf_offset[SURF_INDEX_VERT_CONST_BUFFER] = 0;
64 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
65 }
66 return;
67 }
68
69 /* _NEW_PROGRAM_CONSTANTS */
70 drm_intel_bo_unreference(brw->vs.const_bo);
71 uint32_t size = brw->vs.prog_data->base.nr_pull_params * 4;
72 brw->vs.const_bo = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
73 size, 64);
74
75 drm_intel_gem_bo_map_gtt(brw->vs.const_bo);
76 for (i = 0; i < brw->vs.prog_data->base.nr_pull_params; i++) {
77 memcpy(brw->vs.const_bo->virtual + i * 4,
78 brw->vs.prog_data->base.pull_param[i],
79 4);
80 }
81
82 if (0) {
83 for (i = 0; i < ALIGN(brw->vs.prog_data->base.nr_pull_params, 4) / 4;
84 i++) {
85 float *row = (float *)brw->vs.const_bo->virtual + i * 4;
86 printf("vs const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
87 i, row[0], row[1], row[2], row[3]);
88 }
89 }
90
91 drm_intel_gem_bo_unmap_gtt(brw->vs.const_bo);
92
93 const int surf = SURF_INDEX_VERT_CONST_BUFFER;
94 intel->vtbl.create_constant_surface(brw, brw->vs.const_bo, 0, size,
95 &brw->vs.surf_offset[surf], false);
96
97 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
98 }
99
100 const struct brw_tracked_state brw_vs_pull_constants = {
101 .dirty = {
102 .mesa = (_NEW_PROGRAM_CONSTANTS),
103 .brw = (BRW_NEW_BATCH | BRW_NEW_VERTEX_PROGRAM),
104 .cache = CACHE_NEW_VS_PROG,
105 },
106 .emit = brw_upload_vs_pull_constants,
107 };
108
109 static void
110 brw_upload_vs_ubo_surfaces(struct brw_context *brw)
111 {
112 struct gl_context *ctx = &brw->intel.ctx;
113 /* _NEW_PROGRAM */
114 struct gl_shader_program *prog = ctx->Shader.CurrentVertexProgram;
115
116 if (!prog)
117 return;
118
119 brw_upload_ubo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
120 &brw->vs.surf_offset[SURF_INDEX_VS_UBO(0)]);
121 }
122
123 const struct brw_tracked_state brw_vs_ubo_surfaces = {
124 .dirty = {
125 .mesa = _NEW_PROGRAM,
126 .brw = BRW_NEW_BATCH | BRW_NEW_UNIFORM_BUFFER,
127 .cache = 0,
128 },
129 .emit = brw_upload_vs_ubo_surfaces,
130 };
131
132 /**
133 * Constructs the binding table for the WM surface state, which maps unit
134 * numbers to surface state objects.
135 */
136 static void
137 brw_vs_upload_binding_table(struct brw_context *brw)
138 {
139 uint32_t *bind;
140 int i;
141
142 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
143 gen7_create_shader_time_surface(brw, &brw->vs.surf_offset[SURF_INDEX_VS_SHADER_TIME]);
144
145 assert(brw->vs.prog_data->base.num_surfaces
146 <= SURF_INDEX_VS_SHADER_TIME);
147 brw->vs.prog_data->base.num_surfaces = SURF_INDEX_VS_SHADER_TIME;
148 }
149
150 /* CACHE_NEW_VS_PROG: Skip making a binding table if we don't use textures or
151 * pull constants.
152 */
153 if (brw->vs.prog_data->base.num_surfaces == 0) {
154 if (brw->vs.bind_bo_offset != 0) {
155 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
156 brw->vs.bind_bo_offset = 0;
157 }
158 return;
159 }
160
161 /* Might want to calculate nr_surfaces first, to avoid taking up so much
162 * space for the binding table.
163 */
164 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
165 sizeof(uint32_t) * BRW_MAX_VS_SURFACES,
166 32, &brw->vs.bind_bo_offset);
167
168 /* BRW_NEW_SURFACES and BRW_NEW_VS_CONSTBUF */
169 for (i = 0; i < BRW_MAX_VS_SURFACES; i++) {
170 bind[i] = brw->vs.surf_offset[i];
171 }
172
173 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
174 }
175
176 const struct brw_tracked_state brw_vs_binding_table = {
177 .dirty = {
178 .mesa = 0,
179 .brw = (BRW_NEW_BATCH |
180 BRW_NEW_VS_CONSTBUF |
181 BRW_NEW_SURFACES),
182 .cache = CACHE_NEW_VS_PROG
183 },
184 .emit = brw_vs_upload_binding_table,
185 };