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/image.h"
30 #include "main/macros.h"
31 
32 #include "vbo/vbo.h"
33 
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_cb_bitmap.h"
37 #include "st_cb_bufferobjects.h"
38 #include "st_draw.h"
39 #include "st_program.h"
40 
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_inlines.h"
44 #include "util/u_draw.h"
45 
46 #include "draw/draw_private.h"
47 #include "draw/draw_context.h"
48 
49 
50 /**
51  * Set the (private) draw module's post-transformed vertex format when in
52  * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
53  */
54 static void
set_feedback_vertex_format(struct gl_context * ctx)55 set_feedback_vertex_format(struct gl_context *ctx)
56 {
57 #if 0
58    struct st_context *st = st_context(ctx);
59    struct vertex_info vinfo;
60    GLuint i;
61 
62    memset(&vinfo, 0, sizeof(vinfo));
63 
64    if (ctx->RenderMode == GL_SELECT) {
65       assert(ctx->RenderMode == GL_SELECT);
66       vinfo.num_attribs = 1;
67       vinfo.format[0] = FORMAT_4F;
68       vinfo.interp_mode[0] = INTERP_LINEAR;
69    }
70    else {
71       /* GL_FEEDBACK, or glRasterPos */
72       /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
73       vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
74       for (i = 0; i < vinfo.num_attribs; i++) {
75          vinfo.format[i] = FORMAT_4F;
76          vinfo.interp_mode[i] = INTERP_LINEAR;
77       }
78    }
79 
80    draw_set_vertex_info(st->draw, &vinfo);
81 #endif
82 }
83 
84 
85 /**
86  * Helper for drawing current vertex arrays.
87  */
88 static void
draw_arrays(struct draw_context * draw,unsigned mode,unsigned start,unsigned count)89 draw_arrays(struct draw_context *draw, unsigned mode,
90             unsigned start, unsigned count)
91 {
92    struct pipe_draw_info info;
93 
94    util_draw_init_info(&info);
95 
96    info.mode = mode;
97    info.start = start;
98    info.count = count;
99    info.min_index = start;
100    info.max_index = start + count - 1;
101 
102    draw_vbo(draw, &info);
103 }
104 
105 
106 /**
107  * Called by VBO to draw arrays when in selection or feedback mode and
108  * to implement glRasterPos.
109  * This is very much like the normal draw_vbo() function above.
110  * Look at code refactoring some day.
111  */
112 void
st_feedback_draw_vbo(struct gl_context * ctx,const struct _mesa_prim * prims,GLuint nr_prims,const struct _mesa_index_buffer * ib,GLboolean index_bounds_valid,GLuint min_index,GLuint max_index,struct gl_transform_feedback_object * tfb_vertcount,unsigned stream,struct gl_buffer_object * indirect)113 st_feedback_draw_vbo(struct gl_context *ctx,
114                      const struct _mesa_prim *prims,
115                      GLuint nr_prims,
116                      const struct _mesa_index_buffer *ib,
117 		     GLboolean index_bounds_valid,
118                      GLuint min_index,
119                      GLuint max_index,
120                      struct gl_transform_feedback_object *tfb_vertcount,
121                      unsigned stream,
122                      struct gl_buffer_object *indirect)
123 {
124    struct st_context *st = st_context(ctx);
125    struct pipe_context *pipe = st->pipe;
126    struct draw_context *draw = st_get_draw_context(st);
127    const struct st_vertex_program *vp;
128    const struct pipe_shader_state *vs;
129    struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
130    struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
131    struct pipe_index_buffer ibuffer;
132    struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
133    struct pipe_transfer *ib_transfer = NULL;
134    const struct gl_vertex_array **arrays = ctx->Array._DrawArrays;
135    GLuint attr, i;
136    const GLubyte *low_addr = NULL;
137    const void *mapped_indices = NULL;
138 
139    if (!draw)
140       return;
141 
142    st_flush_bitmap_cache(st);
143    st_invalidate_readpix_cache(st);
144 
145    st_validate_state(st, ST_PIPELINE_RENDER);
146 
147    if (!index_bounds_valid)
148       vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
149 
150    /* must get these after state validation! */
151    vp = st->vp;
152    vs = &st->vp_variant->tgsi;
153 
154    if (!st->vp_variant->draw_shader) {
155       st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
156    }
157 
158    /*
159     * Set up the draw module's state.
160     *
161     * We'd like to do this less frequently, but the normal state-update
162     * code sends state updates to the pipe, not to our private draw module.
163     */
164    assert(draw);
165    draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
166    draw_set_clip_state(draw, &st->state.clip);
167    draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
168    draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
169    set_feedback_vertex_format(ctx);
170 
171    /* Find the lowest address of the arrays we're drawing */
172    if (vp->num_inputs) {
173       low_addr = arrays[vp->index_to_input[0]]->Ptr;
174 
175       for (attr = 1; attr < vp->num_inputs; attr++) {
176          const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
177          low_addr = MIN2(low_addr, start);
178       }
179    }
180 
181    /* loop over TGSI shader inputs to determine vertex buffer
182     * and attribute info
183     */
184    for (attr = 0; attr < vp->num_inputs; attr++) {
185       const GLuint mesaAttr = vp->index_to_input[attr];
186       struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
187       void *map;
188 
189       if (bufobj && bufobj->Name) {
190          /* Attribute data is in a VBO.
191           * Recall that for VBOs, the gl_vertex_array->Ptr field is
192           * really an offset from the start of the VBO, not a pointer.
193           */
194          struct st_buffer_object *stobj = st_buffer_object(bufobj);
195          assert(stobj->buffer);
196 
197          vbuffers[attr].buffer = NULL;
198          vbuffers[attr].user_buffer = NULL;
199          pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
200          vbuffers[attr].buffer_offset = pointer_to_offset(low_addr);
201          velements[attr].src_offset = arrays[mesaAttr]->Ptr - low_addr;
202 
203          /* map the attrib buffer */
204          map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
205                                PIPE_TRANSFER_READ,
206                                &vb_transfer[attr]);
207          draw_set_mapped_vertex_buffer(draw, attr, map,
208                                        vbuffers[attr].buffer->width0);
209       }
210       else {
211          vbuffers[attr].buffer = NULL;
212          vbuffers[attr].user_buffer = arrays[mesaAttr]->Ptr;
213          vbuffers[attr].buffer_offset = 0;
214          velements[attr].src_offset = 0;
215 
216          draw_set_mapped_vertex_buffer(draw, attr, vbuffers[attr].user_buffer,
217                                        ~0);
218       }
219 
220       /* common-case setup */
221       vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
222       velements[attr].instance_divisor = 0;
223       velements[attr].vertex_buffer_index = attr;
224       velements[attr].src_format =
225          st_pipe_vertex_format(arrays[mesaAttr]->Type,
226                                arrays[mesaAttr]->Size,
227                                arrays[mesaAttr]->Format,
228                                arrays[mesaAttr]->Normalized,
229                                arrays[mesaAttr]->Integer);
230       assert(velements[attr].src_format);
231 
232       /* tell draw about this attribute */
233 #if 0
234       draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
235 #endif
236    }
237 
238    draw_set_vertex_buffers(draw, 0, vp->num_inputs, vbuffers);
239    draw_set_vertex_elements(draw, vp->num_inputs, velements);
240 
241    memset(&ibuffer, 0, sizeof(ibuffer));
242    if (ib) {
243       struct gl_buffer_object *bufobj = ib->obj;
244 
245       ibuffer.index_size = vbo_sizeof_ib_type(ib->type);
246       if (ibuffer.index_size == 0)
247          goto out_unref_vertex;
248 
249       if (bufobj && bufobj->Name) {
250          struct st_buffer_object *stobj = st_buffer_object(bufobj);
251 
252          pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
253          ibuffer.offset = pointer_to_offset(ib->ptr);
254 
255          mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
256                                           PIPE_TRANSFER_READ, &ib_transfer);
257       }
258       else {
259          /* skip setting ibuffer.buffer as the draw module does not use it */
260          mapped_indices = ib->ptr;
261       }
262 
263       draw_set_indexes(draw,
264                        (ubyte *) mapped_indices + ibuffer.offset,
265                        ibuffer.index_size, ~0);
266    }
267 
268    /* set the constant buffer */
269    draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
270                                    st->state.constants[PIPE_SHADER_VERTEX].ptr,
271                                    st->state.constants[PIPE_SHADER_VERTEX].size);
272 
273 
274    /* draw here */
275    for (i = 0; i < nr_prims; i++) {
276       draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
277    }
278 
279 
280    /*
281     * unmap vertex/index buffers
282     */
283    if (ib) {
284       draw_set_indexes(draw, NULL, 0, 0);
285       if (ib_transfer)
286          pipe_buffer_unmap(pipe, ib_transfer);
287       pipe_resource_reference(&ibuffer.buffer, NULL);
288    }
289 
290  out_unref_vertex:
291    for (attr = 0; attr < vp->num_inputs; attr++) {
292       if (vb_transfer[attr])
293          pipe_buffer_unmap(pipe, vb_transfer[attr]);
294       draw_set_mapped_vertex_buffer(draw, attr, NULL, 0);
295       pipe_resource_reference(&vbuffers[attr].buffer, NULL);
296    }
297    draw_set_vertex_buffers(draw, 0, vp->num_inputs, NULL);
298 }
299