Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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