llvmpipe: handle NULL color buffer pointers
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 unsigned 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 <= Elements(llvmpipe->samplers[shader]));
79
80 /* Check for no-op */
81 if (start + num <= llvmpipe->num_samplers[shader] &&
82 !memcmp(llvmpipe->samplers[shader] + start, samplers,
83 num * sizeof(void *))) {
84 return;
85 }
86
87 draw_flush(llvmpipe->draw);
88
89 /* set the new samplers */
90 for (i = 0; i < num; i++) {
91 llvmpipe->samplers[shader][start + i] = samplers[i];
92 }
93
94 /* find highest non-null samplers[] entry */
95 {
96 unsigned j = MAX2(llvmpipe->num_samplers[shader], start + num);
97 while (j > 0 && llvmpipe->samplers[shader][j - 1] == NULL)
98 j--;
99 llvmpipe->num_samplers[shader] = j;
100 }
101
102 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
103 draw_set_samplers(llvmpipe->draw,
104 shader,
105 llvmpipe->samplers[shader],
106 llvmpipe->num_samplers[shader]);
107 }
108
109 llvmpipe->dirty |= LP_NEW_SAMPLER;
110 }
111
112
113 static void
114 llvmpipe_set_sampler_views(struct pipe_context *pipe,
115 unsigned shader,
116 unsigned start,
117 unsigned num,
118 struct pipe_sampler_view **views)
119 {
120 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
121 uint i;
122
123 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
124
125 assert(shader < PIPE_SHADER_TYPES);
126 assert(start + num <= Elements(llvmpipe->sampler_views[shader]));
127
128 /* Check for no-op */
129 if (start + num <= llvmpipe->num_sampler_views[shader] &&
130 !memcmp(llvmpipe->sampler_views[shader] + start, views,
131 num * sizeof(struct pipe_sampler_view *))) {
132 return;
133 }
134
135 draw_flush(llvmpipe->draw);
136
137 /* set the new sampler views */
138 for (i = 0; i < num; i++) {
139 /* Note: we're using pipe_sampler_view_release() here to work around
140 * a possible crash when the old view belongs to another context that
141 * was already destroyed.
142 */
143 pipe_sampler_view_release(pipe,
144 &llvmpipe->sampler_views[shader][start + i]);
145 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
146 views[i]);
147 }
148
149 /* find highest non-null sampler_views[] entry */
150 {
151 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
152 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
153 j--;
154 llvmpipe->num_sampler_views[shader] = j;
155 }
156
157 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
158 draw_set_sampler_views(llvmpipe->draw,
159 shader,
160 llvmpipe->sampler_views[shader],
161 llvmpipe->num_sampler_views[shader]);
162 }
163
164 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
165 }
166
167
168 static struct pipe_sampler_view *
169 llvmpipe_create_sampler_view(struct pipe_context *pipe,
170 struct pipe_resource *texture,
171 const struct pipe_sampler_view *templ)
172 {
173 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
174 /*
175 * XXX we REALLY want to see the correct bind flag here but the OpenGL
176 * state tracker can't guarantee that at least for texture buffer objects.
177 */
178 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW))
179 debug_printf("Illegal sampler view creation without bind flag\n");
180
181 if (view) {
182 *view = *templ;
183 view->reference.count = 1;
184 view->texture = NULL;
185 pipe_resource_reference(&view->texture, texture);
186 view->context = pipe;
187 }
188
189 return view;
190 }
191
192
193 static void
194 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
195 struct pipe_sampler_view *view)
196 {
197 pipe_resource_reference(&view->texture, NULL);
198 FREE(view);
199 }
200
201
202 static void
203 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
204 void *sampler)
205 {
206 FREE( sampler );
207 }
208
209
210 static void
211 prepare_shader_sampling(
212 struct llvmpipe_context *lp,
213 unsigned num,
214 struct pipe_sampler_view **views,
215 unsigned shader_type,
216 struct pipe_resource *mapped_tex[PIPE_MAX_SHADER_SAMPLER_VIEWS])
217 {
218
219 unsigned i;
220 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
221 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
222 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
223 const void *addr;
224
225 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
226 if (!num)
227 return;
228
229 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
230 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
231
232 if (view) {
233 struct pipe_resource *tex = view->texture;
234 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
235 unsigned width0 = tex->width0;
236 unsigned num_layers = tex->depth0;
237 unsigned first_level = 0;
238 unsigned last_level = 0;
239
240 /* We're referencing the texture's internal data, so save a
241 * reference to it.
242 */
243 pipe_resource_reference(&mapped_tex[i], tex);
244
245 if (!lp_tex->dt) {
246 /* regular texture - setup array of mipmap level offsets */
247 struct pipe_resource *res = view->texture;
248 int j;
249 void *mip_ptr;
250
251 if (llvmpipe_resource_is_texture(res)) {
252 first_level = view->u.tex.first_level;
253 last_level = view->u.tex.last_level;
254 assert(first_level <= last_level);
255 assert(last_level <= res->last_level);
256
257 /* must trigger allocation first before we can get base ptr */
258 /* XXX this may fail due to OOM ? */
259 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, view->u.tex.first_level,
260 LP_TEX_USAGE_READ);
261 addr = lp_tex->linear_img.data;
262
263 for (j = first_level; j <= last_level; j++) {
264 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, j,
265 LP_TEX_USAGE_READ);
266 mip_offsets[j] = (uint8_t *)mip_ptr - (uint8_t *)addr;
267 /*
268 * could get mip offset directly but need call above to
269 * invoke tiled->linear conversion.
270 */
271 assert(lp_tex->linear_mip_offsets[j] == mip_offsets[j]);
272 row_stride[j] = lp_tex->row_stride[j];
273 img_stride[j] = lp_tex->img_stride[j];
274 }
275 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
276 res->target == PIPE_TEXTURE_2D_ARRAY) {
277 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
278 for (j = first_level; j <= last_level; j++) {
279 mip_offsets[j] += view->u.tex.first_layer *
280 lp_tex->img_stride[j];
281 }
282 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
283 assert(view->u.tex.last_layer < res->array_size);
284 }
285 }
286 else {
287 unsigned view_blocksize = util_format_get_blocksize(view->format);
288 mip_ptr = lp_tex->data;
289 addr = mip_ptr;
290 /* probably don't really need to fill that out */
291 mip_offsets[0] = 0;
292 row_stride[0] = 0;
293 row_stride[0] = 0;
294
295 /* everything specified in number of elements here. */
296 width0 = view->u.buf.last_element - view->u.buf.first_element + 1;
297 addr = (uint8_t *)addr + view->u.buf.first_element *
298 view_blocksize;
299 assert(view->u.buf.first_element <= view->u.buf.last_element);
300 assert(view->u.buf.last_element * view_blocksize < res->width0);
301 }
302 }
303 else {
304 /* display target texture/surface */
305 /*
306 * XXX: Where should this be unmapped?
307 */
308 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
309 struct sw_winsys *winsys = screen->winsys;
310 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
311 PIPE_TRANSFER_READ);
312 row_stride[0] = lp_tex->row_stride[0];
313 img_stride[0] = lp_tex->img_stride[0];
314 mip_offsets[0] = 0;
315 assert(addr);
316 }
317 draw_set_mapped_texture(lp->draw,
318 shader_type,
319 i,
320 width0, tex->height0, num_layers,
321 first_level, last_level,
322 addr,
323 row_stride, img_stride, mip_offsets);
324 }
325 }
326 }
327
328
329 /**
330 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
331 */
332 void
333 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
334 unsigned num,
335 struct pipe_sampler_view **views)
336 {
337 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX,
338 lp->mapped_vs_tex);
339 }
340
341 void
342 llvmpipe_cleanup_vertex_sampling(struct llvmpipe_context *ctx)
343 {
344 unsigned i;
345 for (i = 0; i < Elements(ctx->mapped_vs_tex); i++) {
346 pipe_resource_reference(&ctx->mapped_vs_tex[i], NULL);
347 }
348 }
349
350
351 /**
352 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
353 */
354 void
355 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
356 unsigned num,
357 struct pipe_sampler_view **views)
358 {
359 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY,
360 lp->mapped_gs_tex);
361 }
362
363 void
364 llvmpipe_cleanup_geometry_sampling(struct llvmpipe_context *ctx)
365 {
366 unsigned i;
367 for (i = 0; i < Elements(ctx->mapped_gs_tex); i++) {
368 pipe_resource_reference(&ctx->mapped_gs_tex[i], NULL);
369 }
370 }
371
372 void
373 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
374 {
375 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
376
377 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
378 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
379 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
380 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
381 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
382 }