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