st/mesa: support samplers for Selection/Feedback/RasterPos
[mesa.git] / src / mesa / state_tracker / st_draw_feedback.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 #include "main/imports.h"
29 #include "main/arrayobj.h"
30 #include "main/image.h"
31 #include "main/macros.h"
32 #include "main/varray.h"
33
34 #include "vbo/vbo.h"
35
36 #include "st_context.h"
37 #include "st_atom.h"
38 #include "st_cb_bitmap.h"
39 #include "st_cb_bufferobjects.h"
40 #include "st_draw.h"
41 #include "st_program.h"
42 #include "st_util.h"
43
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "util/u_inlines.h"
47 #include "util/u_draw.h"
48 #include "util/format/u_format.h"
49
50 #include "draw/draw_private.h"
51 #include "draw/draw_context.h"
52
53
54 /**
55 * Set the (private) draw module's post-transformed vertex format when in
56 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
57 */
58 static void
59 set_feedback_vertex_format(struct gl_context *ctx)
60 {
61 #if 0
62 struct st_context *st = st_context(ctx);
63 struct vertex_info vinfo;
64 GLuint i;
65
66 memset(&vinfo, 0, sizeof(vinfo));
67
68 if (ctx->RenderMode == GL_SELECT) {
69 assert(ctx->RenderMode == GL_SELECT);
70 vinfo.num_attribs = 1;
71 vinfo.format[0] = FORMAT_4F;
72 vinfo.interp_mode[0] = INTERP_LINEAR;
73 }
74 else {
75 /* GL_FEEDBACK, or glRasterPos */
76 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
77 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
78 for (i = 0; i < vinfo.num_attribs; i++) {
79 vinfo.format[i] = FORMAT_4F;
80 vinfo.interp_mode[i] = INTERP_LINEAR;
81 }
82 }
83
84 draw_set_vertex_info(st->draw, &vinfo);
85 #endif
86 }
87
88
89 /**
90 * Called by VBO to draw arrays when in selection or feedback mode and
91 * to implement glRasterPos.
92 * This function mirrors the normal st_draw_vbo().
93 * Look at code refactoring some day.
94 */
95 void
96 st_feedback_draw_vbo(struct gl_context *ctx,
97 const struct _mesa_prim *prims,
98 GLuint nr_prims,
99 const struct _mesa_index_buffer *ib,
100 GLboolean index_bounds_valid,
101 GLuint min_index,
102 GLuint max_index,
103 struct gl_transform_feedback_object *tfb_vertcount,
104 unsigned stream,
105 struct gl_buffer_object *indirect)
106 {
107 struct st_context *st = st_context(ctx);
108 struct pipe_context *pipe = st->pipe;
109 struct draw_context *draw = st_get_draw_context(st);
110 const struct st_vertex_program *vp;
111 struct st_vp_variant *vp_variant;
112 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
113 unsigned num_vbuffers = 0;
114 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
115 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
116 struct pipe_transfer *ib_transfer = NULL;
117 GLuint i;
118 const void *mapped_indices = NULL;
119 struct pipe_draw_info info;
120
121 if (!draw)
122 return;
123
124 /* Initialize pipe_draw_info. */
125 info.primitive_restart = false;
126 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
127 info.indirect = NULL;
128 info.count_from_stream_output = NULL;
129 info.restart_index = 0;
130
131 st_flush_bitmap_cache(st);
132 st_invalidate_readpix_cache(st);
133
134 st_validate_state(st, ST_PIPELINE_RENDER);
135
136 if (!index_bounds_valid)
137 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
138
139 /* must get these after state validation! */
140 vp = (struct st_vertex_program *)st->vp;
141 vp_variant = st->vp_variant;
142
143 struct pipe_shader_state state = {0};
144 state.type = PIPE_SHADER_IR_TGSI;
145 state.tokens = vp_variant->tokens;
146
147 if (!vp_variant->draw_shader) {
148 vp_variant->draw_shader = draw_create_vertex_shader(draw, &state);
149 }
150
151 /*
152 * Set up the draw module's state.
153 *
154 * We'd like to do this less frequently, but the normal state-update
155 * code sends state updates to the pipe, not to our private draw module.
156 */
157 assert(draw);
158 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
159 draw_set_clip_state(draw, &st->state.clip);
160 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
161 draw_bind_vertex_shader(draw, vp_variant->draw_shader);
162 set_feedback_vertex_format(ctx);
163
164 /* Must setup these after state validation! */
165 /* Setup arrays */
166 st_setup_arrays(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
167 /* Setup current values as userspace arrays */
168 st_setup_current_user(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
169
170 /* Map all buffers and tell draw about their mapping */
171 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
172 struct pipe_vertex_buffer *vbuffer = &vbuffers[buf];
173
174 if (vbuffer->is_user_buffer) {
175 draw_set_mapped_vertex_buffer(draw, buf, vbuffer->buffer.user, ~0);
176 } else {
177 void *map = pipe_buffer_map(pipe, vbuffer->buffer.resource,
178 PIPE_TRANSFER_READ, &vb_transfer[buf]);
179 draw_set_mapped_vertex_buffer(draw, buf, map,
180 vbuffer->buffer.resource->width0);
181 }
182 }
183
184 draw_set_vertex_buffers(draw, 0, num_vbuffers, vbuffers);
185 draw_set_vertex_elements(draw, vp->num_inputs, velements);
186
187 unsigned start = 0;
188
189 if (ib) {
190 struct gl_buffer_object *bufobj = ib->obj;
191 unsigned index_size = ib->index_size;
192
193 if (index_size == 0)
194 goto out_unref_vertex;
195
196 if (bufobj && bufobj->Name) {
197 struct st_buffer_object *stobj = st_buffer_object(bufobj);
198
199 start = pointer_to_offset(ib->ptr) / index_size;
200 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
201 PIPE_TRANSFER_READ, &ib_transfer);
202 }
203 else {
204 mapped_indices = ib->ptr;
205 }
206
207 info.index_size = ib->index_size;
208 info.min_index = min_index;
209 info.max_index = max_index;
210 info.has_user_indices = true;
211 info.index.user = mapped_indices;
212
213 draw_set_indexes(draw,
214 (ubyte *) mapped_indices,
215 index_size, ~0);
216
217 if (ctx->Array._PrimitiveRestart) {
218 info.primitive_restart = true;
219 info.restart_index = _mesa_primitive_restart_index(ctx, info.index_size);
220 }
221 } else {
222 info.index_size = 0;
223 info.has_user_indices = false;
224 }
225
226 /* set constant buffers */
227 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
228 st->state.constants[PIPE_SHADER_VERTEX].ptr,
229 st->state.constants[PIPE_SHADER_VERTEX].size);
230
231 const struct gl_program *prog = &vp->Base.Base;
232 struct pipe_transfer *ubo_transfer[PIPE_MAX_CONSTANT_BUFFERS] = {0};
233 assert(prog->info.num_ubos <= ARRAY_SIZE(ubo_transfer));
234
235 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
236 struct gl_buffer_binding *binding =
237 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
238 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
239 struct pipe_resource *buf = st_obj->buffer;
240
241 if (!buf)
242 continue;
243
244 unsigned offset = binding->Offset;
245 unsigned size = buf->width0 - offset;
246
247 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
248 * Take the minimum just to be sure.
249 */
250 if (!binding->AutomaticSize)
251 size = MIN2(size, (unsigned) binding->Size);
252
253 void *ptr = pipe_buffer_map_range(pipe, buf, offset, size,
254 PIPE_TRANSFER_READ, &ubo_transfer[i]);
255
256 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i, ptr,
257 size);
258 }
259
260 /* samplers */
261 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
262 for (unsigned i = 0; i < st->state.num_vert_samplers; i++)
263 samplers[i] = &st->state.vert_samplers[i];
264
265 draw_set_samplers(draw, PIPE_SHADER_VERTEX, samplers,
266 st->state.num_vert_samplers);
267
268 /* sampler views */
269 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX,
270 st->state.vert_sampler_views,
271 st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
272
273 struct pipe_transfer *sv_transfer[PIPE_MAX_SAMPLERS][PIPE_MAX_TEXTURE_LEVELS];
274
275 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
276 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
277 if (!view)
278 continue;
279
280 struct pipe_resource *res = view->texture;
281 unsigned width0 = res->width0;
282 unsigned num_layers = res->depth0;
283 unsigned first_level = 0;
284 unsigned last_level = 0;
285 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
286 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
287 uint32_t mip_offset[PIPE_MAX_TEXTURE_LEVELS];
288 uintptr_t mip_addr[PIPE_MAX_TEXTURE_LEVELS];
289 uintptr_t base_addr;
290
291 if (res->target != PIPE_BUFFER) {
292 first_level = view->u.tex.first_level;
293 last_level = view->u.tex.last_level;
294 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
295 base_addr = UINTPTR_MAX;
296
297 for (unsigned j = first_level; j <= last_level; j++) {
298 unsigned map_layers = res->target == PIPE_TEXTURE_3D ?
299 util_num_layers(res, j) : num_layers;
300
301 sv_transfer[i][j] = NULL;
302 mip_addr[j] = (uintptr_t)
303 pipe_transfer_map_3d(pipe, res, j,
304 PIPE_TRANSFER_READ, 0, 0,
305 view->u.tex.first_layer,
306 u_minify(res->width0, j),
307 u_minify(res->height0, j),
308 map_layers, &sv_transfer[i][j]);
309 row_stride[j] = sv_transfer[i][j]->stride;
310 img_stride[j] = sv_transfer[i][j]->layer_stride;
311
312 /* Get the minimum address, because the draw module takes only
313 * 1 address for the whole texture + uint32 offsets for mip levels,
314 * so we need to convert mapped resource pointers into that scheme.
315 */
316 base_addr = MIN2(base_addr, mip_addr[j]);
317 }
318 for (unsigned j = first_level; j <= last_level; j++) {
319 /* TODO: The draw module should accept pointers for mipmap levels
320 * instead of offsets. This is unlikely to work on 64-bit archs.
321 */
322 assert(mip_addr[j] - base_addr <= UINT32_MAX);
323 mip_offset[j] = mip_addr[j] - base_addr;
324 }
325 } else {
326 width0 = view->u.buf.size / util_format_get_blocksize(view->format);
327
328 /* probably don't really need to fill that out */
329 mip_offset[0] = 0;
330 row_stride[0] = 0;
331 img_stride[0] = 0;
332
333 sv_transfer[i][0] = NULL;
334 base_addr = (uintptr_t)
335 pipe_buffer_map_range(pipe, res, view->u.buf.offset,
336 view->u.buf.size,
337 PIPE_TRANSFER_READ,
338 &sv_transfer[i][0]);
339 }
340
341 draw_set_mapped_texture(draw, PIPE_SHADER_VERTEX, i, width0,
342 res->height0, num_layers, first_level,
343 last_level, (void*)base_addr, row_stride,
344 img_stride, mip_offset);
345 }
346
347 /* draw here */
348 for (i = 0; i < nr_prims; i++) {
349 info.count = prims[i].count;
350
351 if (!info.count)
352 continue;
353
354 info.mode = prims[i].mode;
355 info.start = start + prims[i].start;
356 info.start_instance = prims[i].base_instance;
357 info.instance_count = prims[i].num_instances;
358 info.index_bias = prims[i].basevertex;
359 info.drawid = prims[i].draw_id;
360 if (!ib) {
361 info.min_index = info.start;
362 info.max_index = info.start + info.count - 1;
363 }
364
365 draw_vbo(draw, &info);
366 }
367
368 /* unmap sampler views */
369 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
370 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
371
372 if (view) {
373 if (view->texture->target != PIPE_BUFFER) {
374 for (unsigned j = view->u.tex.first_level;
375 j <= view->u.tex.last_level; j++) {
376 pipe_transfer_unmap(pipe, sv_transfer[i][j]);
377 }
378 } else {
379 pipe_transfer_unmap(pipe, sv_transfer[i][0]);
380 }
381 }
382 }
383
384 draw_set_samplers(draw, PIPE_SHADER_VERTEX, NULL, 0);
385 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX, NULL, 0);
386
387 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
388 if (ubo_transfer[i]) {
389 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i,
390 NULL, 0);
391 pipe_buffer_unmap(pipe, ubo_transfer[i]);
392 }
393 }
394
395 /*
396 * unmap vertex/index buffers
397 */
398 if (ib) {
399 draw_set_indexes(draw, NULL, 0, 0);
400 if (ib_transfer)
401 pipe_buffer_unmap(pipe, ib_transfer);
402 }
403
404 out_unref_vertex:
405 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
406 if (vb_transfer[buf])
407 pipe_buffer_unmap(pipe, vb_transfer[buf]);
408 draw_set_mapped_vertex_buffer(draw, buf, NULL, 0);
409 }
410 draw_set_vertex_buffers(draw, 0, num_vbuffers, NULL);
411 }