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