• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
132    struct pipe_transfer *ib_transfer = NULL;
133    const struct gl_vertex_array **arrays = ctx->Array._DrawArrays;
134    GLuint attr, i;
135    const GLubyte *low_addr = NULL;
136    const void *mapped_indices = NULL;
137 
138    if (!draw)
139       return;
140 
141    st_flush_bitmap_cache(st);
142    st_invalidate_readpix_cache(st);
143 
144    st_validate_state(st, ST_PIPELINE_RENDER);
145 
146    if (!index_bounds_valid)
147       vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
148 
149    /* must get these after state validation! */
150    vp = st->vp;
151    vs = &st->vp_variant->tgsi;
152 
153    if (!st->vp_variant->draw_shader) {
154       st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
155    }
156 
157    /*
158     * Set up the draw module's state.
159     *
160     * We'd like to do this less frequently, but the normal state-update
161     * code sends state updates to the pipe, not to our private draw module.
162     */
163    assert(draw);
164    draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
165    draw_set_clip_state(draw, &st->state.clip);
166    draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
167    draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
168    set_feedback_vertex_format(ctx);
169 
170    /* Find the lowest address of the arrays we're drawing */
171    if (vp->num_inputs) {
172       low_addr = arrays[vp->index_to_input[0]]->Ptr;
173 
174       for (attr = 1; attr < vp->num_inputs; attr++) {
175          const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
176          low_addr = MIN2(low_addr, start);
177       }
178    }
179 
180    /* loop over TGSI shader inputs to determine vertex buffer
181     * and attribute info
182     */
183    for (attr = 0; attr < vp->num_inputs; attr++) {
184       const GLuint mesaAttr = vp->index_to_input[attr];
185       struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
186       void *map;
187 
188       if (bufobj && bufobj->Name) {
189          /* Attribute data is in a VBO.
190           * Recall that for VBOs, the gl_vertex_array->Ptr field is
191           * really an offset from the start of the VBO, not a pointer.
192           */
193          struct st_buffer_object *stobj = st_buffer_object(bufobj);
194          assert(stobj->buffer);
195 
196          vbuffers[attr].buffer.resource = NULL;
197          vbuffers[attr].is_user_buffer = false;
198          pipe_resource_reference(&vbuffers[attr].buffer.resource, stobj->buffer);
199          vbuffers[attr].buffer_offset = pointer_to_offset(low_addr);
200          velements[attr].src_offset = arrays[mesaAttr]->Ptr - low_addr;
201 
202          /* map the attrib buffer */
203          map = pipe_buffer_map(pipe, vbuffers[attr].buffer.resource,
204                                PIPE_TRANSFER_READ,
205                                &vb_transfer[attr]);
206          draw_set_mapped_vertex_buffer(draw, attr, map,
207                                        vbuffers[attr].buffer.resource->width0);
208       }
209       else {
210          vbuffers[attr].buffer.user = arrays[mesaAttr]->Ptr;
211          vbuffers[attr].is_user_buffer = true;
212          vbuffers[attr].buffer_offset = 0;
213          velements[attr].src_offset = 0;
214 
215          draw_set_mapped_vertex_buffer(draw, attr,
216                                        vbuffers[attr].buffer.user, ~0);
217       }
218 
219       /* common-case setup */
220       vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
221       velements[attr].instance_divisor = 0;
222       velements[attr].vertex_buffer_index = attr;
223       velements[attr].src_format =
224          st_pipe_vertex_format(arrays[mesaAttr]->Type,
225                                arrays[mesaAttr]->Size,
226                                arrays[mesaAttr]->Format,
227                                arrays[mesaAttr]->Normalized,
228                                arrays[mesaAttr]->Integer);
229       assert(velements[attr].src_format);
230 
231       /* tell draw about this attribute */
232 #if 0
233       draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
234 #endif
235    }
236 
237    draw_set_vertex_buffers(draw, 0, vp->num_inputs, vbuffers);
238    draw_set_vertex_elements(draw, vp->num_inputs, velements);
239 
240    unsigned start = 0;
241 
242    if (ib) {
243       struct gl_buffer_object *bufobj = ib->obj;
244       unsigned index_size = ib->index_size;
245 
246       if (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          start = pointer_to_offset(ib->ptr) / index_size;
253          mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
254                                           PIPE_TRANSFER_READ, &ib_transfer);
255       }
256       else {
257          mapped_indices = ib->ptr;
258       }
259 
260       draw_set_indexes(draw,
261                        (ubyte *) mapped_indices,
262                        index_size, ~0);
263    }
264 
265    /* set the constant buffer */
266    draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
267                                    st->state.constants[PIPE_SHADER_VERTEX].ptr,
268                                    st->state.constants[PIPE_SHADER_VERTEX].size);
269 
270 
271    /* draw here */
272    for (i = 0; i < nr_prims; i++) {
273       draw_arrays(draw, prims[i].mode, start + prims[i].start, prims[i].count);
274    }
275 
276 
277    /*
278     * unmap vertex/index buffers
279     */
280    if (ib) {
281       draw_set_indexes(draw, NULL, 0, 0);
282       if (ib_transfer)
283          pipe_buffer_unmap(pipe, ib_transfer);
284    }
285 
286  out_unref_vertex:
287    for (attr = 0; attr < vp->num_inputs; attr++) {
288       if (vb_transfer[attr])
289          pipe_buffer_unmap(pipe, vb_transfer[attr]);
290       draw_set_mapped_vertex_buffer(draw, attr, NULL, 0);
291       pipe_vertex_buffer_unreference(&vbuffers[attr]);
292    }
293    draw_set_vertex_buffers(draw, 0, vp->num_inputs, NULL);
294 }
295