1fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell/*
2fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Mesa 3-D graphics library
3fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Version:  6.3
4fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *
5fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
6fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *
7fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
8fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * copy of this software and associated documentation files (the "Software"),
9fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * to deal in the Software without restriction, including without limitation
10fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
12fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Software is furnished to do so, subject to the following conditions:
13fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *
14fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * The above copyright notice and this permission notice shall be included
15fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * in all copies or substantial portions of the Software.
16fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *
17fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *
24fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * Authors:
25fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell *    Keith Whitwell <keith@tungstengraphics.com>
26fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell */
27fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
28fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
29c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/api_arrayelt.h"
30c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/glheader.h"
31c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/mtypes.h"
32c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/vtxfmt.h"
33fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell#include "vbo_context.h"
34fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
353644dbbdbd1abe19a1dad37fa9d7f0577e2471b3Brian Paul
363644dbbdbd1abe19a1dad37fa9d7f0577e2471b3Brian Paul
37f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergvoid vbo_exec_init( struct gl_context *ctx )
38fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell{
39fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
40fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
41fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   exec->ctx = ctx;
42fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
43fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   /* Initialize the arrayelt helper
44fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell    */
45fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   if (!ctx->aelt_context &&
46fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell       !_ae_create_context( ctx ))
47fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell      return;
48fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
49fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   vbo_exec_vtx_init( exec );
50fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   vbo_exec_array_init( exec );
51fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
52e8abd098b36c30d76b4c396970fd793ae58e0702Keith Whitwell   /* Hook our functions into exec and compile dispatch tables.
53e8abd098b36c30d76b4c396970fd793ae58e0702Keith Whitwell    */
54e8abd098b36c30d76b4c396970fd793ae58e0702Keith Whitwell   _mesa_install_exec_vtxfmt( ctx, &exec->vtxfmt );
55e8abd098b36c30d76b4c396970fd793ae58e0702Keith Whitwell
56fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   ctx->Driver.NeedFlush = 0;
57fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
58c64a2b708944ec671b1104067245500fcfc6ed94Keith Whitwell   ctx->Driver.BeginVertices = vbo_exec_BeginVertices;
59fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   ctx->Driver.FlushVertices = vbo_exec_FlushVertices;
60fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
61e8abd098b36c30d76b4c396970fd793ae58e0702Keith Whitwell   vbo_exec_invalidate_state( ctx, ~0 );
62fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell}
63fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
64fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
65f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergvoid vbo_exec_destroy( struct gl_context *ctx )
66fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell{
67fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
68fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
69fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   if (ctx->aelt_context) {
70fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell      _ae_destroy_context( ctx );
71fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell      ctx->aelt_context = NULL;
72fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   }
73fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
74fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   vbo_exec_vtx_destroy( exec );
75fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   vbo_exec_array_destroy( exec );
76fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell}
77fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
783644dbbdbd1abe19a1dad37fa9d7f0577e2471b3Brian Paul
793644dbbdbd1abe19a1dad37fa9d7f0577e2471b3Brian Paul/**
803644dbbdbd1abe19a1dad37fa9d7f0577e2471b3Brian Paul * Really want to install these callbacks to a central facility to be
81fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * invoked according to the state flags.  That will have to wait for a
82fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell * mesa rework:
83fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell */
84f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergvoid vbo_exec_invalidate_state( struct gl_context *ctx, GLuint new_state )
85fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell{
86fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
87fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
88784dd51198433e5c299da4a7742c68d21d68d1c1Marek Olšák   if (new_state & (_NEW_PROGRAM|_NEW_ARRAY)) {
89784dd51198433e5c299da4a7742c68d21d68d1c1Marek Olšák      exec->array.recalculate_inputs = GL_TRUE;
90784dd51198433e5c299da4a7742c68d21d68d1c1Marek Olšák   }
91784dd51198433e5c299da4a7742c68d21d68d1c1Marek Olšák
92fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   if (new_state & (_NEW_PROGRAM|_NEW_EVAL))
93fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell      exec->eval.recalculate_maps = 1;
94fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell
95fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell   _ae_invalidate_state(ctx, new_state);
96fd12b37dbada6f945a94b93ecf332d0b6a8eef06Keith Whitwell}
97dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry
98dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry
99dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry/**
100dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry * Figure out the number of transform feedback primitives that will be output
101dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry * by the given _mesa_prim command, assuming that no geometry shading is done
102dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry * and primitive restart is not used.
103dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry *
104dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry * This is intended for use by driver back-ends in implementing the
105dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry * PRIMITIVES_GENERATED and TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN queries.
106dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry */
107dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berrysize_t
108dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berrycount_tessellated_primitives(const struct _mesa_prim *prim)
109dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry{
110dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   size_t num_primitives;
111dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   switch (prim->mode) {
112dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_POINTS:
113dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count;
114dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
115dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_LINE_STRIP:
116dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count >= 2 ? prim->count - 1 : 0;
117dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
118dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_LINE_LOOP:
119dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count >= 2 ? prim->count : 0;
120dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
121dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_LINES:
122dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count / 2;
123dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
124dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_TRIANGLE_STRIP:
125dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_TRIANGLE_FAN:
126dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_POLYGON:
127dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count >= 3 ? prim->count - 2 : 0;
128dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
129dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_TRIANGLES:
130dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count / 3;
131dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
132dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_QUAD_STRIP:
133dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = prim->count >= 4 ? ((prim->count / 2) - 1) * 2 : 0;
134dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
135dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   case GL_QUADS:
136dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = (prim->count / 4) * 2;
137dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
138dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   default:
139dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      assert(!"Unexpected primitive type in count_tessellated_primitives");
140dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      num_primitives = 0;
141dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry      break;
142dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   }
143dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry   return num_primitives * prim->num_instances;
144dc7b6d7d6defdfe4f79fb682d3378a3bed8b5db9Paul Berry}
145