1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
35 #include "draw/draw_context.h"
37 #include "lp_context.h"
38 #include "lp_screen.h"
41 #include "state_tracker/sw_winsys.h"
45 llvmpipe_create_sampler_state(struct pipe_context
*pipe
,
46 const struct pipe_sampler_state
*sampler
)
48 struct pipe_sampler_state
*state
= mem_dup(sampler
, sizeof *sampler
);
50 if (LP_PERF
& PERF_NO_MIP_LINEAR
) {
51 if (state
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
52 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
55 if (LP_PERF
& PERF_NO_MIPMAPS
)
56 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
58 if (LP_PERF
& PERF_NO_LINEAR
) {
59 state
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
60 state
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
68 llvmpipe_bind_sampler_states(struct pipe_context
*pipe
,
69 enum pipe_shader_type shader
,
74 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
77 assert(shader
< PIPE_SHADER_TYPES
);
78 assert(start
+ num
<= ARRAY_SIZE(llvmpipe
->samplers
[shader
]));
80 draw_flush(llvmpipe
->draw
);
82 /* set the new samplers */
83 for (i
= 0; i
< num
; i
++) {
84 llvmpipe
->samplers
[shader
][start
+ i
] = samplers
[i
];
87 /* find highest non-null samplers[] entry */
89 unsigned j
= MAX2(llvmpipe
->num_samplers
[shader
], start
+ num
);
90 while (j
> 0 && llvmpipe
->samplers
[shader
][j
- 1] == NULL
)
92 llvmpipe
->num_samplers
[shader
] = j
;
95 if (shader
== PIPE_SHADER_VERTEX
||
96 shader
== PIPE_SHADER_GEOMETRY
||
97 shader
== PIPE_SHADER_TESS_CTRL
||
98 shader
== PIPE_SHADER_TESS_EVAL
) {
99 draw_set_samplers(llvmpipe
->draw
,
101 llvmpipe
->samplers
[shader
],
102 llvmpipe
->num_samplers
[shader
]);
104 else if (shader
== PIPE_SHADER_COMPUTE
) {
105 llvmpipe
->cs_dirty
|= LP_CSNEW_SAMPLER
;
107 llvmpipe
->dirty
|= LP_NEW_SAMPLER
;
113 llvmpipe_set_sampler_views(struct pipe_context
*pipe
,
114 enum pipe_shader_type shader
,
117 struct pipe_sampler_view
**views
)
119 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
122 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
124 assert(shader
< PIPE_SHADER_TYPES
);
125 assert(start
+ num
<= ARRAY_SIZE(llvmpipe
->sampler_views
[shader
]));
127 draw_flush(llvmpipe
->draw
);
129 /* set the new sampler views */
130 for (i
= 0; i
< num
; i
++) {
132 * Warn if someone tries to set a view created in a different context
133 * (which is why we need the hack above in the first place).
134 * An assert would be better but st/mesa relies on it...
136 if (views
[i
] && views
[i
]->context
!= pipe
) {
137 debug_printf("Illegal setting of sampler_view %d created in another "
140 pipe_sampler_view_reference(&llvmpipe
->sampler_views
[shader
][start
+ i
],
144 /* find highest non-null sampler_views[] entry */
146 unsigned j
= MAX2(llvmpipe
->num_sampler_views
[shader
], start
+ num
);
147 while (j
> 0 && llvmpipe
->sampler_views
[shader
][j
- 1] == NULL
)
149 llvmpipe
->num_sampler_views
[shader
] = j
;
152 if (shader
== PIPE_SHADER_VERTEX
||
153 shader
== PIPE_SHADER_GEOMETRY
||
154 shader
== PIPE_SHADER_TESS_CTRL
||
155 shader
== PIPE_SHADER_TESS_EVAL
) {
156 draw_set_sampler_views(llvmpipe
->draw
,
158 llvmpipe
->sampler_views
[shader
],
159 llvmpipe
->num_sampler_views
[shader
]);
161 else if (shader
== PIPE_SHADER_COMPUTE
) {
162 llvmpipe
->cs_dirty
|= LP_CSNEW_SAMPLER_VIEW
;
164 llvmpipe
->dirty
|= LP_NEW_SAMPLER_VIEW
;
169 static struct pipe_sampler_view
*
170 llvmpipe_create_sampler_view(struct pipe_context
*pipe
,
171 struct pipe_resource
*texture
,
172 const struct pipe_sampler_view
*templ
)
174 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
176 * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
177 * This looks unfixable, so fix the bind flags instead when it happens.
179 if (!(texture
->bind
& PIPE_BIND_SAMPLER_VIEW
)) {
180 debug_printf("Illegal sampler view creation without bind flag\n");
181 texture
->bind
|= PIPE_BIND_SAMPLER_VIEW
;
186 view
->reference
.count
= 1;
187 view
->texture
= NULL
;
188 pipe_resource_reference(&view
->texture
, texture
);
189 view
->context
= pipe
;
193 * This is possibly too lenient, but the primary reason is just
194 * to catch state trackers which forget to initialize this, so
195 * it only catches clearly impossible view targets.
197 if (view
->target
!= texture
->target
) {
198 if (view
->target
== PIPE_TEXTURE_1D
)
199 assert(texture
->target
== PIPE_TEXTURE_1D_ARRAY
);
200 else if (view
->target
== PIPE_TEXTURE_1D_ARRAY
)
201 assert(texture
->target
== PIPE_TEXTURE_1D
);
202 else if (view
->target
== PIPE_TEXTURE_2D
)
203 assert(texture
->target
== PIPE_TEXTURE_2D_ARRAY
||
204 texture
->target
== PIPE_TEXTURE_CUBE
||
205 texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
206 else if (view
->target
== PIPE_TEXTURE_2D_ARRAY
)
207 assert(texture
->target
== PIPE_TEXTURE_2D
||
208 texture
->target
== PIPE_TEXTURE_CUBE
||
209 texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
210 else if (view
->target
== PIPE_TEXTURE_CUBE
)
211 assert(texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
212 texture
->target
== PIPE_TEXTURE_2D_ARRAY
);
213 else if (view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
214 assert(texture
->target
== PIPE_TEXTURE_CUBE
||
215 texture
->target
== PIPE_TEXTURE_2D_ARRAY
);
227 llvmpipe_sampler_view_destroy(struct pipe_context
*pipe
,
228 struct pipe_sampler_view
*view
)
230 pipe_resource_reference(&view
->texture
, NULL
);
236 llvmpipe_delete_sampler_state(struct pipe_context
*pipe
,
244 prepare_shader_sampling(
245 struct llvmpipe_context
*lp
,
247 struct pipe_sampler_view
**views
,
248 enum pipe_shader_type shader_type
)
252 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
];
253 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
];
254 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
];
257 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
261 for (i
= 0; i
< num
; i
++) {
262 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
265 struct pipe_resource
*tex
= view
->texture
;
266 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
267 unsigned width0
= tex
->width0
;
268 unsigned num_layers
= tex
->depth0
;
269 unsigned first_level
= 0;
270 unsigned last_level
= 0;
271 unsigned sample_stride
= 0;
272 unsigned num_samples
= tex
->nr_samples
;
275 /* regular texture - setup array of mipmap level offsets */
276 struct pipe_resource
*res
= view
->texture
;
279 if (llvmpipe_resource_is_texture(res
)) {
280 first_level
= view
->u
.tex
.first_level
;
281 last_level
= view
->u
.tex
.last_level
;
282 assert(first_level
<= last_level
);
283 assert(last_level
<= res
->last_level
);
284 addr
= lp_tex
->tex_data
;
286 for (j
= first_level
; j
<= last_level
; j
++) {
287 mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
288 row_stride
[j
] = lp_tex
->row_stride
[j
];
289 img_stride
[j
] = lp_tex
->img_stride
[j
];
291 if (tex
->target
== PIPE_TEXTURE_1D_ARRAY
||
292 tex
->target
== PIPE_TEXTURE_2D_ARRAY
||
293 tex
->target
== PIPE_TEXTURE_CUBE
||
294 tex
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
295 num_layers
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
296 for (j
= first_level
; j
<= last_level
; j
++) {
297 mip_offsets
[j
] += view
->u
.tex
.first_layer
*
298 lp_tex
->img_stride
[j
];
300 if (view
->target
== PIPE_TEXTURE_CUBE
||
301 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
302 assert(num_layers
% 6 == 0);
304 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
305 assert(view
->u
.tex
.last_layer
< res
->array_size
);
309 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
311 /* probably don't really need to fill that out */
316 /* everything specified in number of elements here. */
317 width0
= view
->u
.buf
.size
/ view_blocksize
;
318 addr
= (uint8_t *)addr
+ view
->u
.buf
.offset
;
319 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
323 /* display target texture/surface */
325 * XXX: Where should this be unmapped?
327 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
328 struct sw_winsys
*winsys
= screen
->winsys
;
329 addr
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
331 row_stride
[0] = lp_tex
->row_stride
[0];
332 img_stride
[0] = lp_tex
->img_stride
[0];
336 draw_set_mapped_texture(lp
->draw
,
339 width0
, tex
->height0
, num_layers
,
340 first_level
, last_level
,
341 num_samples
, sample_stride
,
343 row_stride
, img_stride
, mip_offsets
);
350 * Called whenever we're about to draw (no dirty flag, FIXME?).
353 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context
*lp
,
355 struct pipe_sampler_view
**views
)
357 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_VERTEX
);
362 * Called whenever we're about to draw (no dirty flag, FIXME?).
365 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context
*lp
,
367 struct pipe_sampler_view
**views
)
369 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_GEOMETRY
);
373 * Called whenever we're about to draw (no dirty flag, FIXME?).
376 llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context
*lp
,
378 struct pipe_sampler_view
**views
)
380 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_TESS_CTRL
);
384 * Called whenever we're about to draw (no dirty flag, FIXME?).
387 llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context
*lp
,
389 struct pipe_sampler_view
**views
)
391 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_TESS_EVAL
);
395 prepare_shader_images(
396 struct llvmpipe_context
*lp
,
398 struct pipe_image_view
*views
,
399 enum pipe_shader_type shader_type
)
407 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
411 for (i
= 0; i
< num
; i
++) {
412 struct pipe_image_view
*view
= i
< num
? &views
[i
] : NULL
;
415 struct pipe_resource
*img
= view
->resource
;
416 struct llvmpipe_resource
*lp_img
= llvmpipe_resource(img
);
420 unsigned width
= u_minify(img
->width0
, view
->u
.tex
.level
);
421 unsigned height
= u_minify(img
->height0
, view
->u
.tex
.level
);
422 unsigned num_layers
= img
->depth0
;
425 /* regular texture - setup array of mipmap level offsets */
426 struct pipe_resource
*res
= view
->resource
;
428 if (llvmpipe_resource_is_texture(res
)) {
429 uint32_t mip_offset
= lp_img
->mip_offsets
[view
->u
.tex
.level
];
430 addr
= lp_img
->tex_data
;
432 if (img
->target
== PIPE_TEXTURE_1D_ARRAY
||
433 img
->target
== PIPE_TEXTURE_2D_ARRAY
||
434 img
->target
== PIPE_TEXTURE_3D
||
435 img
->target
== PIPE_TEXTURE_CUBE
||
436 img
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
437 num_layers
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
438 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
439 mip_offset
+= view
->u
.tex
.first_layer
* lp_img
->img_stride
[view
->u
.tex
.level
];
442 row_stride
= lp_img
->row_stride
[view
->u
.tex
.level
];
443 img_stride
= lp_img
->img_stride
[view
->u
.tex
.level
];
444 addr
= (uint8_t *)addr
+ mip_offset
;
447 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
449 /* probably don't really need to fill that out */
453 /* everything specified in number of elements here. */
454 width
= view
->u
.buf
.size
/ view_blocksize
;
455 addr
= (uint8_t *)addr
+ view
->u
.buf
.offset
;
456 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
460 /* display target texture/surface */
462 * XXX: Where should this be unmapped?
464 struct llvmpipe_screen
*screen
= llvmpipe_screen(img
->screen
);
465 struct sw_winsys
*winsys
= screen
->winsys
;
466 addr
= winsys
->displaytarget_map(winsys
, lp_img
->dt
,
468 row_stride
= lp_img
->row_stride
[0];
469 img_stride
= lp_img
->img_stride
[0];
472 draw_set_mapped_image(lp
->draw
,
475 width
, height
, num_layers
,
477 row_stride
, img_stride
, 0, 0);
484 * Called whenever we're about to draw (no dirty flag, FIXME?).
487 llvmpipe_prepare_vertex_images(struct llvmpipe_context
*lp
,
489 struct pipe_image_view
*views
)
491 prepare_shader_images(lp
, num
, views
, PIPE_SHADER_VERTEX
);
496 * Called whenever we're about to draw (no dirty flag, FIXME?).
499 llvmpipe_prepare_geometry_images(struct llvmpipe_context
*lp
,
501 struct pipe_image_view
*views
)
503 prepare_shader_images(lp
, num
, views
, PIPE_SHADER_GEOMETRY
);
507 * Called whenever we're about to draw (no dirty flag, FIXME?).
510 llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context
*lp
,
512 struct pipe_image_view
*views
)
514 prepare_shader_images(lp
, num
, views
, PIPE_SHADER_TESS_CTRL
);
518 * Called whenever we're about to draw (no dirty flag, FIXME?).
521 llvmpipe_prepare_tess_eval_images(struct llvmpipe_context
*lp
,
523 struct pipe_image_view
*views
)
525 prepare_shader_images(lp
, num
, views
, PIPE_SHADER_TESS_EVAL
);
529 llvmpipe_init_sampler_funcs(struct llvmpipe_context
*llvmpipe
)
531 llvmpipe
->pipe
.create_sampler_state
= llvmpipe_create_sampler_state
;
533 llvmpipe
->pipe
.bind_sampler_states
= llvmpipe_bind_sampler_states
;
534 llvmpipe
->pipe
.create_sampler_view
= llvmpipe_create_sampler_view
;
535 llvmpipe
->pipe
.set_sampler_views
= llvmpipe_set_sampler_views
;
536 llvmpipe
->pipe
.sampler_view_destroy
= llvmpipe_sampler_view_destroy
;
537 llvmpipe
->pipe
.delete_sampler_state
= llvmpipe_delete_sampler_state
;