summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/transformfeedback.c
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2010-05-10 21:11:21 -0600
committerBrian Paul <[email protected]>2010-05-10 21:11:25 -0600
commitfef6e36e0736a68e24d7844bae65a01de8359214 (patch)
treee5d9abc8125f9166c55efe22dc11309099e1394c /src/mesa/main/transformfeedback.c
parent12bc39f30c2cc27fe9bc4d33c7c79919d895537d (diff)
mesa: more transform feedback infrastructure
Includes GL_ARB_transform_feedback2 which encapsulates transform feedback state in objects.
Diffstat (limited to 'src/mesa/main/transformfeedback.c')
-rw-r--r--src/mesa/main/transformfeedback.c474
1 files changed, 460 insertions, 14 deletions
diff --git a/src/mesa/main/transformfeedback.c b/src/mesa/main/transformfeedback.c
index 74519ba38a6..528e7d954b9 100644
--- a/src/mesa/main/transformfeedback.c
+++ b/src/mesa/main/transformfeedback.c
@@ -33,6 +33,7 @@
#include "buffers.h"
#include "bufferobj.h"
#include "context.h"
+#include "hash.h"
#include "transformfeedback.h"
#include "shader/prog_parameter.h"
@@ -40,6 +41,47 @@
/**
+ * Do reference counting of transform feedback buffers.
+ */
+static void
+reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
+ struct gl_transform_feedback_object *obj)
+{
+ if (*ptr == obj)
+ return;
+
+ if (*ptr) {
+ /* Unreference the old object */
+ struct gl_transform_feedback_object *oldObj = *ptr;
+
+ ASSERT(oldObj->RefCount > 0);
+ oldObj->RefCount--;
+
+ if (oldObj->RefCount == 0) {
+ GET_CURRENT_CONTEXT(ctx);
+ if (ctx)
+ ctx->Driver.DeleteTransformFeedback(ctx, oldObj);
+ }
+
+ *ptr = NULL;
+ }
+ ASSERT(!*ptr);
+
+ if (obj) {
+ /* reference new object */
+ if (obj->RefCount == 0) {
+ _mesa_problem(NULL, "referencing deleted transform feedback object");
+ *ptr = NULL;
+ }
+ else {
+ obj->RefCount++;
+ *ptr = obj;
+ }
+ }
+}
+
+
+/**
* Check if the given primitive mode (as in glBegin(mode)) is compatible
* with the current transform feedback mode (if it's enabled).
* This is to be called from glBegin(), glDrawArrays(), glDrawElements(), etc.
@@ -49,7 +91,7 @@
GLboolean
_mesa_validate_primitive_mode(GLcontext *ctx, GLenum mode)
{
- if (ctx->TransformFeedback.Active) {
+ if (ctx->TransformFeedback.CurrentObject->Active) {
switch (mode) {
case GL_POINTS:
return ctx->TransformFeedback.Mode == GL_POINTS;
@@ -74,7 +116,7 @@ _mesa_validate_primitive_mode(GLcontext *ctx, GLenum mode)
GLboolean
_mesa_validate_transform_feedback_buffers(GLcontext *ctx)
{
-
+ /* XXX to do */
return GL_TRUE;
}
@@ -86,29 +128,173 @@ _mesa_validate_transform_feedback_buffers(GLcontext *ctx)
void
_mesa_init_transform_feedback(GLcontext *ctx)
{
+ if (!ctx->Driver.NewTransformFeedback) {
+ /* this feature/extension may not be supported by the driver */
+ return;
+ }
+
+ ctx->TransformFeedback.DefaultObject =
+ ctx->Driver.NewTransformFeedback(ctx, 0);
+
+ assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
+
+ reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
+ ctx->TransformFeedback.DefaultObject);
+
+ assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
+
+ ctx->TransformFeedback.Objects = _mesa_NewHashTable();
+
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
ctx->Shared->NullBufferObj);
}
+
+/**
+ * Callback for _mesa_HashDeleteAll().
+ */
+static void
+delete_cb(GLuint key, void *data, void *userData)
+{
+ GLcontext *ctx = (GLcontext *) userData;
+ struct gl_transform_feedback_object *obj =
+ (struct gl_transform_feedback_object *) data;
+
+ ctx->Driver.DeleteTransformFeedback(ctx, obj);
+}
+
+
/**
* Per-context free/clean-up for transform feedback.
*/
void
_mesa_free_transform_feedback(GLcontext *ctx)
{
+ if (!ctx->Driver.NewTransformFeedback) {
+ /* this feature/extension may not be supported by the driver */
+ return;
+ }
+
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
NULL);
+
+ /* Delete all feedback objects */
+ _mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx);
+
+ /* Delete the default feedback object */
+ assert(ctx->Driver.DeleteTransformFeedback);
+ ctx->Driver.DeleteTransformFeedback(ctx, ctx->TransformFeedback.DefaultObject);
+
+ ctx->TransformFeedback.CurrentObject = NULL;
}
+/** Default fallback for ctx->Driver.NewTransformFeedback() */
+static struct gl_transform_feedback_object *
+new_transform_feedback(GLcontext *ctx, GLuint name)
+{
+ struct gl_transform_feedback_object *obj;
+ obj = CALLOC_STRUCT(gl_transform_feedback_object);
+ if (obj) {
+ obj->Name = name;
+ obj->RefCount = 1;
+ }
+ return obj;
+}
+
+/** Default fallback for ctx->Driver.DeleteTransformFeedback() */
+static void
+delete_transform_feedback(GLcontext *ctx,
+ struct gl_transform_feedback_object *obj)
+{
+ GLuint i;
+
+ for (i = 0; i < Elements(obj->Buffers); i++) {
+ _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
+ }
+
+ free(obj);
+}
+
+/** Default fallback for ctx->Driver.BeginTransformFeedback() */
+static void
+begin_transform_feedback(GLcontext *ctx, GLenum mode,
+ struct gl_transform_feedback_object *obj)
+{
+ /* nop */
+}
+
+/** Default fallback for ctx->Driver.EndTransformFeedback() */
+static void
+end_transform_feedback(GLcontext *ctx,
+ struct gl_transform_feedback_object *obj)
+{
+ /* nop */
+}
+
+/** Default fallback for ctx->Driver.PauseTransformFeedback() */
+static void
+pause_transform_feedback(GLcontext *ctx,
+ struct gl_transform_feedback_object *obj)
+{
+ /* nop */
+}
+
+/** Default fallback for ctx->Driver.ResumeTransformFeedback() */
+static void
+resume_transform_feedback(GLcontext *ctx,
+ struct gl_transform_feedback_object *obj)
+{
+ /* nop */
+}
+
+/** Default fallback for ctx->Driver.DrawTransformFeedback() */
+static void
+draw_transform_feedback(GLcontext *ctx, GLenum mode,
+ struct gl_transform_feedback_object *obj)
+{
+ /* XXX to do */
+ /*
+ * Get number of vertices in obj's feedback buffer.
+ * Call ctx->Exec.DrawArrays(mode, 0, count);
+ */
+}
+
+
+/**
+ * Plug in default device driver functions for transform feedback.
+ * Most drivers will override some/all of these.
+ */
+void
+_mesa_init_transform_feedback_functions(struct dd_function_table *driver)
+{
+ driver->NewTransformFeedback = new_transform_feedback;
+ driver->DeleteTransformFeedback = delete_transform_feedback;
+ driver->BeginTransformFeedback = begin_transform_feedback;
+ driver->EndTransformFeedback = end_transform_feedback;
+ driver->PauseTransformFeedback = pause_transform_feedback;
+ driver->ResumeTransformFeedback = resume_transform_feedback;
+ driver->DrawTransformFeedback = draw_transform_feedback;
+}
+
+
+
+/**
+ ** Begin API functions
+ **/
+
+
void GLAPIENTRY
_mesa_BeginTransformFeedback(GLenum mode)
{
+ struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
+ obj = ctx->TransformFeedback.CurrentObject;
+
switch (mode) {
case GL_POINTS:
case GL_LINES:
@@ -120,29 +306,38 @@ _mesa_BeginTransformFeedback(GLenum mode)
return;
}
- if (ctx->TransformFeedback.Active) {
+ if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginTransformFeedback(already active)");
return;
}
- ctx->TransformFeedback.Active = GL_TRUE;
+ obj->Active = GL_TRUE;
ctx->TransformFeedback.Mode = mode;
+
+ assert(ctx->Driver.BeginTransformFeedback);
+ ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
}
void GLAPIENTRY
_mesa_EndTransformFeedback(void)
{
+ struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
- if (!ctx->TransformFeedback.Active) {
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (!obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glEndTransformFeedback(not active)");
return;
}
- ctx->TransformFeedback.Active = GL_FALSE;
+ ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
+
+ assert(ctx->Driver.EndTransformFeedback);
+ ctx->Driver.EndTransformFeedback(ctx, obj);
}
@@ -154,6 +349,9 @@ bind_buffer_range(GLcontext *ctx, GLuint index,
struct gl_buffer_object *bufObj,
GLintptr offset, GLsizeiptr size)
{
+ struct gl_transform_feedback_object *obj =
+ ctx->TransformFeedback.CurrentObject;
+
/* The general binding point */
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
@@ -161,13 +359,13 @@ bind_buffer_range(GLcontext *ctx, GLuint index,
/* The per-attribute binding point */
_mesa_reference_buffer_object(ctx,
- &ctx->TransformFeedback.Buffers[index],
+ &obj->Buffers[index],
bufObj);
- ctx->TransformFeedback.BufferNames[index] = bufObj->Name;
+ obj->BufferNames[index] = bufObj->Name;
- ctx->TransformFeedback.Offset[index] = offset;
- ctx->TransformFeedback.Size[index] = size;
+ obj->Offset[index] = offset;
+ obj->Size[index] = size;
}
@@ -179,6 +377,7 @@ void GLAPIENTRY
_mesa_BindBufferRange(GLenum target, GLuint index,
GLuint buffer, GLintptr offset, GLsizeiptr size)
{
+ struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
GET_CURRENT_CONTEXT(ctx);
@@ -187,7 +386,9 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
return;
}
- if (ctx->TransformFeedback.Active) {
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindBufferRange(transform feedback active)");
return;
@@ -235,16 +436,19 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
void GLAPIENTRY
_mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
{
+ struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
- GET_CURRENT_CONTEXT(ctx);
GLsizeiptr size;
+ GET_CURRENT_CONTEXT(ctx);
if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
_mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
return;
}
- if (ctx->TransformFeedback.Active) {
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindBufferRange(transform feedback active)");
return;
@@ -280,6 +484,7 @@ void GLAPIENTRY
_mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
GLintptr offset)
{
+ struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
GET_CURRENT_CONTEXT(ctx);
GLsizeiptr size;
@@ -289,7 +494,9 @@ _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
return;
}
- if (ctx->TransformFeedback.Active) {
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindBufferRange(transform feedback active)");
return;
@@ -433,3 +640,242 @@ _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
}
}
+
+
+static struct gl_transform_feedback_object *
+lookup_transform_feedback_object(GLcontext *ctx, GLuint name)
+{
+ if (name == 0) {
+ return ctx->TransformFeedback.DefaultObject;
+ }
+ else
+ return (struct gl_transform_feedback_object *)
+ _mesa_HashLookup(ctx->TransformFeedback.Objects, name);
+}
+
+
+/**
+ * Create new transform feedback objects. Transform feedback objects
+ * encapsulate the state related to transform feedback to allow quickly
+ * switching state (and drawing the results, below).
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
+{
+ GLuint first;
+ GET_CURRENT_CONTEXT(ctx);
+
+ ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+ if (n < 0) {
+ _mesa_error(ctx, GL_INVALID_VALUE, "glGenTransformFeedbacks(n < 0)");
+ return;
+ }
+
+ if (!names)
+ return;
+
+ /* we don't need contiguous IDs, but this might be faster */
+ first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
+ if (first) {
+ GLsizei i;
+ for (i = 0; i < n; i++) {
+ struct gl_transform_feedback_object *obj
+ = ctx->Driver.NewTransformFeedback(ctx, first + i);
+ if (!obj) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
+ return;
+ }
+ names[i] = first + i;
+ _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
+ }
+ }
+ else {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
+ }
+}
+
+
+/**
+ * Is the given ID a transform feedback object?
+ * Part of GL_ARB_transform_feedback2.
+ */
+GLboolean GLAPIENTRY
+_mesa_IsTransformFeedback(GLuint name)
+{
+ GET_CURRENT_CONTEXT(ctx);
+
+ ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
+
+ if (name && lookup_transform_feedback_object(ctx, name))
+ return GL_TRUE;
+ else
+ return GL_FALSE;
+}
+
+
+/**
+ * Bind the given transform feedback object.
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_BindTransformFeedback(GLenum target, uint name)
+{
+ struct gl_transform_feedback_object *obj;
+ GET_CURRENT_CONTEXT(ctx);
+
+ if (target != GL_TRANSFORM_FEEDBACK) {
+ _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
+ return;
+ }
+
+ if (ctx->TransformFeedback.CurrentObject->Active &&
+ !ctx->TransformFeedback.CurrentObject->Paused) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glBindTransformFeedback(transform is active, or not paused)");
+ return;
+ }
+
+ obj = lookup_transform_feedback_object(ctx, name);
+ if (!obj) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glBindTransformFeedback(name=%u)", name);
+ return;
+ }
+
+ reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
+ obj);
+}
+
+
+/**
+ * Delete the given transform feedback objects.
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
+{
+ GLint i;
+ GET_CURRENT_CONTEXT(ctx);
+
+ ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+ if (n < 0) {
+ _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
+ return;
+ }
+
+ if (!names)
+ return;
+
+ for (i = 0; i < n; i++) {
+ if (names[i] > 0) {
+ struct gl_transform_feedback_object *obj
+ = lookup_transform_feedback_object(ctx, names[i]);
+ if (obj) {
+ if (obj->Active) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glDeleteTransformFeedbacks(object %u is active)",
+ names[i]);
+ return;
+ }
+ _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
+ /* unref, but object may not be deleted until later */
+ reference_transform_feedback_object(&obj, NULL);
+ }
+ }
+ }
+}
+
+
+/**
+ * Pause transform feedback.
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_PauseTransformFeedback(void)
+{
+ struct gl_transform_feedback_object *obj;
+ GET_CURRENT_CONTEXT(ctx);
+
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (!obj->Active || obj->Paused) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glPauseTransformFeedback(feedback not active or already paused)");
+ return;
+ }
+
+ obj->Paused = GL_TRUE;
+
+ assert(ctx->Driver.PauseTransformFeedback);
+ ctx->Driver.PauseTransformFeedback(ctx, obj);
+}
+
+
+/**
+ * Resume transform feedback.
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_ResumeTransformFeedback(void)
+{
+ struct gl_transform_feedback_object *obj;
+ GET_CURRENT_CONTEXT(ctx);
+
+ obj = ctx->TransformFeedback.CurrentObject;
+
+ if (!obj->Active || !obj->Paused) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glPauseTransformFeedback(feedback not active or not paused)");
+ return;
+ }
+
+ obj->Paused = GL_FALSE;
+
+ assert(ctx->Driver.ResumeTransformFeedback);
+ ctx->Driver.ResumeTransformFeedback(ctx, obj);
+}
+
+
+/**
+ * Draw the vertex data in a transform feedback object.
+ * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
+ * \param name the transform feedback object
+ * The number of vertices comes from the transform feedback object.
+ * User still has to setup of the vertex attribute info with
+ * glVertexPointer, glColorPointer, etc.
+ * Part of GL_ARB_transform_feedback2.
+ */
+void GLAPIENTRY
+_mesa_DrawTransformFeedback(GLenum mode, GLuint name)
+{
+ GET_CURRENT_CONTEXT(ctx);
+ struct gl_transform_feedback_object *obj =
+ lookup_transform_feedback_object(ctx, name);
+
+ if (!obj) {
+ _mesa_error(ctx, GL_INVALID_VALUE,
+ "glDrawTransformFeedback(name = %u)", name);
+ return;
+ }
+
+ /* XXX check if EndTransformFeedback has never been called while
+ * the object was bound
+ */
+
+ assert(ctx->Driver.DrawTransformFeedback);
+ ctx->Driver.DrawTransformFeedback(ctx, mode, obj);
+}
+
+
+/*
+XXX misc to do:
+
+glGet*() for
+
+GL_TRANSFORM_FEEDBACK_BUFFER_PAUSED
+GL_TRANSFORM_FEEDBACK_BUFFER_ACTIVE
+GL_TRANSFORM_FEEDBACK_BINDING
+*/