Subversion Repositories Kolibri OS

Rev

Rev 4358 | Go to most recent revision | Details | Compare with Previous | 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
/*
29
 * This file implements the st_draw_vbo() function which is called from
30
 * Mesa's VBO module.  All point/line/triangle rendering is done through
31
 * this function whether the user called glBegin/End, glDrawArrays,
32
 * glDrawElements, glEvalMesh, or glCalList, etc.
33
 *
34
 * Authors:
35
 *   Keith Whitwell 
36
 */
37
 
38
 
39
#include "main/imports.h"
40
#include "main/image.h"
41
#include "main/bufferobj.h"
42
#include "main/macros.h"
43
 
44
#include "vbo/vbo.h"
45
 
46
#include "st_context.h"
47
#include "st_atom.h"
48
#include "st_cb_bufferobjects.h"
49
#include "st_cb_xformfb.h"
50
#include "st_debug.h"
51
#include "st_draw.h"
52
#include "st_program.h"
53
 
54
#include "pipe/p_context.h"
55
#include "pipe/p_defines.h"
56
#include "util/u_inlines.h"
57
#include "util/u_format.h"
58
#include "util/u_prim.h"
59
#include "util/u_draw_quad.h"
60
#include "util/u_upload_mgr.h"
61
#include "draw/draw_context.h"
62
#include "cso_cache/cso_context.h"
63
 
64
#include "../glsl/ir_uniform.h"
65
 
66
 
67
/**
68
 * This is very similar to vbo_all_varyings_in_vbos() but we are
69
 * only interested in per-vertex data.  See bug 38626.
70
 */
71
static GLboolean
72
all_varyings_in_vbos(const struct gl_client_array *arrays[])
73
{
74
   GLuint i;
75
 
76
   for (i = 0; i < VERT_ATTRIB_MAX; i++)
77
      if (arrays[i]->StrideB &&
78
          !arrays[i]->InstanceDivisor &&
79
          !_mesa_is_bufferobj(arrays[i]->BufferObj))
80
	 return GL_FALSE;
81
 
82
   return GL_TRUE;
83
}
84
 
85
 
86
/**
87
 * Basically, translate Mesa's index buffer information into
88
 * a pipe_index_buffer object.
89
 * \return TRUE or FALSE for success/failure
90
 */
91
static boolean
92
setup_index_buffer(struct st_context *st,
93
                   const struct _mesa_index_buffer *ib,
94
                   struct pipe_index_buffer *ibuffer)
95
{
96
   struct gl_buffer_object *bufobj = ib->obj;
97
 
98
   ibuffer->index_size = vbo_sizeof_ib_type(ib->type);
99
 
100
   /* get/create the index buffer object */
101
   if (_mesa_is_bufferobj(bufobj)) {
102
      /* indices are in a real VBO */
103
      ibuffer->buffer = st_buffer_object(bufobj)->buffer;
104
      ibuffer->offset = pointer_to_offset(ib->ptr);
105
   }
106
   else if (st->indexbuf_uploader) {
107
      if (u_upload_data(st->indexbuf_uploader, 0,
108
                        ib->count * ibuffer->index_size, ib->ptr,
109
                        &ibuffer->offset, &ibuffer->buffer) != PIPE_OK) {
110
         /* out of memory */
111
         return FALSE;
112
      }
113
      u_upload_unmap(st->indexbuf_uploader);
114
   }
115
   else {
116
      /* indices are in user space memory */
117
      ibuffer->user_buffer = ib->ptr;
118
   }
119
 
120
   cso_set_index_buffer(st->cso_context, ibuffer);
121
   return TRUE;
122
}
123
 
124
 
125
/**
126
 * Prior to drawing, check that any uniforms referenced by the
127
 * current shader have been set.  If a uniform has not been set,
128
 * issue a warning.
129
 */
130
static void
131
check_uniforms(struct gl_context *ctx)
132
{
133
   struct gl_shader_program *shProg[3] = {
134
      ctx->Shader.CurrentVertexProgram,
135
      ctx->Shader.CurrentGeometryProgram,
136
      ctx->Shader.CurrentFragmentProgram,
137
   };
138
   unsigned j;
139
 
140
   for (j = 0; j < 3; j++) {
141
      unsigned i;
142
 
143
      if (shProg[j] == NULL || !shProg[j]->LinkStatus)
144
	 continue;
145
 
146
      for (i = 0; i < shProg[j]->NumUserUniformStorage; i++) {
147
         const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i];
148
         if (!u->initialized) {
149
            _mesa_warning(ctx,
150
                          "Using shader with uninitialized uniform: %s",
151
                          u->name);
152
         }
153
      }
154
   }
155
}
156
 
157
 
158
/**
159
 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
160
 * the corresponding Gallium type.
161
 */
162
static unsigned
163
translate_prim(const struct gl_context *ctx, unsigned prim)
164
{
165
   /* GL prims should match Gallium prims, spot-check a few */
166
   STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
167
   STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
168
   STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
169
 
170
   /* Avoid quadstrips if it's easy to do so:
171
    * Note: it's important to do the correct trimming if we change the
172
    * prim type!  We do that wherever this function is called.
173
    */
174
   if (prim == GL_QUAD_STRIP &&
175
       ctx->Light.ShadeModel != GL_FLAT &&
176
       ctx->Polygon.FrontMode == GL_FILL &&
177
       ctx->Polygon.BackMode == GL_FILL)
178
      prim = GL_TRIANGLE_STRIP;
179
 
180
   return prim;
181
}
182
 
