1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_math.h"
31 #include "svga_sampler_view.h"
32 #include "svga_winsys.h"
33 #include "svga_context.h"
34 #include "svga_state.h"
38 void svga_cleanup_tss_binding(struct svga_context
*svga
)
41 unsigned count
= MAX2( svga
->curr
.num_sampler_views
,
42 svga
->state
.hw_draw
.num_views
);
44 for (i
= 0; i
< count
; i
++) {
45 struct svga_hw_view_state
*view
= &svga
->state
.hw_draw
.views
[i
];
47 svga_sampler_view_reference(&view
->v
, NULL
);
48 pipe_sampler_view_release(&svga
->pipe
, &svga
->curr
.sampler_views
[i
]);
49 pipe_resource_reference( &view
->texture
, NULL
);
59 struct svga_hw_view_state
*view
;
60 } bind
[PIPE_MAX_SAMPLERS
];
66 static enum pipe_error
67 update_tss_binding(struct svga_context
*svga
,
70 boolean reemit
= svga
->rebind
.texture_samplers
;
72 unsigned count
= MAX2( svga
->curr
.num_sampler_views
,
73 svga
->state
.hw_draw
.num_views
);
77 struct bind_queue queue
;
81 for (i
= 0; i
< count
; i
++) {
82 const struct svga_sampler_state
*s
= svga
->curr
.sampler
[i
];
83 struct svga_hw_view_state
*view
= &svga
->state
.hw_draw
.views
[i
];
84 struct pipe_resource
*texture
= NULL
;
85 struct pipe_sampler_view
*sv
= svga
->curr
.sampler_views
[i
];
89 min_lod
= MAX2(0, (s
->view_min_lod
+ sv
->u
.tex
.first_level
));
90 max_lod
= MIN2(s
->view_max_lod
+ sv
->u
.tex
.first_level
,
91 sv
->texture
->last_level
);
92 texture
= sv
->texture
;
98 if (view
->texture
!= texture
||
99 view
->min_lod
!= min_lod
||
100 view
->max_lod
!= max_lod
) {
102 svga_sampler_view_reference(&view
->v
, NULL
);
103 pipe_resource_reference( &view
->texture
, texture
);
106 view
->min_lod
= min_lod
;
107 view
->max_lod
= max_lod
;
110 view
->v
= svga_get_tex_sampler_view(&svga
->pipe
,
117 * We need to reemit non-null texture bindings, even when they are not
118 * dirty, to ensure that the resources are paged in.
122 (reemit
&& view
->v
)) {
123 queue
.bind
[queue
.bind_count
].unit
= i
;
124 queue
.bind
[queue
.bind_count
].view
= view
;
127 if (!view
->dirty
&& view
->v
) {
128 svga_validate_sampler_view(svga
, view
->v
);
132 svga
->state
.hw_draw
.num_views
= svga
->curr
.num_sampler_views
;
134 if (queue
.bind_count
) {
135 SVGA3dTextureState
*ts
;
137 if (SVGA3D_BeginSetTextureState( svga
->swc
,
139 queue
.bind_count
) != PIPE_OK
)
142 for (i
= 0; i
< queue
.bind_count
; i
++) {
143 struct svga_winsys_surface
*handle
;
145 ts
[i
].stage
= queue
.bind
[i
].unit
;
146 ts
[i
].name
= SVGA3D_TS_BIND_TEXTURE
;
148 if (queue
.bind
[i
].view
->v
) {
149 handle
= queue
.bind
[i
].view
->v
->handle
;
154 svga
->swc
->surface_relocation(svga
->swc
,
159 queue
.bind
[i
].view
->dirty
= FALSE
;
162 SVGA_FIFOCommitAll( svga
->swc
);
165 svga
->rebind
.texture_samplers
= FALSE
;
170 return PIPE_ERROR_OUT_OF_MEMORY
;
177 * Similar to update_tss_binding, but without any state checking/update.
179 * Called at the beginning of every new command buffer to ensure that
180 * non-dirty textures are properly paged-in.
183 svga_reemit_tss_bindings(struct svga_context
*svga
)
187 struct bind_queue queue
;
189 assert(svga
->rebind
.texture_samplers
);
191 queue
.bind_count
= 0;
193 for (i
= 0; i
< svga
->state
.hw_draw
.num_views
; i
++) {
194 struct svga_hw_view_state
*view
= &svga
->state
.hw_draw
.views
[i
];
197 queue
.bind
[queue
.bind_count
].unit
= i
;
198 queue
.bind
[queue
.bind_count
].view
= view
;
203 if (queue
.bind_count
) {
204 SVGA3dTextureState
*ts
;
206 ret
= SVGA3D_BeginSetTextureState(svga
->swc
,
209 if (ret
!= PIPE_OK
) {
213 for (i
= 0; i
< queue
.bind_count
; i
++) {
214 struct svga_winsys_surface
*handle
;
216 ts
[i
].stage
= queue
.bind
[i
].unit
;
217 ts
[i
].name
= SVGA3D_TS_BIND_TEXTURE
;
219 assert(queue
.bind
[i
].view
->v
);
220 handle
= queue
.bind
[i
].view
->v
->handle
;
221 svga
->swc
->surface_relocation(svga
->swc
,
227 SVGA_FIFOCommitAll(svga
->swc
);
230 svga
->rebind
.texture_samplers
= FALSE
;
236 struct svga_tracked_state svga_hw_tss_binding
= {
237 "texture binding emit",
238 SVGA_NEW_TEXTURE_BINDING
|
244 /***********************************************************************
249 SVGA3dTextureState ts
[PIPE_MAX_SAMPLERS
*SVGA3D_TS_MAX
];
253 #define EMIT_TS(svga, unit, val, token, fail) \
255 assert(unit < Elements(svga->state.hw_draw.ts)); \
256 assert(SVGA3D_TS_##token < Elements(svga->state.hw_draw.ts[unit])); \
257 if (svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] != val) { \
258 svga_queue_tss( &queue, unit, SVGA3D_TS_##token, val ); \
259 svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] = val; \
263 #define EMIT_TS_FLOAT(svga, unit, fvalue, token, fail) \
265 unsigned val = fui(fvalue); \
266 assert(unit < Elements(svga->state.hw_draw.ts)); \
267 assert(SVGA3D_TS_##token < Elements(svga->state.hw_draw.ts[unit])); \
268 if (svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] != val) { \
269 svga_queue_tss( &queue, unit, SVGA3D_TS_##token, val ); \
270 svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] = val; \
276 svga_queue_tss( struct ts_queue
*q
,
281 assert(q
->ts_count
< sizeof(q
->ts
)/sizeof(q
->ts
[0]));
282 q
->ts
[q
->ts_count
].stage
= unit
;
283 q
->ts
[q
->ts_count
].name
= tss
;
284 q
->ts
[q
->ts_count
].value
= value
;
289 static enum pipe_error
290 update_tss(struct svga_context
*svga
,
294 struct ts_queue queue
;
297 for (i
= 0; i
< svga
->curr
.num_samplers
; i
++) {
298 if (svga
->curr
.sampler
[i
]) {
299 const struct svga_sampler_state
*curr
= svga
->curr
.sampler
[i
];
301 EMIT_TS(svga
, i
, curr
->mipfilter
, MIPFILTER
, fail
);
302 EMIT_TS(svga
, i
, curr
->min_lod
, TEXTURE_MIPMAP_LEVEL
, fail
);
303 EMIT_TS(svga
, i
, curr
->magfilter
, MAGFILTER
, fail
);
304 EMIT_TS(svga
, i
, curr
->minfilter
, MINFILTER
, fail
);
305 EMIT_TS(svga
, i
, curr
->aniso_level
, TEXTURE_ANISOTROPIC_LEVEL
, fail
);
306 EMIT_TS_FLOAT(svga
, i
, curr
->lod_bias
, TEXTURE_LOD_BIAS
, fail
);
307 EMIT_TS(svga
, i
, curr
->addressu
, ADDRESSU
, fail
);
308 EMIT_TS(svga
, i
, curr
->addressw
, ADDRESSW
, fail
);
309 EMIT_TS(svga
, i
, curr
->bordercolor
, BORDERCOLOR
, fail
);
310 // TEXCOORDINDEX -- hopefully not needed
312 if (svga
->curr
.tex_flags
.flag_1d
& (1 << i
)) {
313 EMIT_TS(svga
, i
, SVGA3D_TEX_ADDRESS_WRAP
, ADDRESSV
, fail
);
316 EMIT_TS(svga
, i
, curr
->addressv
, ADDRESSV
, fail
);
318 if (svga
->curr
.tex_flags
.flag_srgb
& (1 << i
))
319 EMIT_TS_FLOAT(svga
, i
, 2.2f
, GAMMA
, fail
);
321 EMIT_TS_FLOAT(svga
, i
, 1.0f
, GAMMA
, fail
);
326 if (queue
.ts_count
) {
327 SVGA3dTextureState
*ts
;
329 if (SVGA3D_BeginSetTextureState( svga
->swc
,
331 queue
.ts_count
) != PIPE_OK
)
336 queue
.ts_count
* sizeof queue
.ts
[0]);
338 SVGA_FIFOCommitAll( svga
->swc
);
344 /* XXX: need to poison cached hardware state on failure to ensure
345 * dirty state gets re-emitted. Fix this by re-instating partial
346 * FIFOCommit command and only updating cached hw state once the
347 * initial allocation has succeeded.
349 memset(svga
->state
.hw_draw
.ts
, 0xcd, sizeof(svga
->state
.hw_draw
.ts
));
351 return PIPE_ERROR_OUT_OF_MEMORY
;
355 struct svga_tracked_state svga_hw_tss
= {
356 "texture state emit",
358 SVGA_NEW_TEXTURE_FLAGS
),