From d03acd3b6c6d958ee6a1d30754b946929a1db7c7 Mon Sep 17 00:00:00 2001 From: baldurk Date: Wed, 29 Jul 2020 15:17:45 +0100 Subject: [PATCH] Test reading instanced data via client memory pointers in GL --- util/test/demos/gl/gl_vao_0.cpp | 33 +++++++++++++++++++++++++++++++-- util/test/tests/GL/GL_VAO_0.py | 21 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/util/test/demos/gl/gl_vao_0.cpp b/util/test/demos/gl/gl_vao_0.cpp index 7973359b2..010e4f3fc 100644 --- a/util/test/demos/gl/gl_vao_0.cpp +++ b/util/test/demos/gl/gl_vao_0.cpp @@ -26,7 +26,8 @@ RD_TEST(GL_VAO_0, OpenGLGraphicsTest) { - static constexpr const char *Description = "Uses VAO 0 (i.e. never binds a VAO)"; + static constexpr const char *Description = + "Uses VAO 0 (i.e. never binds a VAO) as well as testing client memory pointer behaviour."; std::string common = R"EOSHADER( @@ -49,9 +50,11 @@ layout(location = 2) in vec2 UV; out v2f vertOut; +uniform vec4 instance_xform; + void main() { - vertOut.pos = vec4(Position.xyz, 1); + vertOut.pos = vec4(Position.x + float(gl_InstanceID % 4), Position.y + float(gl_InstanceID / 4), Position.z, 1) * vec4(instance_xform.xy, 1, 1); gl_Position = vertOut.pos; vertOut.col = Color; vertOut.uv = vec4(UV.xy, 0, 1); @@ -102,8 +105,14 @@ void main() GLuint program = MakeProgram(common + vertex, common + pixel); + GLint loc = glGetUniformLocation(program, "instance_xform"); + + glEnable(GL_SCISSOR_TEST); + while(Running()) { + glScissor(0, 0, GLsizei(screenWidth), GLsizei(screenHeight)); + float col[] = {0.2f, 0.2f, 0.2f, 1.0f}; glClearBufferfv(GL_COLOR, 0, col); @@ -116,6 +125,8 @@ void main() glUseProgram(program); + glUniform4f(loc, 1.0f, 1.0f, 0.0f, 0.0f); + // use both buffers glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib); glBindBuffer(GL_ARRAY_BUFFER, vb); @@ -150,6 +161,24 @@ void main() glViewport((screenWidth * 3) / 4, 0, GLsizei(screenWidth) / 4, GLsizei(screenHeight)); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, idxs); + // draw with instance data that requires more data than a non-instanced stream would need + glViewport(0, 0, GLsizei(screenWidth) / 2, GLsizei(screenHeight) / 2); + glScissor(0, 0, GLsizei(screenWidth) / 2, GLsizei(screenHeight) / 2); + glClearBufferfv(GL_COLOR, 0, col); + + Vec4f instcols[20] = {}; + for(size_t i = 4; i < ARRAY_COUNT(instcols); i++) + instcols[i].z = 0.5f * (i - 3); + + glUniform4f(loc, 0.25f, 0.25f, 0.0f, 0.0f); + + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vec4f), instcols); + glVertexAttribDivisor(1, 1); + + glDrawElementsInstancedBaseInstance(GL_TRIANGLES, 3, GL_UNSIGNED_INT, idxs, 16, 4); + + glVertexAttribDivisor(1, 0); + Present(); } diff --git a/util/test/tests/GL/GL_VAO_0.py b/util/test/tests/GL/GL_VAO_0.py index 5bfa5c243..45223e3bd 100644 --- a/util/test/tests/GL/GL_VAO_0.py +++ b/util/test/tests/GL/GL_VAO_0.py @@ -52,3 +52,24 @@ class GL_VAO_0(rdtest.TestCase): draw = draw.next + draw = self.find_draw("Instanced") + + self.check(draw is not None) + + self.controller.SetFrameEvent(draw.eventId, False) + + # Each instance should have color output of 0.5 * instance in blue + for i in range(0, draw.numInstances): + postvs_data = self.get_postvs(draw, rd.MeshDataStage.VSOut, 0, draw.numIndices, i) + + postvs_ref = { + 0: { + 'vtx': 0, + 'idx': 0, + 'v2f_block.col': [0.0, 0.0, 0.5*(i+1), 0.0], + }, + } + + self.check_mesh_data(postvs_ref, postvs_data) + + rdtest.log.success('Instance {} is OK'.format(i))