183
 
184
/**
185
 * This function gets plugged into the VBO module and is called when
186
 * we have something to render.
187
 * Basically, translate the information into the format expected by gallium.
188
 */
189
void
190
st_draw_vbo(struct gl_context *ctx,
191
            const struct _mesa_prim *prims,
192
            GLuint nr_prims,
193
            const struct _mesa_index_buffer *ib,
194
	    GLboolean index_bounds_valid,
195
            GLuint min_index,
196
            GLuint max_index,
197
            struct gl_transform_feedback_object *tfb_vertcount)
198
{
199
   struct st_context *st = st_context(ctx);
200
   struct pipe_index_buffer ibuffer = {0};
201
   struct pipe_draw_info info;
202
   const struct gl_client_array **arrays = ctx->Array._DrawArrays;
203
   unsigned i;
204
 
205
   /* Mesa core state should have been validated already */
206
   assert(ctx->NewState == 0x0);
207
 
208
   /* Validate state. */
209
   if (st->dirty.st || ctx->NewDriverState) {
210
      st_validate_state(st);
211
 
212
#if 0
213
      if (MESA_VERBOSE & VERBOSE_GLSL) {
214
         check_uniforms(ctx);
215
      }
216
#else
217
      (void) check_uniforms;
218
#endif
219
   }
220
 
4401 Serge 221
   if (st->vertex_array_out_of_memory) {
222
      return;
223
   }
224
 
4358 Serge 225
   util_draw_init_info(&info);
226
   if (ib) {
227
      /* Get index bounds for user buffers. */
228
      if (!index_bounds_valid)
229
         if (!all_varyings_in_vbos(arrays))
230
            vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
231
                                   nr_prims);
232
 
233
      if (!setup_index_buffer(st, ib, &ibuffer)) {
234
         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBegin/DrawElements/DrawArray");
235
         return;
236
      }
237
 
238
      info.indexed = TRUE;
239
      if (min_index != ~0 && max_index != ~0) {
240
         info.min_index = min_index;
241
         info.max_index = max_index;
242
      }
243
 
244
      /* The VBO module handles restart for the non-indexed GLDrawArrays
245
       * so we only set these fields for indexed drawing:
246
       */
247
      info.primitive_restart = ctx->Array._PrimitiveRestart;
248
      info.restart_index = ctx->Array.RestartIndex;
249
   }
250
   else {
251
      /* Transform feedback drawing is always non-indexed. */
252
      /* Set info.count_from_stream_output. */
253
      if (tfb_vertcount) {
254
         st_transform_feedback_draw_init(tfb_vertcount, &info);
255
      }
256
   }
257
 
258
   /* do actual drawing */
259
   for (i = 0; i < nr_prims; i++) {
260
      info.mode = translate_prim( ctx, prims[i].mode );
261
      info.start = prims[i].start;
262
      info.count = prims[i].count;
263
      info.start_instance = prims[i].base_instance;
264
      info.instance_count = prims[i].num_instances;
265
      info.index_bias = prims[i].basevertex;
266
      if (!ib) {
267
         info.min_index = info.start;
268
         info.max_index = info.start + info.count - 1;
269
      }
270
 
271
      if (ST_DEBUG & DEBUG_DRAW) {
272
         debug_printf("st/draw: mode %s  start %u  count %u  indexed %d\n",
273
                      u_prim_name(info.mode),
274
                      info.start,
275
                      info.count,
276
                      info.indexed);
277
      }
278
 
279
      if (info.count_from_stream_output) {
280
         cso_draw_vbo(st->cso_context, &info);
281
      }
282
      else if (info.primitive_restart) {
283
         /* don't trim, restarts might be inside index list */
284
         cso_draw_vbo(st->cso_context, &info);
285
      }
286
      else if (u_trim_pipe_prim(prims[i].mode, &info.count))
287
         cso_draw_vbo(st->cso_context, &info);
288
   }
289
 
290
   if (ib && st->indexbuf_uploader && !_mesa_is_bufferobj(ib->obj)) {
291
      pipe_resource_reference(&ibuffer.buffer, NULL);
292
   }
293
}
294
 
295
 
296
void
297
st_init_draw(struct st_context *st)
298
{
299
   struct gl_context *ctx = st->ctx;
300
 
301
   vbo_set_draw_func(ctx, st_draw_vbo);
302
 
303
   st->draw = draw_create(st->pipe); /* for selection/feedback */
304
 
305
   /* Disable draw options that might convert points/lines to tris, etc.
306
    * as that would foul-up feedback/selection mode.
307
    */
308
   draw_wide_line_threshold(st->draw, 1000.0f);
309
   draw_wide_point_threshold(st->draw, 1000.0f);
310
   draw_enable_line_stipple(st->draw, FALSE);
311
   draw_enable_point_sprites(st->draw, FALSE);
312
}
313
 
314
 
315
void
316
st_destroy_draw(struct st_context *st)
317
{
318
   draw_destroy(st->draw);
319
}