intel/fs: Drop the gl_program from fs_visitor
[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 {
102 llvmpipe->dirty |= LP_NEW_SAMPLER;
103 }
104 }
105
106
107 static void
108 llvmpipe_set_sampler_views(struct pipe_context *pipe,
109 enum pipe_shader_type shader,
110 unsigned start,
111 unsigned num,
112 struct pipe_sampler_view **views)
113 {
114 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
115 uint i;
116
117 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
118
119 assert(shader < PIPE_SHADER_TYPES);
120 assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
121
122 draw_flush(llvmpipe->draw);
123
124 /* set the new sampler views */
125 for (i = 0; i < num; i++) {
126 /*
127 * Warn if someone tries to set a view created in a different context
128 * (which is why we need the hack above in the first place).
129 * An assert would be better but st/mesa relies on it...
130 */
131 if (views[i] && views[i]->context != pipe) {
132 debug_printf("Illegal setting of sampler_view %d created in another "
133 "context\n", i);
134 }
135 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
136 views[i]);
137 }
138
139 /* find highest non-null sampler_views[] entry */
140 {
141 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
142 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
143 j--;
144 llvmpipe->num_sampler_views[shader] = j;
145 }
146
147 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
148 draw_set_sampler_views(llvmpipe->draw,
149 shader,
150 llvmpipe->sampler_views[shader],
151 llvmpipe->num_sampler_views[shader]);
152 }
153 else {
154 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
155 }
156 }
157
158
159 static struct pipe_sampler_view *
160 llvmpipe_create_sampler_view(struct pipe_context *pipe,
161 struct pipe_resource *texture,
162 const struct pipe_sampler_view *templ)
163 {
164 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
165 /*
166 * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
167 * This looks unfixable, so fix the bind flags instead when it happens.
168 */
169 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
170 debug_printf("Illegal sampler view creation without bind flag\n");
171 texture->bind |= PIPE_BIND_SAMPLER_VIEW;
172 }
173
174 if (view) {
175 *view = *templ;
176 view->reference.count = 1;
177 view->texture = NULL;
178 pipe_resource_reference(&view->texture, texture);
179 view->context = pipe;
180
181 #ifdef DEBUG
182 /*
183 * This is possibly too lenient, but the primary reason is just
184 * to catch state trackers which forget to initialize this, so
185 * it only catches clearly impossible view targets.
186 */
187 if (view->target != texture->target) {
188 if (view->target == PIPE_TEXTURE_1D)
189 assert(texture->target == PIPE_TEXTURE_1D_ARRAY);
190 else if (view->target == PIPE_TEXTURE_1D_ARRAY)
191 assert(texture->target == PIPE_TEXTURE_1D);
192 else if (view->target == PIPE_TEXTURE_2D)
193 assert(texture->target == PIPE_TEXTURE_2D_ARRAY ||
194 texture->target == PIPE_TEXTURE_CUBE ||
195 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
196 else if (view->target == PIPE_TEXTURE_2D_ARRAY)
197 assert(texture->target == PIPE_TEXTURE_2D ||
198 texture->target == PIPE_TEXTURE_CUBE ||
199 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
200 else if (view->target == PIPE_TEXTURE_CUBE)
201 assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY ||
202 texture->target == PIPE_TEXTURE_2D_ARRAY);
203 else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)
204 assert(texture->target == PIPE_TEXTURE_CUBE ||
205 texture->target == PIPE_TEXTURE_2D_ARRAY);
206 else
207 assert(0);
208 }
209 #endif
210 }
211
212 return view;
213 }
214
215
216 static void
217 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
218 struct pipe_sampler_view *view)
219 {
220 pipe_resource_reference(&view->texture, NULL);
221 FREE(view);
222 }
223
224
225 static void
226 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
227 void *sampler)
228 {
229 FREE( sampler );
230 }
231
232
233 static void
234 prepare_shader_sampling(
235 struct llvmpipe_context *lp,
236 unsigned num,
237 struct pipe_sampler_view **views,
238 enum pipe_shader_type shader_type)
239 {
240
241 unsigned i;
242 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
243 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
244 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
245 const void *addr;
246
247 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
248 if (!num)
249 return;
250
251 for (i = 0; i < num; i++) {
252 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
253
254 if (view) {
255 struct pipe_resource *tex = view->texture;
256 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
257 unsigned width0 = tex->width0;
258 unsigned num_layers = tex->depth0;
259 unsigned first_level = 0;
260 unsigned last_level = 0;
261
262 if (!lp_tex->dt) {
263 /* regular texture - setup array of mipmap level offsets */
264 struct pipe_resource *res = view->texture;
265 int j;
266
267 if (llvmpipe_resource_is_texture(res)) {
268 first_level = view->u.tex.first_level;
269 last_level = view->u.tex.last_level;
270 assert(first_level <= last_level);
271 assert(last_level <= res->last_level);
272 addr = lp_tex->tex_data;
273
274 for (j = first_level; j <= last_level; j++) {
275 mip_offsets[j] = lp_tex->mip_offsets[j];
276 row_stride[j] = lp_tex->row_stride[j];
277 img_stride[j] = lp_tex->img_stride[j];
278 }
279 if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
280 tex->target == PIPE_TEXTURE_2D_ARRAY ||
281 tex->target == PIPE_TEXTURE_CUBE ||
282 tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
283 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
284 for (j = first_level; j <= last_level; j++) {
285 mip_offsets[j] += view->u.tex.first_layer *
286 lp_tex->img_stride[j];
287 }
288 if (view->target == PIPE_TEXTURE_CUBE ||
289 view->target == PIPE_TEXTURE_CUBE_ARRAY) {
290 assert(num_layers % 6 == 0);
291 }
292 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
293 assert(view->u.tex.last_layer < res->array_size);
294 }
295 }
296 else {
297 unsigned view_blocksize = util_format_get_blocksize(view->format);
298 addr = lp_tex->data;
299 /* probably don't really need to fill that out */
300 mip_offsets[0] = 0;
301 row_stride[0] = 0;
302 img_stride[0] = 0;
303
304 /* everything specified in number of elements here. */
305 width0 = view->u.buf.size / view_blocksize;
306 addr = (uint8_t *)addr + view->u.buf.offset;
307 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
308 }
309 }
310 else {
311 /* display target texture/surface */
312 /*
313 * XXX: Where should this be unmapped?
314 */
315 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
316 struct sw_winsys *winsys = screen->winsys;
317 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
318 PIPE_TRANSFER_READ);
319 row_stride[0] = lp_tex->row_stride[0];
320 img_stride[0] = lp_tex->img_stride[0];
321 mip_offsets[0] = 0;
322 assert(addr);
323 }
324 draw_set_mapped_texture(lp->draw,
325 shader_type,
326 i,
327 width0, tex->height0, num_layers,
328 first_level, last_level,
329 addr,
330 row_stride, img_stride, mip_offsets);
331 }
332 }
333 }
334
335
336 /**
337 * Called whenever we're about to draw (no dirty flag, FIXME?).
338 */
339 void
340 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
341 unsigned num,
342 struct pipe_sampler_view **views)
343 {
344 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
345 }
346
347
348 /**
349 * Called whenever we're about to draw (no dirty flag, FIXME?).
350 */
351 void
352 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
353 unsigned num,
354 struct pipe_sampler_view **views)
355 {
356 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
357 }
358
359
360 void
361 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
362 {
363 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
364
365 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
366 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
367 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
368 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
369 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
370 }