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
,
74 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
77 assert(shader
< PIPE_SHADER_TYPES
);
78 assert(start
+ num
<= Elements(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
|| shader
== PIPE_SHADER_GEOMETRY
) {
96 draw_set_samplers(llvmpipe
->draw
,
98 llvmpipe
->samplers
[shader
],
99 llvmpipe
->num_samplers
[shader
]);
102 llvmpipe
->dirty
|= LP_NEW_SAMPLER
;
107 llvmpipe_set_sampler_views(struct pipe_context
*pipe
,
111 struct pipe_sampler_view
**views
)
113 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
116 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
118 assert(shader
< PIPE_SHADER_TYPES
);
119 assert(start
+ num
<= Elements(llvmpipe
->sampler_views
[shader
]));
121 draw_flush(llvmpipe
->draw
);
123 /* set the new sampler views */
124 for (i
= 0; i
< num
; i
++) {
125 /* Note: we're using pipe_sampler_view_release() here to work around
126 * a possible crash when the old view belongs to another context that
127 * was already destroyed.
129 pipe_sampler_view_release(pipe
,
130 &llvmpipe
->sampler_views
[shader
][start
+ i
]);
131 pipe_sampler_view_reference(&llvmpipe
->sampler_views
[shader
][start
+ i
],
135 /* find highest non-null sampler_views[] entry */
137 unsigned j
= MAX2(llvmpipe
->num_sampler_views
[shader
], start
+ num
);
138 while (j
> 0 && llvmpipe
->sampler_views
[shader
][j
- 1] == NULL
)
140 llvmpipe
->num_sampler_views
[shader
] = j
;
143 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
144 draw_set_sampler_views(llvmpipe
->draw
,
146 llvmpipe
->sampler_views
[shader
],
147 llvmpipe
->num_sampler_views
[shader
]);
150 llvmpipe
->dirty
|= LP_NEW_SAMPLER_VIEW
;
154 static struct pipe_sampler_view
*
155 llvmpipe_create_sampler_view(struct pipe_context
*pipe
,
156 struct pipe_resource
*texture
,
157 const struct pipe_sampler_view
*templ
)
159 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
161 * XXX we REALLY want to see the correct bind flag here but the OpenGL
162 * state tracker can't guarantee that at least for texture buffer objects.
164 if (!(texture
->bind
& PIPE_BIND_SAMPLER_VIEW
))
165 debug_printf("Illegal sampler view creation without bind flag\n");
169 view
->reference
.count
= 1;
170 view
->texture
= NULL
;
171 pipe_resource_reference(&view
->texture
, texture
);
172 view
->context
= pipe
;
180 llvmpipe_sampler_view_destroy(struct pipe_context
*pipe
,
181 struct pipe_sampler_view
*view
)
183 pipe_resource_reference(&view
->texture
, NULL
);
189 llvmpipe_delete_sampler_state(struct pipe_context
*pipe
,
197 prepare_shader_sampling(
198 struct llvmpipe_context
*lp
,
200 struct pipe_sampler_view
**views
,
201 unsigned shader_type
,
202 struct pipe_resource
*mapped_tex
[PIPE_MAX_SHADER_SAMPLER_VIEWS
])
206 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
];
207 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
];
208 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
];
211 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
215 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
216 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
219 struct pipe_resource
*tex
= view
->texture
;
220 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(tex
);
221 unsigned width0
= tex
->width0
;
222 unsigned num_layers
= tex
->depth0
;
223 unsigned first_level
= 0;
224 unsigned last_level
= 0;
226 /* We're referencing the texture's internal data, so save a
229 pipe_resource_reference(&mapped_tex
[i
], tex
);
232 /* regular texture - setup array of mipmap level offsets */
233 struct pipe_resource
*res
= view
->texture
;
237 if (llvmpipe_resource_is_texture(res
)) {
238 first_level
= view
->u
.tex
.first_level
;
239 last_level
= view
->u
.tex
.last_level
;
240 assert(first_level
<= last_level
);
241 assert(last_level
<= res
->last_level
);
243 /* must trigger allocation first before we can get base ptr */
244 /* XXX this may fail due to OOM ? */
245 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, view
->u
.tex
.first_level
,
247 addr
= lp_tex
->linear_img
.data
;
249 for (j
= first_level
; j
<= last_level
; j
++) {
250 mip_ptr
= llvmpipe_get_texture_image_all(lp_tex
, j
,
252 mip_offsets
[j
] = (uint8_t *)mip_ptr
- (uint8_t *)addr
;
254 * could get mip offset directly but need call above to
255 * invoke tiled->linear conversion.
257 assert(lp_tex
->mip_offsets
[j
] == mip_offsets
[j
]);
258 row_stride
[j
] = lp_tex
->row_stride
[j
];
259 img_stride
[j
] = lp_tex
->img_stride
[j
];
261 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
262 res
->target
== PIPE_TEXTURE_2D_ARRAY
) {
263 num_layers
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
264 for (j
= first_level
; j
<= last_level
; j
++) {
265 mip_offsets
[j
] += view
->u
.tex
.first_layer
*
266 lp_tex
->img_stride
[j
];
268 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
269 assert(view
->u
.tex
.last_layer
< res
->array_size
);
273 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
274 mip_ptr
= lp_tex
->data
;
276 /* probably don't really need to fill that out */
281 /* everything specified in number of elements here. */
282 width0
= view
->u
.buf
.last_element
- view
->u
.buf
.first_element
+ 1;
283 addr
= (uint8_t *)addr
+ view
->u
.buf
.first_element
*
285 assert(view
->u
.buf
.first_element
<= view
->u
.buf
.last_element
);
286 assert(view
->u
.buf
.last_element
* view_blocksize
< res
->width0
);
290 /* display target texture/surface */
292 * XXX: Where should this be unmapped?
294 struct llvmpipe_screen
*screen
= llvmpipe_screen(tex
->screen
);
295 struct sw_winsys
*winsys
= screen
->winsys
;
296 addr
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
298 row_stride
[0] = lp_tex
->row_stride
[0];
299 img_stride
[0] = lp_tex
->img_stride
[0];
303 draw_set_mapped_texture(lp
->draw
,
306 width0
, tex
->height0
, num_layers
,
307 first_level
, last_level
,
309 row_stride
, img_stride
, mip_offsets
);
316 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
319 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context
*lp
,
321 struct pipe_sampler_view
**views
)
323 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_VERTEX
,
328 llvmpipe_cleanup_vertex_sampling(struct llvmpipe_context
*ctx
)
331 for (i
= 0; i
< Elements(ctx
->mapped_vs_tex
); i
++) {
332 pipe_resource_reference(&ctx
->mapped_vs_tex
[i
], NULL
);
338 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
341 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context
*lp
,
343 struct pipe_sampler_view
**views
)
345 prepare_shader_sampling(lp
, num
, views
, PIPE_SHADER_GEOMETRY
,
350 llvmpipe_cleanup_geometry_sampling(struct llvmpipe_context
*ctx
)
353 for (i
= 0; i
< Elements(ctx
->mapped_gs_tex
); i
++) {
354 pipe_resource_reference(&ctx
->mapped_gs_tex
[i
], NULL
);
359 llvmpipe_init_sampler_funcs(struct llvmpipe_context
*llvmpipe
)
361 llvmpipe
->pipe
.create_sampler_state
= llvmpipe_create_sampler_state
;
363 llvmpipe
->pipe
.bind_sampler_states
= llvmpipe_bind_sampler_states
;
364 llvmpipe
->pipe
.create_sampler_view
= llvmpipe_create_sampler_view
;
365 llvmpipe
->pipe
.set_sampler_views
= llvmpipe_set_sampler_views
;
366 llvmpipe
->pipe
.sampler_view_destroy
= llvmpipe_sampler_view_destroy
;
367 llvmpipe
->pipe
.delete_sampler_state
= llvmpipe_delete_sampler_state
;