36159a363f42e74e4a66a5d0728907074ca7ecbf
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* Authors:
29 * Brian Paul
30 */
31
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34
35 #include "draw/draw_context.h"
36
37 #include "lp_context.h"
38 #include "lp_screen.h"
39 #include "lp_state.h"
40 #include "lp_debug.h"
41 #include "state_tracker/sw_winsys.h"
42
43
44 static void *
45 llvmpipe_create_sampler_state(struct pipe_context *pipe,
46 const struct pipe_sampler_state *sampler)
47 {
48 struct pipe_sampler_state *state = mem_dup(sampler, sizeof *sampler);
49
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;
53 }
54
55 if (LP_PERF & PERF_NO_MIPMAPS)
56 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
57
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;
61 }
62
63 return state;
64 }
65
66
67 static void
68 llvmpipe_bind_sampler_states(struct pipe_context *pipe,
69 enum pipe_shader_type shader,
70 unsigned start,
71 unsigned num,
72 void **samplers)
73 {
74 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
75 unsigned i;
76
77 assert(shader < PIPE_SHADER_TYPES);
78 assert(start + num <= ARRAY_SIZE(llvmpipe->samplers[shader]));
79
80 draw_flush(llvmpipe->draw);
81
82 /* set the new samplers */
83 for (i = 0; i < num; i++) {
84 llvmpipe->samplers[shader][start + i] = samplers[i];
85 }
86
87 /* find highest non-null samplers[] entry */
88 {
89 unsigned j = MAX2(llvmpipe->num_samplers[shader], start + num);
90 while (j > 0 && llvmpipe->samplers[shader][j - 1] == NULL)
91 j--;
92 llvmpipe->num_samplers[shader] = j;
93 }
94
95 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
96 draw_set_samplers(llvmpipe->draw,
97 shader,
98 llvmpipe->samplers[shader],
99 llvmpipe->num_samplers[shader]);
100 }
101 else if (shader == PIPE_SHADER_COMPUTE) {
102 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER;
103 } else {
104 llvmpipe->dirty |= LP_NEW_SAMPLER;
105 }
106 }
107
108
109 static void
110 llvmpipe_set_sampler_views(struct pipe_context *pipe,
111 enum pipe_shader_type shader,
112 unsigned start,
113 unsigned num,
114 struct pipe_sampler_view **views)
115 {
116 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
117 uint i;
118
119 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
120
121 assert(shader < PIPE_SHADER_TYPES);
122 assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
123
124 draw_flush(llvmpipe->draw);
125
126 /* set the new sampler views */
127 for (i = 0; i < num; i++) {
128 /*
129 * Warn if someone tries to set a view created in a different context
130 * (which is why we need the hack above in the first place).
131 * An assert would be better but st/mesa relies on it...
132 */
133 if (views[i] && views[i]->context != pipe) {
134 debug_printf("Illegal setting of sampler_view %d created in another "
135 "context\n", i);
136 }
137 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
138 views[i]);
139 }
140
141 /* find highest non-null sampler_views[] entry */
142 {
143 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
144 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
145 j--;
146 llvmpipe->num_sampler_views[shader] = j;
147 }
148
149 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
150 draw_set_sampler_views(llvmpipe->draw,
151 shader,
152 llvmpipe->sampler_views[shader],
153 llvmpipe->num_sampler_views[shader]);
154 }
155 else if (shader == PIPE_SHADER_COMPUTE) {
156 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER_VIEW;
157 } else {
158 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
159 }
160 }
161
162
163 static struct pipe_sampler_view *
164 llvmpipe_create_sampler_view(struct pipe_context *pipe,
165 struct pipe_resource *texture,
166 const struct pipe_sampler_view *templ)
167 {
168 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
169 /*
170 * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
171 * This looks unfixable, so fix the bind flags instead when it happens.
172 */
173 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
174 debug_printf("Illegal sampler view creation without bind flag\n");
175 texture->bind |= PIPE_BIND_SAMPLER_VIEW;
176 }
177
178 if (view) {
179 *view = *templ;
180 view->reference.count = 1;
181 view->texture = NULL;
182 pipe_resource_reference(&view->texture, texture);
183 view->context = pipe;
184
185 #ifdef DEBUG
186 /*
187 * This is possibly too lenient, but the primary reason is just
188 * to catch state trackers which forget to initialize this, so
189 * it only catches clearly impossible view targets.
190 */
191 if (view->target != texture->target) {
192 if (view->target == PIPE_TEXTURE_1D)
193 assert(texture->target == PIPE_TEXTURE_1D_ARRAY);
194 else if (view->target == PIPE_TEXTURE_1D_ARRAY)
195 assert(texture->target == PIPE_TEXTURE_1D);
196 else if (view->target == PIPE_TEXTURE_2D)
197 assert(texture->target == PIPE_TEXTURE_2D_ARRAY ||
198 texture->target == PIPE_TEXTURE_CUBE ||
199 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
200 else if (view->target == PIPE_TEXTURE_2D_ARRAY)
201 assert(texture->target == PIPE_TEXTURE_2D ||
202 texture->target == PIPE_TEXTURE_CUBE ||
203 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
204 else if (view->target == PIPE_TEXTURE_CUBE)
205 assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY ||
206 texture->target == PIPE_TEXTURE_2D_ARRAY);
207 else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)
208 assert(texture->target == PIPE_TEXTURE_CUBE ||
209 texture->target == PIPE_TEXTURE_2D_ARRAY);
210 else
211 assert(0);
212 }
213 #endif
214 }
215
216 return view;
217 }
218
219
220 static void
221 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
222 struct pipe_sampler_view *view)
223 {
224 pipe_resource_reference(&view->texture, NULL);
225 FREE(view);
226 }
227
228
229 static void
230 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
231 void *sampler)
232 {
233 FREE( sampler );
234 }
235
236
237 static void
238 prepare_shader_sampling(
239 struct llvmpipe_context *lp,
240 unsigned num,
241 struct pipe_sampler_view **views,
242 enum pipe_shader_type shader_type)
243 {
244
245 unsigned i;
246 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
247 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
248 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
249 const void *addr;
250
251 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
252 if (!num)
253 return;
254
255 for (i = 0; i < num; i++) {
256 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
257
258 if (view) {
259 struct pipe_resource *tex = view->texture;
260 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
261 unsigned width0 = tex->width0;
262 unsigned num_layers = tex->depth0;
263 unsigned first_level = 0;
264 unsigned last_level = 0;
265
266 if (!lp_tex->dt) {
267 /* regular texture - setup array of mipmap level offsets */
268 struct pipe_resource *res = view->texture;
269 int j;
270
271 if (llvmpipe_resource_is_texture(res)) {
272 first_level = view->u.tex.first_level;
273 last_level = view->u.tex.last_level;
274 assert(first_level <= last_level);
275 assert(last_level <= res->last_level);
276 addr = lp_tex->tex_data;
277
278 for (j = first_level; j <= last_level; j++) {
279 mip_offsets[j] = lp_tex->mip_offsets[j];
280 row_stride[j] = lp_tex->row_stride[j];
281 img_stride[j] = lp_tex->img_stride[j];
282 }
283 if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
284 tex->target == PIPE_TEXTURE_2D_ARRAY ||
285 tex->target == PIPE_TEXTURE_CUBE ||
286 tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
287 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
288 for (j = first_level; j <= last_level; j++) {
289 mip_offsets[j] += view->u.tex.first_layer *
290 lp_tex->img_stride[j];
291 }
292 if (view->target == PIPE_TEXTURE_CUBE ||
293 view->target == PIPE_TEXTURE_CUBE_ARRAY) {
294 assert(num_layers % 6 == 0);
295 }
296 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
297 assert(view->u.tex.last_layer < res->array_size);
298 }
299 }
300 else {
301 unsigned view_blocksize = util_format_get_blocksize(view->format);
302 addr = lp_tex->data;
303 /* probably don't really need to fill that out */
304 mip_offsets[0] = 0;
305 row_stride[0] = 0;
306 img_stride[0] = 0;
307
308 /* everything specified in number of elements here. */
309 width0 = view->u.buf.size / view_blocksize;
310 addr = (uint8_t *)addr + view->u.buf.offset;
311 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
312 }
313 }
314 else {
315 /* display target texture/surface */
316 /*
317 * XXX: Where should this be unmapped?
318 */
319 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
320 struct sw_winsys *winsys = screen->winsys;
321 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
322 PIPE_TRANSFER_READ);
323 row_stride[0] = lp_tex->row_stride[0];
324 img_stride[0] = lp_tex->img_stride[0];
325 mip_offsets[0] = 0;
326 assert(addr);
327 }
328 draw_set_mapped_texture(lp->draw,
329 shader_type,
330 i,
331 width0, tex->height0, num_layers,
332 first_level, last_level,
333 addr,
334 row_stride, img_stride, mip_offsets);
335 }
336 }
337 }
338
339
340 /**
341 * Called whenever we're about to draw (no dirty flag, FIXME?).
342 */
343 void
344 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
345 unsigned num,
346 struct pipe_sampler_view **views)
347 {
348 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
349 }
350
351
352 /**
353 * Called whenever we're about to draw (no dirty flag, FIXME?).
354 */
355 void
356 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
357 unsigned num,
358 struct pipe_sampler_view **views)
359 {
360 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
361 }
362
363 static void
364 prepare_shader_images(
365 struct llvmpipe_context *lp,
366 unsigned num,
367 struct pipe_image_view *views,
368 enum pipe_shader_type shader_type)
369 {
370
371 unsigned i;
372 uint32_t row_stride;
373 uint32_t img_stride;
374 const void *addr;
375
376 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
377 if (!num)
378 return;
379
380 for (i = 0; i < num; i++) {
381 struct pipe_image_view *view = i < num ? &views[i] : NULL;
382
383 if (view) {
384 struct pipe_resource *img = view->resource;
385 struct llvmpipe_resource *lp_img = llvmpipe_resource(img);
386 if (!img)
387 continue;
388
389 unsigned width0 = img->width0;
390 unsigned num_layers = img->depth0;
391
392 if (!lp_img->dt) {
393 /* regular texture - setup array of mipmap level offsets */
394 struct pipe_resource *res = view->resource;
395
396 if (llvmpipe_resource_is_texture(res)) {
397 uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level];
398 addr = lp_img->tex_data;
399
400 if (img->target == PIPE_TEXTURE_1D_ARRAY ||
401 img->target == PIPE_TEXTURE_2D_ARRAY ||
402 img->target == PIPE_TEXTURE_3D ||
403 img->target == PIPE_TEXTURE_CUBE ||
404 img->target == PIPE_TEXTURE_CUBE_ARRAY) {
405 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
406 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
407 mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level];
408 }
409
410 row_stride = lp_img->row_stride[view->u.tex.level];
411 img_stride = lp_img->img_stride[view->u.tex.level];
412 addr = (uint8_t *)addr + mip_offset;
413 }
414 else {
415 unsigned view_blocksize = util_format_get_blocksize(view->format);
416 addr = lp_img->data;
417 /* probably don't really need to fill that out */
418 row_stride = 0;
419 img_stride = 0;
420
421 /* everything specified in number of elements here. */
422 width0 = view->u.buf.size / view_blocksize;
423 addr = (uint8_t *)addr + view->u.buf.offset;
424 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
425 }
426 }
427 else {
428 /* display target texture/surface */
429 /*
430 * XXX: Where should this be unmapped?
431 */
432 struct llvmpipe_screen *screen = llvmpipe_screen(img->screen);
433 struct sw_winsys *winsys = screen->winsys;
434 addr = winsys->displaytarget_map(winsys, lp_img->dt,
435 PIPE_TRANSFER_READ);
436 row_stride = lp_img->row_stride[0];
437 img_stride = lp_img->img_stride[0];
438 assert(addr);
439 }
440 draw_set_mapped_image(lp->draw,
441 shader_type,
442 i,
443 width0, img->height0, num_layers,
444 addr,
445 row_stride, img_stride);
446 }
447 }
448 }
449
450
451 /**
452 * Called whenever we're about to draw (no dirty flag, FIXME?).
453 */
454 void
455 llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp,
456 unsigned num,
457 struct pipe_image_view *views)
458 {
459 prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX);
460 }
461
462
463 /**
464 * Called whenever we're about to draw (no dirty flag, FIXME?).
465 */
466 void
467 llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp,
468 unsigned num,
469 struct pipe_image_view *views)
470 {
471 prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY);
472 }
473
474 void
475 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
476 {
477 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
478
479 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
480 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
481 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
482 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
483 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
484 }