Merge remote-tracking branch 'origin/master' into vulkan
[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 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 <keithw@vmware.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 #include "intel_buffer_objects.h"
38
39 /**
40 * Creates a temporary BO containing the pull constant data for the shader
41 * stage, and the SURFACE_STATE struct that points at it.
42 *
43 * Pull constants are GLSL uniforms (and other constant data) beyond what we
44 * could fit as push constants, or that have variable-index array access
45 * (which is easiest to support using pull constants, and avoids filling
46 * register space with mostly-unused data).
47 *
48 * Compare this path to brw_curbe.c for gen4/5 push constants, and
49 * gen6_vs_state.c for gen6+ push constants.
50 */
51 void
52 brw_upload_pull_constants(struct brw_context *brw,
53 GLbitfield64 brw_new_constbuf,
54 const struct gl_program *prog,
55 struct brw_stage_state *stage_state,
56 const struct brw_stage_prog_data *prog_data)
57 {
58 unsigned i;
59 uint32_t surf_index = prog_data->binding_table.pull_constants_start;
60
61 if (!prog_data->nr_pull_params) {
62 if (stage_state->surf_offset[surf_index]) {
63 stage_state->surf_offset[surf_index] = 0;
64 brw->ctx.NewDriverState |= brw_new_constbuf;
65 }
66 return;
67 }
68
69 /* Updates the ParamaterValues[i] pointers for all parameters of the
70 * basic type of PROGRAM_STATE_VAR.
71 */
72 _mesa_load_state_parameters(&brw->ctx, prog->Parameters);
73
74 /* BRW_NEW_*_PROG_DATA | _NEW_PROGRAM_CONSTANTS */
75 uint32_t size = prog_data->nr_pull_params * 4;
76 drm_intel_bo *const_bo = NULL;
77 uint32_t const_offset;
78 gl_constant_value *constants = intel_upload_space(brw, size, 64,
79 &const_bo, &const_offset);
80
81 STATIC_ASSERT(sizeof(gl_constant_value) == sizeof(float));
82
83 for (i = 0; i < prog_data->nr_pull_params; i++) {
84 constants[i] = *prog_data->pull_param[i];
85 }
86
87 if (0) {
88 for (i = 0; i < ALIGN(prog_data->nr_pull_params, 4) / 4; i++) {
89 const gl_constant_value *row = &constants[i * 4];
90 fprintf(stderr, "const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
91 i, row[0].f, row[1].f, row[2].f, row[3].f);
92 }
93 }
94
95 brw_create_constant_surface(brw, const_bo, const_offset, size,
96 &stage_state->surf_offset[surf_index]);
97 drm_intel_bo_unreference(const_bo);
98
99 brw->ctx.NewDriverState |= brw_new_constbuf;
100 }
101
102
103 /* Creates a new VS constant buffer reflecting the current VS program's
104 * constants, if needed by the VS program.
105 *
106 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
107 * state atom.
108 */
109 static void
110 brw_upload_vs_pull_constants(struct brw_context *brw)
111 {
112 struct brw_stage_state *stage_state = &brw->vs.base;
113
114 /* BRW_NEW_VERTEX_PROGRAM */
115 struct brw_vertex_program *vp =
116 (struct brw_vertex_program *) brw->vertex_program;
117
118 /* BRW_NEW_VS_PROG_DATA */
119 const struct brw_stage_prog_data *prog_data = &brw->vs.prog_data->base.base;
120
121 /* _NEW_PROGRAM_CONSTANTS */
122 brw_upload_pull_constants(brw, BRW_NEW_VS_CONSTBUF, &vp->program.Base,
123 stage_state, prog_data);
124 }
125
126 const struct brw_tracked_state brw_vs_pull_constants = {
127 .dirty = {
128 .mesa = _NEW_PROGRAM_CONSTANTS,
129 .brw = BRW_NEW_BATCH |
130 BRW_NEW_VERTEX_PROGRAM |
131 BRW_NEW_VS_PROG_DATA,
132 },
133 .emit = brw_upload_vs_pull_constants,
134 };
135
136 static void
137 brw_upload_vs_ubo_surfaces(struct brw_context *brw)
138 {
139 struct gl_context *ctx = &brw->ctx;
140 /* _NEW_PROGRAM */
141 struct gl_shader_program *prog =
142 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
143
144 if (!prog)
145 return;
146
147 /* BRW_NEW_VS_PROG_DATA */
148 brw_upload_ubo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
149 &brw->vs.base, &brw->vs.prog_data->base.base);
150 }
151
152 const struct brw_tracked_state brw_vs_ubo_surfaces = {
153 .dirty = {
154 .mesa = _NEW_PROGRAM,
155 .brw = BRW_NEW_BATCH |
156 BRW_NEW_UNIFORM_BUFFER |
157 BRW_NEW_VS_PROG_DATA,
158 },
159 .emit = brw_upload_vs_ubo_surfaces,
160 };
161
162 static void
163 brw_upload_vs_abo_surfaces(struct brw_context *brw)
164 {
165 struct gl_context *ctx = &brw->ctx;
166 /* _NEW_PROGRAM */
167 struct gl_shader_program *prog =
168 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
169
170 if (prog) {
171 /* BRW_NEW_VS_PROG_DATA */
172 brw_upload_abo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
173 &brw->vs.base, &brw->vs.prog_data->base.base);
174 }
175 }
176
177 const struct brw_tracked_state brw_vs_abo_surfaces = {
178 .dirty = {
179 .mesa = _NEW_PROGRAM,
180 .brw = BRW_NEW_ATOMIC_BUFFER |
181 BRW_NEW_BATCH |
182 BRW_NEW_VS_PROG_DATA,
183 },
184 .emit = brw_upload_vs_abo_surfaces,
185 };
186
187 static void
188 brw_upload_vs_image_surfaces(struct brw_context *brw)
189 {
190 struct gl_context *ctx = &brw->ctx;
191 /* BRW_NEW_VERTEX_PROGRAM */
192 struct gl_shader_program *prog =
193 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
194
195 if (prog) {
196 /* BRW_NEW_VS_PROG_DATA, BRW_NEW_IMAGE_UNITS, _NEW_TEXTURE */
197 brw_upload_image_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
198 &brw->vs.base, &brw->vs.prog_data->base.base);
199 }
200 }
201
202 const struct brw_tracked_state brw_vs_image_surfaces = {
203 .dirty = {
204 .mesa = _NEW_TEXTURE,
205 .brw = BRW_NEW_BATCH |
206 BRW_NEW_IMAGE_UNITS |
207 BRW_NEW_VERTEX_PROGRAM |
208 BRW_NEW_VS_PROG_DATA,
209 },
210 .emit = brw_upload_vs_image_surfaces,
211 };