i965: Make a brw_stage_prog_data for storing the SURF_INDEX information.
[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 uint32_t surf_index = prog_data->base.binding_table.pull_constants_start;
48
49 /* Updates the ParamaterValues[i] pointers for all parameters of the
50 * basic type of PROGRAM_STATE_VAR.
51 */
52 _mesa_load_state_parameters(&brw->ctx, prog->Parameters);
53
54 if (!prog_data->nr_pull_params) {
55 if (stage_state->const_bo) {
56 drm_intel_bo_unreference(stage_state->const_bo);
57 stage_state->const_bo = NULL;
58 stage_state->surf_offset[surf_index] = 0;
59 brw->state.dirty.brw |= brw_new_constbuf;
60 }
61 return;
62 }
63
64 /* _NEW_PROGRAM_CONSTANTS */
65 drm_intel_bo_unreference(stage_state->const_bo);
66 uint32_t size = prog_data->nr_pull_params * 4;
67 stage_state->const_bo = drm_intel_bo_alloc(brw->bufmgr, "vec4_const_buffer",
68 size, 64);
69
70 drm_intel_gem_bo_map_gtt(stage_state->const_bo);
71
72 for (i = 0; i < prog_data->nr_pull_params; i++) {
73 memcpy(stage_state->const_bo->virtual + i * 4,
74 prog_data->pull_param[i],
75 4);
76 }
77
78 if (0) {
79 for (i = 0; i < ALIGN(prog_data->nr_pull_params, 4) / 4; i++) {
80 float *row = (float *)stage_state->const_bo->virtual + i * 4;
81 printf("const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
82 i, row[0], row[1], row[2], row[3]);
83 }
84 }
85
86 drm_intel_gem_bo_unmap_gtt(stage_state->const_bo);
87
88 brw->vtbl.create_constant_surface(brw, stage_state->const_bo, 0, size,
89 &stage_state->surf_offset[surf_index],
90 false);
91
92 brw->state.dirty.brw |= brw_new_constbuf;
93 }
94
95
96 /* Creates a new VS constant buffer reflecting the current VS program's
97 * constants, if needed by the VS program.
98 *
99 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
100 * state atom.
101 */
102 static void
103 brw_upload_vs_pull_constants(struct brw_context *brw)
104 {
105 struct brw_stage_state *stage_state = &brw->vs.base;
106
107 /* BRW_NEW_VERTEX_PROGRAM */
108 struct brw_vertex_program *vp =
109 (struct brw_vertex_program *) brw->vertex_program;
110
111 /* CACHE_NEW_VS_PROG */
112 const struct brw_vec4_prog_data *prog_data = &brw->vs.prog_data->base;
113
114 /* _NEW_PROGRAM_CONSTANTS */
115 brw_upload_vec4_pull_constants(brw, BRW_NEW_VS_CONSTBUF, &vp->program.Base,
116 stage_state, prog_data);
117 }
118
119 const struct brw_tracked_state brw_vs_pull_constants = {
120 .dirty = {
121 .mesa = (_NEW_PROGRAM_CONSTANTS),
122 .brw = (BRW_NEW_BATCH | BRW_NEW_VERTEX_PROGRAM),
123 .cache = CACHE_NEW_VS_PROG,
124 },
125 .emit = brw_upload_vs_pull_constants,
126 };
127
128 static void
129 brw_upload_vs_ubo_surfaces(struct brw_context *brw)
130 {
131 struct gl_context *ctx = &brw->ctx;
132 /* _NEW_PROGRAM */
133 struct gl_shader_program *prog = ctx->Shader.CurrentVertexProgram;
134
135 if (!prog)
136 return;
137
138 /* CACHE_NEW_VS_PROG */
139 brw_upload_ubo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
140 &brw->vs.base, &brw->vs.prog_data->base.base);
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 = CACHE_NEW_VS_PROG,
148 },
149 .emit = brw_upload_vs_ubo_surfaces,
150 };