2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Vertex transform feedback support.
34 #include "bufferobj.h"
36 #include "transformfeedback.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/shader_api.h"
43 * Check if the given primitive mode (as in glBegin(mode)) is compatible
44 * with the current transform feedback mode (if it's enabled).
45 * This is to be called from glBegin(), glDrawArrays(), glDrawElements(), etc.
47 * \return GL_TRUE if the mode is OK, GL_FALSE otherwise.
50 _mesa_validate_primitive_mode(GLcontext
*ctx
, GLenum mode
)
52 if (ctx
->TransformFeedback
.Active
) {
55 return ctx
->TransformFeedback
.Mode
== GL_POINTS
;
59 return ctx
->TransformFeedback
.Mode
== GL_LINES
;
61 return ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
69 * Check that all the buffer objects currently bound for transform
70 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
71 * any buffers are missing.
72 * \return GL_TRUE for success, GL_FALSE if error
75 _mesa_validate_transform_feedback_buffers(GLcontext
*ctx
)
84 * Per-context init for transform feedback.
87 _mesa_init_transform_feedback(GLcontext
*ctx
)
89 _mesa_reference_buffer_object(ctx
,
90 &ctx
->TransformFeedback
.CurrentBuffer
,
91 ctx
->Shared
->NullBufferObj
);
96 * Per-context free/clean-up for transform feedback.
99 _mesa_free_transform_feedback(GLcontext
*ctx
)
101 _mesa_reference_buffer_object(ctx
,
102 &ctx
->TransformFeedback
.CurrentBuffer
,
108 _mesa_BeginTransformFeedback(GLenum mode
)
110 GET_CURRENT_CONTEXT(ctx
);
119 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
123 if (ctx
->TransformFeedback
.Active
) {
124 _mesa_error(ctx
, GL_INVALID_OPERATION
,
125 "glBeginTransformFeedback(already active)");
129 ctx
->TransformFeedback
.Active
= GL_TRUE
;
130 ctx
->TransformFeedback
.Mode
= mode
;
135 _mesa_EndTransformFeedback(void)
137 GET_CURRENT_CONTEXT(ctx
);
139 if (!ctx
->TransformFeedback
.Active
) {
140 _mesa_error(ctx
, GL_INVALID_OPERATION
,
141 "glEndTransformFeedback(not active)");
145 ctx
->TransformFeedback
.Active
= GL_FALSE
;
150 * Helper used by BindBufferRange() and BindBufferBase().
153 bind_buffer_range(GLcontext
*ctx
, GLuint index
,
154 struct gl_buffer_object
*bufObj
,
155 GLintptr offset
, GLsizeiptr size
)
157 /* The general binding point */
158 _mesa_reference_buffer_object(ctx
,
159 &ctx
->TransformFeedback
.CurrentBuffer
,
162 /* The per-attribute binding point */
163 _mesa_reference_buffer_object(ctx
,
164 &ctx
->TransformFeedback
.Buffers
[index
],
167 ctx
->TransformFeedback
.BufferNames
[index
] = bufObj
->Name
;
169 ctx
->TransformFeedback
.Offset
[index
] = offset
;
170 ctx
->TransformFeedback
.Size
[index
] = size
;
175 * Specify a buffer object to receive vertex shader results. Plus,
176 * specify the starting offset to place the results, and max size.
179 _mesa_BindBufferRange(GLenum target
, GLuint index
,
180 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
182 struct gl_buffer_object
*bufObj
;
183 GET_CURRENT_CONTEXT(ctx
);
185 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
186 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
190 if (ctx
->TransformFeedback
.Active
) {
191 _mesa_error(ctx
, GL_INVALID_OPERATION
,
192 "glBindBufferRange(transform feedback active)");
196 if (index
>= ctx
->Const
.MaxTransformFeedbackSeparateAttribs
) {
197 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
201 if ((size
<= 0) || (size
& 0x3)) {
202 /* must be positive and multiple of four */
203 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size%d)", size
);
208 /* must be multiple of four */
209 _mesa_error(ctx
, GL_INVALID_VALUE
,
210 "glBindBufferRange(offset=%d)", offset
);
214 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
216 _mesa_error(ctx
, GL_INVALID_OPERATION
,
217 "glBindBufferRange(invalid buffer=%u)", buffer
);
221 if (offset
+ size
>= bufObj
->Size
) {
222 _mesa_error(ctx
, GL_INVALID_VALUE
,
223 "glBindBufferRange(offset + size > buffer size)", size
);
227 bind_buffer_range(ctx
, index
, bufObj
, offset
, size
);
232 * Specify a buffer object to receive vertex shader results.
233 * As above, but start at offset = 0.
236 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
238 struct gl_buffer_object
*bufObj
;
239 GET_CURRENT_CONTEXT(ctx
);
242 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
243 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
247 if (ctx
->TransformFeedback
.Active
) {
248 _mesa_error(ctx
, GL_INVALID_OPERATION
,
249 "glBindBufferRange(transform feedback active)");
253 if (index
>= ctx
->Const
.MaxTransformFeedbackSeparateAttribs
) {
254 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
258 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
261 "glBindBufferBase(invalid buffer=%u)", buffer
);
265 /* default size is the buffer size rounded down to nearest
268 size
= bufObj
->Size
& ~0x3;
270 bind_buffer_range(ctx
, index
, bufObj
, 0, size
);
275 * Specify a buffer object to receive vertex shader results, plus the
276 * offset in the buffer to start placing results.
277 * This function is part of GL_EXT_transform_feedback, but not GL3.
280 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
283 struct gl_buffer_object
*bufObj
;
284 GET_CURRENT_CONTEXT(ctx
);
287 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
288 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
292 if (ctx
->TransformFeedback
.Active
) {
293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
294 "glBindBufferRange(transform feedback active)");
298 if (index
>= ctx
->Const
.MaxTransformFeedbackSeparateAttribs
) {
299 _mesa_error(ctx
, GL_INVALID_VALUE
,
300 "glBindBufferOffsetEXT(index=%d)", index
);
304 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
306 _mesa_error(ctx
, GL_INVALID_OPERATION
,
307 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
311 /* default size is the buffer size rounded down to nearest
314 size
= (bufObj
->Size
- offset
) & ~0x3;
316 bind_buffer_range(ctx
, index
, bufObj
, offset
, size
);
321 * This function specifies the vertex shader outputs to be written
322 * to the feedback buffer(s), and in what order.
325 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
326 const GLchar
**varyings
, GLenum bufferMode
)
328 struct gl_shader_program
*shProg
;
330 GET_CURRENT_CONTEXT(ctx
);
332 switch (bufferMode
) {
333 case GL_INTERLEAVED_ATTRIBS
:
335 case GL_SEPARATE_ATTRIBS
:
338 _mesa_error(ctx
, GL_INVALID_ENUM
,
339 "glTransformFeedbackVaryings(bufferMode)");
343 if (count
< 0 || count
> ctx
->Const
.MaxTransformFeedbackSeparateAttribs
) {
344 _mesa_error(ctx
, GL_INVALID_VALUE
,
345 "glTransformFeedbackVaryings(count=%d)", count
);
349 shProg
= _mesa_lookup_shader_program(ctx
, program
);
351 _mesa_error(ctx
, GL_INVALID_VALUE
,
352 "glTransformFeedbackVaryings(program=%u)", program
);
356 /* free existing varyings, if any */
357 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
358 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
360 free(shProg
->TransformFeedback
.VaryingNames
);
362 /* allocate new memory for varying names */
363 shProg
->TransformFeedback
.VaryingNames
=
364 (GLchar
**) malloc(count
* sizeof(GLchar
*));
366 if (!shProg
->TransformFeedback
.VaryingNames
) {
367 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
371 /* Save the new names and the count */
372 for (i
= 0; i
< (GLuint
) count
; i
++) {
373 shProg
->TransformFeedback
.VaryingNames
[i
] = _mesa_strdup(varyings
[i
]);
375 shProg
->TransformFeedback
.NumVarying
= count
;
377 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
379 /* The varyings won't be used until shader link time */
384 * Get info about the vertex shader's outputs which are to be written
385 * to the feedback buffer(s).
388 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
389 GLsizei bufSize
, GLsizei
*length
,
390 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
392 const struct gl_shader_program
*shProg
;
393 const GLchar
*varyingName
;
395 GET_CURRENT_CONTEXT(ctx
);
397 shProg
= _mesa_lookup_shader_program(ctx
, program
);
399 _mesa_error(ctx
, GL_INVALID_VALUE
,
400 "glGetTransformFeedbackVaryings(program=%u)", program
);
404 if (index
>= shProg
->TransformFeedback
.NumVarying
) {
405 _mesa_error(ctx
, GL_INVALID_VALUE
,
406 "glGetTransformFeedbackVaryings(index=%u)", index
);
410 varyingName
= shProg
->TransformFeedback
.VaryingNames
[index
];
412 v
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, varyingName
);
414 struct gl_program_parameter
*param
= &shProg
->Varying
->Parameters
[v
];
416 /* return the varying's name and length */
417 _mesa_copy_string(name
, bufSize
, length
, varyingName
);
419 /* return the datatype and value's size (in datatype units) */
421 *type
= param
->DataType
;