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