clover: Define helper classes for the new object model.
[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 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
140 views[i]);
141 }
142
143 /* find highest non-null sampler_views[] entry */
144 {
145 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
146 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
147 j--;
148 llvmpipe->num_sampler_views[shader] = j;
149 }
150
151 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
152 draw_set_sampler_views(llvmpipe->draw,
153 shader,
154 llvmpipe->sampler_views[shader],
155 llvmpipe->num_sampler_views[shader]);
156 }
157
158 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
159 }
160
161
162 static void
163 llvmpipe_set_fragment_sampler_views(struct pipe_context *pipe,
164 unsigned num,
165 struct pipe_sampler_view **views)
166 {
167 llvmpipe_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num, views);
168 }
169
170
171 static void
172 llvmpipe_set_vertex_sampler_views(struct pipe_context *pipe,
173 unsigned num,
174 struct pipe_sampler_view **views)
175 {
176 llvmpipe_set_sampler_views(pipe, PIPE_SHADER_VERTEX, 0, num, views);
177 }
178
179
180 static void
181 llvmpipe_set_geometry_sampler_views(struct pipe_context *pipe,
182 unsigned num,
183 struct pipe_sampler_view **views)
184 {
185 llvmpipe_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, 0, num, views);
186 }
187
188 static struct pipe_sampler_view *
189 llvmpipe_create_sampler_view(struct pipe_context *pipe,
190 struct pipe_resource *texture,
191 const struct pipe_sampler_view *templ)
192 {
193 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
194 /*
195 * XXX we REALLY want to see the correct bind flag here but the OpenGL
196 * state tracker can't guarantee that at least for texture buffer objects.
197 */
198 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW))
199 debug_printf("Illegal sampler view creation without bind flag\n");
200
201 if (view) {
202 *view = *templ;
203 view->reference.count = 1;
204 view->texture = NULL;
205 pipe_resource_reference(&view->texture, texture);
206 view->context = pipe;
207 }
208
209 return view;
210 }
211
212
213 static void
214 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
215 struct pipe_sampler_view *view)
216 {
217 pipe_resource_reference(&view->texture, NULL);
218 FREE(view);
219 }
220
221
222 static void
223 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
224 void *sampler)
225 {
226 FREE( sampler );
227 }
228
229
230 static void
231 prepare_shader_sampling(
232 struct llvmpipe_context *lp,
233 unsigned num,
234 struct pipe_sampler_view **views,
235 unsigned shader_type,
236 struct pipe_resource *mapped_tex[PIPE_MAX_SHADER_SAMPLER_VIEWS])
237 {
238
239 unsigned i;
240 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
241 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
242 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
243 const void *addr;
244
245 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
246 if (!num)
247 return;
248
249 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
250 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
251
252 if (view) {
253 struct pipe_resource *tex = view->texture;
254 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
255 unsigned width0 = tex->width0;
256 unsigned num_layers = tex->depth0;
257 unsigned first_level = 0;
258 unsigned last_level = 0;
259
260 /* We're referencing the texture's internal data, so save a
261 * reference to it.
262 */
263 pipe_resource_reference(&mapped_tex[i], tex);
264
265 if (!lp_tex->dt) {
266 /* regular texture - setup array of mipmap level offsets */
267 struct pipe_resource *res = view->texture;
268 int j;
269 void *mip_ptr;
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
277 /* must trigger allocation first before we can get base ptr */
278 /* XXX this may fail due to OOM ? */
279 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, view->u.tex.first_level,
280 LP_TEX_USAGE_READ);
281 addr = lp_tex->linear_img.data;
282
283 for (j = first_level; j <= last_level; j++) {
284 mip_ptr = llvmpipe_get_texture_image_all(lp_tex, j,
285 LP_TEX_USAGE_READ);
286 mip_offsets[j] = (uint8_t *)mip_ptr - (uint8_t *)addr;
287 /*
288 * could get mip offset directly but need call above to
289 * invoke tiled->linear conversion.
290 */
291 assert(lp_tex->linear_mip_offsets[j] == mip_offsets[j]);
292 row_stride[j] = lp_tex->row_stride[j];
293 img_stride[j] = lp_tex->img_stride[j];
294 }
295 if (res->target == PIPE_TEXTURE_1D_ARRAY ||
296 res->target == PIPE_TEXTURE_2D_ARRAY) {
297 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
298 for (j = first_level; j <= last_level; j++) {
299 mip_offsets[j] += view->u.tex.first_layer *
300 lp_tex->img_stride[j];
301 }
302 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
303 assert(view->u.tex.last_layer < res->array_size);
304 }
305 }
306 else {
307 unsigned view_blocksize = util_format_get_blocksize(view->format);
308 mip_ptr = lp_tex->data;
309 addr = mip_ptr;
310 /* probably don't really need to fill that out */
311 mip_offsets[0] = 0;
312 row_stride[0] = 0;
313 row_stride[0] = 0;
314
315 /* everything specified in number of elements here. */
316 width0 = view->u.buf.last_element - view->u.buf.first_element + 1;
317 addr = (uint8_t *)addr + view->u.buf.first_element *
318 view_blocksize;
319 assert(view->u.buf.first_element <= view->u.buf.last_element);
320 assert(view->u.buf.last_element * view_blocksize < res->width0);
321 }
322 }
323 else {
324 /* display target texture/surface */
325 /*
326 * XXX: Where should this be unmapped?
327 */
328 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
329 struct sw_winsys *winsys = screen->winsys;
330 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
331 PIPE_TRANSFER_READ);
332 row_stride[0] = lp_tex->row_stride[0];
333 img_stride[0] = lp_tex->img_stride[0];
334 mip_offsets[0] = 0;
335 assert(addr);
336 }
337 draw_set_mapped_texture(lp->draw,
338 shader_type,
339 i,
340 width0, tex->height0, num_layers,
341 first_level, last_level,
342 addr,
343 row_stride, img_stride, mip_offsets);
344 }
345 }
346 }
347
348
349 /**
350 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
351 */
352 void
353 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
354 unsigned num,
355 struct pipe_sampler_view **views)
356 {
357 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX,
358 lp->mapped_vs_tex);
359 }
360
361 void
362 llvmpipe_cleanup_vertex_sampling(struct llvmpipe_context *ctx)
363 {
364 unsigned i;
365 for (i = 0; i < Elements(ctx->mapped_vs_tex); i++) {
366 pipe_resource_reference(&ctx->mapped_vs_tex[i], NULL);
367 }
368 }
369
370
371 /**
372 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
373 */
374 void
375 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
376 unsigned num,
377 struct pipe_sampler_view **views)
378 {
379 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY,
380 lp->mapped_gs_tex);
381 }
382
383 void
384 llvmpipe_cleanup_geometry_sampling(struct llvmpipe_context *ctx)
385 {
386 unsigned i;
387 for (i = 0; i < Elements(ctx->mapped_gs_tex); i++) {
388 pipe_resource_reference(&ctx->mapped_gs_tex[i], NULL);
389 }
390 }
391
392 void
393 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
394 {
395 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
396
397 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
398 llvmpipe->pipe.set_fragment_sampler_views = llvmpipe_set_fragment_sampler_views;
399 llvmpipe->pipe.set_vertex_sampler_views = llvmpipe_set_vertex_sampler_views;
400 llvmpipe->pipe.set_geometry_sampler_views = llvmpipe_set_geometry_sampler_views;
401 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
402 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
403 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
404 }