2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
32 #include "main/mtypes.h"
33 #include "main/texstore.h"
34 #include "shader/prog_parameter.h"
36 #include "brw_context.h"
37 #include "brw_state.h"
39 /* Creates a new VS constant buffer reflecting the current VS program's
40 * constants, if needed by the VS program.
42 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
46 prepare_vs_constants(struct brw_context
*brw
)
48 GLcontext
*ctx
= &brw
->intel
.ctx
;
49 struct intel_context
*intel
= &brw
->intel
;
50 struct brw_vertex_program
*vp
=
51 (struct brw_vertex_program
*) brw
->vertex_program
;
52 const struct gl_program_parameter_list
*params
= vp
->program
.Base
.Parameters
;
53 const int size
= params
->NumParameters
* 4 * sizeof(GLfloat
);
56 if (vp
->program
.IsNVProgram
)
57 _mesa_load_tracked_matrices(ctx
);
59 /* Updates the ParamaterValues[i] pointers for all parameters of the
60 * basic type of PROGRAM_STATE_VAR.
62 _mesa_load_state_parameters(&brw
->intel
.ctx
, vp
->program
.Base
.Parameters
);
64 /* BRW_NEW_VERTEX_PROGRAM */
65 if (!vp
->use_const_buffer
) {
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
;
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",
79 drm_intel_gem_bo_map_gtt(brw
->vs
.const_bo
);
80 for (i
= 0; i
< params
->NumParameters
; i
++) {
81 memcpy(brw
->vs
.const_bo
->virtual + i
* 4 * sizeof(float),
82 params
->ParameterValues
[i
],
85 drm_intel_gem_bo_unmap_gtt(brw
->vs
.const_bo
);
86 brw
->state
.dirty
.brw
|= BRW_NEW_VS_CONSTBUF
;
89 const struct brw_tracked_state brw_vs_constants
= {
91 .mesa
= (_NEW_PROGRAM_CONSTANTS
),
92 .brw
= (BRW_NEW_VERTEX_PROGRAM
),
95 .prepare
= prepare_vs_constants
,
99 * Update the surface state for a VS constant buffer.
101 * Sets brw->vs.surf_bo[surf] and brw->vp->const_buffer.
104 brw_update_vs_constant_surface( GLcontext
*ctx
,
107 struct brw_context
*brw
= brw_context(ctx
);
108 struct brw_surface_key key
;
109 struct brw_vertex_program
*vp
=
110 (struct brw_vertex_program
*) brw
->vertex_program
;
111 const struct gl_program_parameter_list
*params
= vp
->program
.Base
.Parameters
;
115 /* If there's no constant buffer, then no surface BO is needed to point at
118 if (brw
->vs
.const_bo
== NULL
) {
119 drm_intel_bo_unreference(brw
->vs
.surf_bo
[surf
]);
120 brw
->vs
.surf_bo
[surf
] = NULL
;
124 memset(&key
, 0, sizeof(key
));
126 key
.format
= MESA_FORMAT_RGBA_FLOAT32
;
127 key
.internal_format
= GL_RGBA
;
128 key
.bo
= brw
->vs
.const_bo
;
129 key
.depthmode
= GL_NONE
;
130 key
.pitch
= params
->NumParameters
;
131 key
.width
= params
->NumParameters
;
137 printf("%s:\n", __FUNCTION__);
138 printf(" width %d height %d depth %d cpp %d pitch %d\n",
139 key.width, key.height, key.depth, key.cpp, key.pitch);
142 drm_intel_bo_unreference(brw
->vs
.surf_bo
[surf
]);
143 brw
->vs
.surf_bo
[surf
] = brw_search_cache(&brw
->surface_cache
,
148 if (brw
->vs
.surf_bo
[surf
] == NULL
) {
149 brw
->vs
.surf_bo
[surf
] = brw_create_constant_surface(brw
, &key
);
155 * Constructs the binding table for the VS surface state.
157 static drm_intel_bo
*
158 brw_vs_get_binding_table(struct brw_context
*brw
)
160 drm_intel_bo
*bind_bo
;
162 bind_bo
= brw_search_cache(&brw
->surface_cache
, BRW_SS_SURF_BIND
,
164 brw
->vs
.surf_bo
, BRW_VS_MAX_SURF
,
167 if (bind_bo
== NULL
) {
168 GLuint data_size
= BRW_VS_MAX_SURF
* sizeof(GLuint
);
169 uint32_t data
[BRW_VS_MAX_SURF
];
172 for (i
= 0; i
< BRW_VS_MAX_SURF
; i
++)
173 if (brw
->vs
.surf_bo
[i
])
174 data
[i
] = brw
->vs
.surf_bo
[i
]->offset
;
178 bind_bo
= brw_upload_cache( &brw
->surface_cache
, BRW_SS_SURF_BIND
,
180 brw
->vs
.surf_bo
, BRW_VS_MAX_SURF
,
183 /* Emit binding table relocations to surface state */
184 for (i
= 0; i
< BRW_VS_MAX_SURF
; i
++) {
185 if (brw
->vs
.surf_bo
[i
] != NULL
) {
186 /* The presumed offsets were set in the data values for
189 drm_intel_bo_emit_reloc(bind_bo
, i
* 4,
190 brw
->vs
.surf_bo
[i
], 0,
191 I915_GEM_DOMAIN_INSTRUCTION
, 0);
200 * Vertex shader surfaces (constant buffer).
202 * This consumes the state updates for the constant buffer needing
203 * to be updated, and produces BRW_NEW_NR_VS_SURFACES for the VS unit and
204 * CACHE_NEW_SURF_BIND for the binding table upload.
206 static void prepare_vs_surfaces(struct brw_context
*brw
)
208 GLcontext
*ctx
= &brw
->intel
.ctx
;
212 brw_update_vs_constant_surface(ctx
, SURF_INDEX_VERT_CONST_BUFFER
);
214 for (i
= 0; i
< BRW_VS_MAX_SURF
; i
++) {
215 if (brw
->vs
.surf_bo
[i
] != NULL
) {
220 if (brw
->vs
.nr_surfaces
!= nr_surfaces
) {
221 brw
->state
.dirty
.brw
|= BRW_NEW_NR_VS_SURFACES
;
222 brw
->vs
.nr_surfaces
= nr_surfaces
;
225 /* Note that we don't end up updating the bind_bo if we don't have a
226 * surface to be pointing at. This should be relatively harmless, as it
227 * just slightly increases our working set size.
229 if (brw
->vs
.nr_surfaces
!= 0) {
230 drm_intel_bo_unreference(brw
->vs
.bind_bo
);
231 brw
->vs
.bind_bo
= brw_vs_get_binding_table(brw
);
235 const struct brw_tracked_state brw_vs_surfaces
= {
238 .brw
= (BRW_NEW_VS_CONSTBUF
),
241 .prepare
= prepare_vs_surfaces
,