brw_vs_surface_state.c revision 8654931d117f178fa575d3fdc5bbff9f8bf9e816
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 "main/texstore.h"
34#include "program/prog_parameter.h"
35
36#include "brw_context.h"
37#include "brw_state.h"
38
39/* Creates a new VS constant buffer reflecting the current VS program's
40 * constants, if needed by the VS program.
41 *
42 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
43 * state atom.
44 */
45static void
46prepare_vs_constants(struct brw_context *brw)
47{
48   struct gl_context *ctx = &brw->intel.ctx;
49   struct intel_context *intel = &brw->intel;
50   /* BRW_NEW_VERTEX_PROGRAM */
51   struct brw_vertex_program *vp =
52      (struct brw_vertex_program *) brw->vertex_program;
53   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
54   int i;
55
56   if (vp->program.IsNVProgram)
57      _mesa_load_tracked_matrices(ctx);
58
59   /* Updates the ParamaterValues[i] pointers for all parameters of the
60    * basic type of PROGRAM_STATE_VAR.
61    */
62   _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
63
64   /* CACHE_NEW_VS_PROG */
65   if (!brw->vs.prog_data->nr_pull_params) {
66      if (brw->vs.const_bo) {
67	 drm_intel_bo_unreference(brw->vs.const_bo);
68	 brw->vs.const_bo = NULL;
69	 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
70      }
71      return;
72   }
73
74   /* _NEW_PROGRAM_CONSTANTS */
75   drm_intel_bo_unreference(brw->vs.const_bo);
76   brw->vs.const_bo = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
77					 brw->vs.prog_data->nr_pull_params * 4,
78					 64);
79
80   drm_intel_gem_bo_map_gtt(brw->vs.const_bo);
81   for (i = 0; i < brw->vs.prog_data->nr_pull_params; i++) {
82      memcpy(brw->vs.const_bo->virtual + i * 4,
83	     brw->vs.prog_data->pull_param[i],
84	     4);
85   }
86
87   if (0) {
88      for (i = 0; i < params->NumParameters; i++) {
89	 float *row = (float *)brw->vs.const_bo->virtual + i * 4;
90	 printf("vs const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
91		i, row[0], row[1], row[2], row[3]);
92      }
93   }
94
95   drm_intel_gem_bo_unmap_gtt(brw->vs.const_bo);
96   brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
97}
98
99const struct brw_tracked_state brw_vs_constants = {
100   .dirty = {
101      .mesa = (_NEW_PROGRAM_CONSTANTS),
102      .brw = (BRW_NEW_VERTEX_PROGRAM),
103      .cache = CACHE_NEW_VS_PROG,
104   },
105   .prepare = prepare_vs_constants,
106};
107
108/**
109 * Update the surface state for a VS constant buffer.
110 *
111 * Sets brw->vs.surf_bo[surf] and brw->vp->const_buffer.
112 */
113static void
114brw_update_vs_constant_surface( struct gl_context *ctx,
115                                GLuint surf)
116{
117   struct brw_context *brw = brw_context(ctx);
118   struct intel_context *intel = &brw->intel;
119   struct brw_vertex_program *vp =
120      (struct brw_vertex_program *) brw->vertex_program;
121   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
122
123   assert(surf == 0);
124
125   /* If there's no constant buffer, then no surface BO is needed to point at
126    * it.
127    */
128   if (brw->vs.const_bo == NULL) {
129      brw->vs.surf_offset[surf] = 0;
130      return;
131   }
132
133   if (intel->gen >= 7) {
134      gen7_create_constant_surface(brw, brw->vs.const_bo, params->NumParameters,
135				  &brw->vs.surf_offset[surf]);
136   } else {
137      brw_create_constant_surface(brw, brw->vs.const_bo, params->NumParameters,
138				  &brw->vs.surf_offset[surf]);
139   }
140}
141
142
143static void
144prepare_vs_surfaces(struct brw_context *brw)
145{
146   int nr_surfaces = 0;
147
148   if (brw->vs.const_bo) {
149      brw_add_validated_bo(brw, brw->vs.const_bo);
150      nr_surfaces = 1;
151   }
152
153   if (brw->vs.nr_surfaces != nr_surfaces) {
154      brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
155      brw->vs.nr_surfaces = nr_surfaces;
156   }
157}
158
159/**
160 * Vertex shader surfaces (constant buffer).
161 *
162 * This consumes the state updates for the constant buffer needing
163 * to be updated, and produces BRW_NEW_NR_VS_SURFACES for the VS unit and
164 * CACHE_NEW_SURF_BIND for the binding table upload.
165 */
166static void upload_vs_surfaces(struct brw_context *brw)
167{
168   struct gl_context *ctx = &brw->intel.ctx;
169   uint32_t *bind;
170   int i;
171
172   /* BRW_NEW_NR_VS_SURFACES */
173   if (brw->vs.nr_surfaces == 0) {
174      if (brw->vs.bind_bo_offset) {
175	 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
176      }
177      brw->vs.bind_bo_offset = 0;
178      return;
179   }
180
181   brw_update_vs_constant_surface(ctx, SURF_INDEX_VERT_CONST_BUFFER);
182
183   /* Might want to calculate nr_surfaces first, to avoid taking up so much
184    * space for the binding table. (once we have vs samplers)
185    */
186   bind = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
187			  sizeof(uint32_t) * BRW_VS_MAX_SURF,
188			  32, &brw->vs.bind_bo_offset);
189
190   for (i = 0; i < BRW_VS_MAX_SURF; i++) {
191      /* BRW_NEW_VS_CONSTBUF */
192      bind[i] = brw->vs.surf_offset[i];
193   }
194
195   brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
196}
197
198const struct brw_tracked_state brw_vs_surfaces = {
199   .dirty = {
200      .mesa = 0,
201      .brw = (BRW_NEW_VS_CONSTBUF |
202	      BRW_NEW_NR_VS_SURFACES |
203	      BRW_NEW_BATCH),
204      .cache = 0
205   },
206   .prepare = prepare_vs_surfaces,
207   .emit = upload_vs_surfaces,
208};
209