]> git.cworth.org Git - apitrace/blobdiff - helpers/glsize.hpp
d3d9trace: Fix D3DPT_TRIANGLEFAN vertex count computation.
[apitrace] / helpers / glsize.hpp
index c4fe9bf9c866b5aa19c0b4283a9acf3998c39102..a920ac0dbaedddda2142b3c74cc66f02af6afb7f 100644 (file)
@@ -445,18 +445,142 @@ _glDrawElementsBaseVertex_count(GLsizei count, GLenum type, const GLvoid *indice
 #define _glDrawArraysInstancedEXT_count _glDrawArraysInstanced_count
 #define _glDrawElementsInstancedEXT_count _glDrawElementsInstanced_count
 
+typedef struct {
+    GLuint count;
+    GLuint primCount;
+    GLuint first;
+    GLuint baseInstance;
+} DrawArraysIndirectCommand;
+
+static inline GLuint
+_glMultiDrawArraysIndirect_count(const GLvoid *indirect, GLsizei drawcount, GLsizei stride) {
+    const DrawArraysIndirectCommand *cmd;
+    GLvoid *temp = 0;
+
+    if (drawcount <= 0) {
+        return 0;
+    }
+
+    if (stride == 0) {
+        stride = sizeof *cmd;
+    }
+
+    GLint draw_indirect_buffer = _glGetInteger(GL_DRAW_INDIRECT_BUFFER_BINDING);
+    if (draw_indirect_buffer) {
+        // Read commands from indirect buffer object
+        GLintptr offset = (GLintptr)indirect;
+        GLsizeiptr size = sizeof *cmd + (drawcount - 1) * stride;
+        GLvoid *temp = malloc(size);
+        if (!temp) {
+            return 0;
+        }
+        memset(temp, 0, size);
+        _glGetBufferSubData(GL_DRAW_INDIRECT_BUFFER, offset, size, temp);
+        indirect = temp;
+    } else {
+        if (!indirect) {
+            return 0;
+        }
+    }
+
+    GLuint count = 0;
+    for (GLsizei i = 0; i < drawcount; ++i) {
+        cmd = (const DrawArraysIndirectCommand *)((const GLbyte *)indirect + i * stride);
+
+        GLuint count_i = _glDrawArraysInstancedBaseInstance_count(
+            cmd->first,
+            cmd->count,
+            cmd->primCount,
+            cmd->baseInstance
+        );
+
+        count = std::max(count, count_i);
+    }
+
+    if (draw_indirect_buffer) {
+        free(temp);
+    }
+
+    return count;
+}
+
 static inline GLuint
 _glDrawArraysIndirect_count(const GLvoid *indirect) {
-    os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
-    return 0;
+    return _glMultiDrawArraysIndirect_count(indirect, 1, 0);
+}
+
+typedef struct {
+    GLuint count;
+    GLuint primCount;
+    GLuint firstIndex;
+    GLuint baseVertex;
+    GLuint baseInstance;
+} DrawElementsIndirectCommand;
+
+static inline GLuint
+_glMultiDrawElementsIndirect_count(GLenum type, const GLvoid *indirect, GLsizei drawcount, GLsizei stride) {
+    const DrawElementsIndirectCommand *cmd;
+    GLvoid *temp = 0;
+
+    if (drawcount <= 0) {
+        return 0;
+    }
+
+    if (stride == 0) {
+        stride = sizeof *cmd;
+    }
+
+    GLint draw_indirect_buffer = _glGetInteger(GL_DRAW_INDIRECT_BUFFER_BINDING);
+    if (draw_indirect_buffer) {
+        // Read commands from indirect buffer object
+        GLintptr offset = (GLintptr)indirect;
+        GLsizeiptr size = sizeof *cmd + (drawcount - 1) * stride;
+        GLvoid *temp = malloc(size);
+        if (!temp) {
+            return 0;
+        }
+        memset(temp, 0, size);
+        _glGetBufferSubData(GL_DRAW_INDIRECT_BUFFER, offset, size, temp);
+        indirect = temp;
+    } else {
+        if (!indirect) {
+            return 0;
+        }
+    }
+
+    cmd = (const DrawElementsIndirectCommand *)indirect;
+
+    GLuint count = 0;
+    for (GLsizei i = 0; i < drawcount; ++i) {
+        cmd = (const DrawElementsIndirectCommand *)((const GLbyte *)indirect + i * stride);
+
+        GLuint count_i = _glDrawElementsInstancedBaseVertexBaseInstance_count(
+            cmd->count,
+            type,
+            (GLvoid *)(uintptr_t)(cmd->firstIndex * _gl_type_size(type)),
+            cmd->primCount,
+            cmd->baseVertex,
+            cmd->baseInstance
+        );
+
+        count = std::max(count, count_i);
+    }
+
+    if (draw_indirect_buffer) {
+        free(temp);
+    }
+
+    return count;
 }
 
 static inline GLuint
 _glDrawElementsIndirect_count(GLenum type, const GLvoid *indirect) {
-    os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
-    return 0;
+    return _glMultiDrawElementsIndirect_count(type, indirect, 1, 0);
 }
 
+#define _glMultiDrawArraysIndirectAMD_count _glMultiDrawArraysIndirect_count
+#define _glMultiDrawElementsIndirectAMD_count _glMultiDrawElementsIndirect_count
+
 static inline GLuint
 _glMultiDrawArrays_count(const GLint *first, const GLsizei *count, GLsizei drawcount) {
     GLuint _count = 0;
@@ -582,37 +706,66 @@ _glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint v
 
 #define _glMap2f_size _glMap2d_size
 
+/**
+ * Number of channels in this format.
+ *
+ * That is, the number of elements per pixel when this format is passed with a
+ * to DrawPixels, ReadPixels, TexImage*, TexSubImage*, GetTexImage, etc.
+ */
 static inline unsigned
 _gl_format_channels(GLenum format) {
     switch (format) {
     case GL_COLOR_INDEX:
     case GL_RED:
+    case GL_RED_INTEGER:
     case GL_GREEN:
+    case GL_GREEN_INTEGER:
     case GL_BLUE:
+    case GL_BLUE_INTEGER:
     case GL_ALPHA:
+    case GL_ALPHA_INTEGER:
     case GL_INTENSITY:
     case GL_LUMINANCE:
+    case GL_LUMINANCE_INTEGER_EXT:
     case GL_DEPTH_COMPONENT:
     case GL_STENCIL_INDEX:
         return 1;
     case GL_DEPTH_STENCIL:
     case GL_LUMINANCE_ALPHA:
+    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
     case GL_RG:
-    case GL_HILO_NV:
-    case GL_DSDT_NV:
+    case GL_RG_INTEGER:
+    case GL_422_EXT: // (luminance, chrominance)
+    case GL_422_REV_EXT: // (luminance, chrominance)
+    case GL_422_AVERAGE_EXT: // (luminance, chrominance)
+    case GL_422_REV_AVERAGE_EXT: // (luminance, chrominance)
+    case GL_HILO_NV: // (hi, lo)
+    case GL_DSDT_NV: // (ds, dt)
+    case GL_YCBCR_422_APPLE: // (luminance, chroma)
+    case GL_RGB_422_APPLE: // (G, B) on even pixels, (G, R) on odd pixels
+    case GL_YCRCB_422_SGIX: // (Y, [Cb,Cr])
         return 2;
     case GL_RGB:
+    case GL_RGB_INTEGER:
     case GL_BGR:
-    case GL_DSDT_MAG_NV:
+    case GL_BGR_INTEGER:
+    case GL_DSDT_MAG_NV: // (ds, dt, magnitude)
+    case GL_YCRCB_444_SGIX: // (Cb, Y, Cr)
         return 3;
     case GL_RGBA:
+    case GL_RGBA_INTEGER:
     case GL_BGRA:
+    case GL_BGRA_INTEGER:
     case GL_ABGR_EXT:
     case GL_CMYK_EXT:
-    case GL_DSDT_MAG_VIB_NV:
+    case GL_DSDT_MAG_VIB_NV: // (ds, dt, magnitude, vibrance)
         return 4;
     case GL_CMYKA_EXT:
         return 5;
+    case GL_FORMAT_SUBSAMPLE_24_24_OML:
+    case GL_FORMAT_SUBSAMPLE_244_244_OML:
+        // requires UNSIGNED_INT_10_10_10_2, so this value will be ignored
+        return 0;
     default:
         os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
         return 0;
@@ -815,6 +968,47 @@ _glClearBuffer_size(GLenum buffer)
     }
 }
 
+
+/**
+ * Helper function for determining the string lengths for glShaderSource and
+ * glShaderSourceARB, which is a tad too complex to inline in the specs.
+ */
+template<class Char>
+static inline size_t
+_glShaderSource_length(const Char * const * string, const GLint *length, GLsizei index)
+{
+    if (length != NULL && length[index] >= 0) {
+        return (size_t)length[index];
+    } else {
+        return strlen(string[index]);
+    }
+}
+
+/**
+ * Helper function for determining the string lengths for glGetDebugMessageLog*.
+ */
+template<class Char>
+static inline size_t
+_glGetDebugMessageLog_length(const Char * string, const GLsizei *lengths, GLuint count)
+{
+    size_t size = 0;
+    GLuint index;
+    if (lengths) {
+        for (index = 0; index < count; ++index) {
+            size += lengths[index];
+        }
+    } else {
+        for (index = 0; index < count; ++index) {
+            size += strlen(&string[size]) + 1;
+        }
+    }
+    if (size) {
+        // Remove the last null terminator
+        --size;
+    }
+    return size;
+}
+
 /* 
  * attribute list, terminated by the given terminator.
  */