• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By fleissi
      Hey guys!

      I'm new here and I recently started developing my own rendering engine. It's open source, based on OpenGL/DirectX and C++.
      The full source code is hosted on github:
      https://github.com/fleissna/flyEngine

      I would appreciate if people with experience in game development / engine desgin could take a look at my source code. I'm looking for honest, constructive criticism on how to improve the engine.
      I'm currently writing my master's thesis in computer science and in the recent year I've gone through all the basics about graphics programming, learned DirectX and OpenGL, read some articles on Nvidia GPU Gems, read books and integrated some of this stuff step by step into the engine.

      I know about the basics, but I feel like there is some missing link that I didn't get yet to merge all those little pieces together.

      Features I have so far:
      - Dynamic shader generation based on material properties
      - Dynamic sorting of meshes to be renderd based on shader and material
      - Rendering large amounts of static meshes
      - Hierarchical culling (detail + view frustum)
      - Limited support for dynamic (i.e. moving) meshes
      - Normal, Parallax and Relief Mapping implementations
      - Wind animations based on vertex displacement
      - A very basic integration of the Bullet physics engine
      - Procedural Grass generation
      - Some post processing effects (Depth of Field, Light Volumes, Screen Space Reflections, God Rays)
      - Caching mechanisms for textures, shaders, materials and meshes

      Features I would like to have:
      - Global illumination methods
      - Scalable physics
      - Occlusion culling
      - A nice procedural terrain generator
      - Scripting
      - Level Editing
      - Sound system
      - Optimization techniques

      Books I have so far:
      - Real-Time Rendering Third Edition
      - 3D Game Programming with DirectX 11
      - Vulkan Cookbook (not started yet)

      I hope you guys can take a look at my source code and if you're really motivated, feel free to contribute :-)
      There are some videos on youtube that demonstrate some of the features:
      Procedural grass on the GPU
      Procedural Terrain Engine
      Quadtree detail and view frustum culling

      The long term goal is to turn this into a commercial game engine. I'm aware that this is a very ambitious goal, but I'm sure it's possible if you work hard for it.

      Bye,

      Phil
    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Drawing Multiple Instanced Meshes

This topic is 636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have multiple meshes combined into one VBO and I need to render multiple instances of each mesh. As far as I can tell, there are quite a few ways to go about it.

First, there is the question whether to have multiple index buffers or just one. Since we are already combining the vertices into one buffer, my initial thought was to do the same for indices.

The option with multiple index buffers would be:
 
Bind Vertex Buffer
Foreach Mesh:
    Bind Instance Attributes
    Bind Index Buffer
    glDrawElementsInstanced(GL_TRIANGLES, mesh.num_instances, GL_UNSIGNED_INT, 0, mesh.num_indices)

And the combined index buffer would be:
 
Bind Vertex Buffer
Bind Index Buffer
for mesh in meshes:
    Bind Instance Attributes
    glDrawElementsInstanced(GL_TRIANGLES, mesh.num_instances, GL_UNSIGNED_INT, mesh.base_index, mesh.num_indices)

Furthermore, there is glDrawElementsInstancedBaseVertex, which as far as I can tell just saves me from offsetting the indices a priori.

Finally, there is also glDrawElementsIndirect, which seems the most flexible, but also requires rather recent OpenGL.

Anyway, just wondering if my understanding is correct and if anyone has some experience on the topic they would like to share. Edited by kloffy

Share this post


Link to post
Share on other sites
Advertisement

I can (probably?) only answer one question of your post:

 

* Yes, having indices in one buffer is better, if it fits in your engine. That means if you don't have any state you have to set per object, it's best to use one vertex buffer and one index buffer, since you wouldn't have to bind buffers between draw calls. This principle is totally independent from everything else, since it heads towards minimizing driver overhead. If you do have different state to set, you would have to use drawIndirect, but I don't know much about it and have no experience with it. But it should work better when having a single index buffer too, compared to using multiple index buffers.

 

I would guess if you heavily use instancing, you are more likely to run out of processing power on the gpu side, rather than you would  face a problem with index buffer binding. I have only used isntancing with multiple index buffers as well as multiple vertex buffers. Since I had a very huge benefit from using instancing, my answer would be: Take your engine as it is for now, use instancing and decide if you are satisfied with the result - if not, try to optimize. Using a single buffer for everything can also have downsides - for example when you have to update it with data that doesn't fit in the given region anymore and so on.

Edited by hannesp

Share this post


Link to post
Share on other sites
Awesome, thanks! In my case, all per object state is in the instance attributes, so using combined buffers seems like the way to go.

I just saw there is a glDrawElementsInstancedBaseVertexBaseInstance (what a name, just rolls off the tongue), which I may be able to use, and then all of the binding would be moved out of the loop. Still trying to get my head around how exactly it would fit into the overall framework. Edited by kloffy

Share this post


Link to post
Share on other sites
Multiple draw calls are still a problem.

If you can condense this down to one draw call (hint: you can) you'll be in better shape.

Doing so implies putting all of your mesh instance attributes into a single buffer and then indexing into that buffer during the draw call.

You can generally do this using a combination of "draw id" and "instance id" (I forget their GL names). You might also need an extra indirection buffer to map a particular mesh/instance to an offset within the attribute buffer if the mesh attributes are not of uniform width.

Fewer draw calls means that the hardware can parallelize better, and gives the driver fewer chances or need to do behind-your-back state flushing or other expensive calculations.

Share this post


Link to post
Share on other sites
Yes, having a single draw call was my initial goal, but I haven't quite figured out an elegant way to achieve it.

You can generally do this using a combination of "draw id" and "instance id" (I forget their GL names). You might also need an extra indirection buffer to map a particular mesh/instance to an offset within the attribute buffer if the mesh attributes are not of uniform width.

Fewer draw calls means that the hardware can parallelize better, and gives the driver fewer chances or need to do behind-your-back state flushing or other expensive calculations.


Could you elaborate more or perhaps point me to some material on the topic? I can kind of see it with shader storage buffer objects and somehow indexing into them using instance attributes, but I am not sure if that is what you mean.

Share this post


Link to post
Share on other sites
I have implemented the glDrawElementsInstancedBaseVertexBaseInstance version:
 
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import sys

import ctypes

import numpy as np

from PyQt5 import QtCore, QtGui, QtOpenGL, QtWidgets

import OpenGL
from OpenGL import GL
from OpenGL.GL import shaders

# Helper Functions (Omitted unused types for brevity...)

INPUT_FUNCTIONS = {
    (GL.GL_FLOAT, (1,)): GL.glVertexAttribPointer,
    (GL.GL_FLOAT, (2,)): GL.glVertexAttribPointer,
    (GL.GL_FLOAT, (3,)): GL.glVertexAttribPointer,
    (GL.GL_FLOAT, (4,)): GL.glVertexAttribPointer,
}

UNIFORM_FUNCTIONS = {
    (GL.GL_FLOAT, ()): GL.glUniform1f,
    (GL.GL_FLOAT, (1,)): GL.glUniform1fv,
    (GL.GL_FLOAT, (2,)): GL.glUniform2fv,
    (GL.GL_FLOAT, (3,)): GL.glUniform3fv,
    (GL.GL_FLOAT, (4,)): GL.glUniform4fv,
    (GL.GL_FLOAT, (2, 2)): GL.glUniformMatrix2fv,
    (GL.GL_FLOAT, (3, 3)): GL.glUniformMatrix3fv,
    (GL.GL_FLOAT, (4, 4)): GL.glUniformMatrix4fv,
}

def input_setter(program, key, type):
    location = GL.glGetAttribLocation(program, key)
    function = INPUT_FUNCTIONS[type]
    gltype, shape = type
    def _input_setter(type, value, stride, offset=0, divisor=0):
        GL.glEnableVertexAttribArray(location)
        GL.glBindBuffer(type, value)
        function(location, shape[0], gltype, GL.GL_FALSE, stride, ctypes.c_void_p(int(offset)))
        GL.glVertexAttribDivisor(location, divisor)
        GL.glBindBuffer(type, 0)
    return _input_setter

def uniform_setter(program, key, type):
    location = GL.glGetUniformLocation(program, key)
    function = UNIFORM_FUNCTIONS[type]
    gltype, shape = type
    return {
        0: lambda value, count=1, transpose=False: function(location, value),
        1: lambda value, count=1, transpose=False: function(location, count, value),
        2: lambda value, count=1, transpose=False: function(location, count, transpose, value)
    }[len(shape)]


VS_SOURCE = """
#version 420
layout(location=0) in vec4 position;
layout(location=1) in vec4 color;

layout(location=2) in vec4 instance_position;
layout(location=3) in vec4 instance_color;

out VertexData {
    vec4 position;
    vec4 color;
} vs;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
   vs.position = model * (position * vec4(vec3(0.1), 1.0) + instance_position);
   vs.color = color * instance_color;
   
   gl_Position = projection * view * vs.position;
}
"""


FS_SOURCE = """
#version 420
in VertexData {
    vec4 position;
    vec4 color;
} vs;

layout(location=0) out vec4 frag_color;
void main()
{
   frag_color = vs.color; //vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
"""


def vec(v, n=4, dtype=np.float32):
    result = np.zeros(n, dtype=dtype)
    result[:min(n,len(v))] = v
    return result


def dir(v, n=4, dtype=np.float32):
    result = vec(v, n=n, dtype=dtype)
    result[-1] = 0.0
    return result


def pos(v, n=4, dtype=np.float32):
    result = vec(v, n=n, dtype=dtype)
    result[-1] = 1.0
    return result


def color_from_ordinal(i):
    return np.array([(i >> 0) & 1, (i >> 1) & 1, (i >> 2) & 1, 1], dtype=np.float32)


class Mesh(object):
    def __init__(self, vertices, indices):
        self.vertices = vertices
        self.indices = indices

class MultiMesh(object):
    @property
    def vertices(self):
        return np.concatenate([mesh.vertices for mesh in self.meshes])
    @property
    def indices(self):
        return np.concatenate([mesh.indices for mesh in self.meshes])
    def __init__(self, meshes):
        self.meshes = meshes
    def vertex_count(self, i):
        return len(self.meshes[i].vertices)
    def vertex_offset(self, i):
        return sum(self.vertex_count(j) for j in range(i))
    def index_count(self, i):
        return len(self.meshes[i].indices)
    def index_offset(self, i):
        return sum(self.index_count(j) for j in range(i))

def make_triangle():
    vertex_count = 3
    vertices = np.zeros(vertex_count, dtype=[("position", np.float32, 4),("color", np.float32, 4)])
    vertices["position"] = [pos(v) for v in [[0.0, +1.0, 0.0], [-1.0, -1.0, 0.0], [+1.0, -1.0, 0.0]]]
    vertices["color"] = [[1.0, 1.0, 1.0, 1.0]] * vertex_count
    indices = np.arange(vertex_count, dtype=np.uint8)
    return Mesh(vertices, indices)


def make_rectangle():
    vertex_count = 4
    vertices = np.zeros(vertex_count, dtype=[("position", np.float32, 4),("color", np.float32, 4)])
    vertices["position"] = [pos(v) for v in [[-1.0, -1.0, 0.0], [-1.0, +1.0, 0.0], [+1.0, -1.0, 0.0], [+1.0, +1.0, 0.0]]]
    vertices["color"] = [[1.0, 1.0, 1.0, 1.0]] * vertex_count
    indices = np.arange(vertex_count, dtype=np.uint8)
    return Mesh(vertices, indices)


class MainWindow(QtGui.QOpenGLWindow):
    def initializeGL(self):
        self.createProgram()
        
        self.instance_count = 100
        instance_data = np.zeros(self.instance_count, dtype=[("instance_position", np.float32, 4), ("instance_color", np.float32, 4),])
        instance_data["instance_position"] = [dir(v) for v in 2.0 * np.random.rand(self.instance_count, 4) - 1.0]
        instance_data["instance_color"] = [color_from_ordinal(i) for i in np.random.randint(1, 8, size=self.instance_count)] 
        
        self.mesh = MultiMesh([make_triangle(), make_rectangle()])
        
        self.createVAO(instance_data, self.mesh.vertices, self.mesh.indices)
    
    def createProgram(self):
        self.program = shaders.compileProgram(
            shaders.compileShader(VS_SOURCE, GL.GL_VERTEX_SHADER),
            shaders.compileShader(FS_SOURCE, GL.GL_FRAGMENT_SHADER),
        )
        
        self.inputs = {key: input_setter(self.program, key, type) for key, type in {
            "instance_position": (GL.GL_FLOAT, (4,)),
            "instance_color": (GL.GL_FLOAT, (4,)),
            "position": (GL.GL_FLOAT, (4,)),
            "color": (GL.GL_FLOAT, (4,)),
        }.items()}
        
        self.uniforms =  {key: uniform_setter(self.program, key, type) for key, type in {
            "model": (GL.GL_FLOAT, (4, 4)),
            "view": (GL.GL_FLOAT, (4, 4)),
            "projection": (GL.GL_FLOAT, (4, 4)),
        }.items()}
    
    def createVAO(self, instance_data, vertex_data, index_data):
        self.vao = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vao)
        
        # Create Buffers
        self.instance_vbo = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.instance_vbo)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, instance_data.nbytes, instance_data, GL.GL_STATIC_DRAW)
        
        self.vbo = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vertex_data.nbytes, vertex_data, GL.GL_STATIC_DRAW)
        
        self.ibo = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo)
        GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, index_data.nbytes, index_data, GL.GL_STATIC_DRAW)
        
        self.inputs["instance_position"](GL.GL_ARRAY_BUFFER, self.instance_vbo, instance_data.itemsize, 0 * np.dtype(np.float32).itemsize, divisor=1)
        self.inputs["instance_color"](GL.GL_ARRAY_BUFFER, self.instance_vbo, instance_data.itemsize, 4 * np.dtype(np.float32).itemsize, divisor=1)
        
        self.inputs["position"](GL.GL_ARRAY_BUFFER, self.vbo, vertex_data.itemsize, 0 * np.dtype(np.float32).itemsize)
        self.inputs["color"](GL.GL_ARRAY_BUFFER, self.vbo, vertex_data.itemsize, 4 * np.dtype(np.float32).itemsize)
        
        GL.glBindVertexArray(0)
    
    def resizeGL(self, width, height):
        self.size = np.array([width, height], dtype=np.float32)
        self.aspect = self.size/np.min(self.size)

    def paintGL(self):
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        
        try:
            GL.glUseProgram(self.program)
            
            self.uniforms["model"](np.identity(4, dtype=np.float32))
            self.uniforms["view"](np.identity(4, dtype=np.float32))
            self.uniforms["projection"](np.diag(np.r_[1.0/self.aspect,1,1]))
            
            GL.glBindVertexArray(self.vao)
            
            mesh_count = len(self.mesh.meshes)
            for i in range(mesh_count):
                index_count = self.mesh.index_count(i)
                index_offset = ctypes.c_void_p(self.mesh.index_offset(i) * np.dtype(np.uint8).itemsize)
                base_vertex = self.mesh.vertex_offset(i)
                instance_count = self.instance_count//mesh_count
                base_instance = i*instance_count
                GL.glDrawElementsInstancedBaseVertexBaseInstance(GL.GL_TRIANGLE_STRIP, index_count, GL.GL_UNSIGNED_BYTE, index_offset, instance_count, base_vertex, base_instance)
            
        finally:
            GL.glBindVertexArray(0)
            GL.glUseProgram(0)
    
    def keyReleaseEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            QtWidgets.QApplication.instance().quit()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    
    format = QtGui.QSurfaceFormat()
    format.setDepthBufferSize(24)
    format.setStencilBufferSize(8)
    format.setProfile(QtGui.QSurfaceFormat.CoreProfile)
    format.setVersion(4, 2)
    
    window = MainWindow()
    window.setFormat(format)
    window.resize(640, 480)
    window.show()
    
    sys.exit(app.exec_())

Apologies that the code is a bit verbose (OpenGL... :-/), but it is completely self-contained. Just needs PyQT5, PyOpenGL and numpy. The key section is the paint method:
 
    def paintGL(self):
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        
        try:
            GL.glUseProgram(self.program)
            
            self.uniforms["model"](np.identity(4, dtype=np.float32))
            self.uniforms["view"](np.identity(4, dtype=np.float32))
            self.uniforms["projection"](np.diag(np.r_[1.0/self.aspect,1,1]))
            
            GL.glBindVertexArray(self.vao)
            
            mesh_count = len(self.mesh.meshes)
            for i in range(mesh_count):
                index_count = self.mesh.index_count(i)
                index_offset = ctypes.c_void_p(self.mesh.index_offset(i) * np.dtype(np.uint8).itemsize)
                base_vertex = self.mesh.vertex_offset(i)
                instance_count = self.instance_count//mesh_count
                base_instance = i*instance_count
                GL.glDrawElementsInstancedBaseVertexBaseInstance(GL.GL_TRIANGLE_STRIP, index_count, GL.GL_UNSIGNED_BYTE, index_offset, instance_count, base_vertex, base_instance)
            
        finally:
            GL.glBindVertexArray(0)
            GL.glUseProgram(0)

Consequently, this version has one draw call per mesh. Still scratching my head over how to do it with a single draw call. Edited by kloffy

Share this post


Link to post
Share on other sites
Just a quick update on the reducing draw calls issue: I still don't see a way of doing it without one of the glDraw*Indirect variants. However, that would require pretty recent extensions, some of which are not in core GL yet:

https://www.opengl.org/registry/specs/ARB/multi_draw_indirect.txt

https://www.opengl.org/registry/specs/ARB/shader_draw_parameters.txt

I think for now I might just have to live with the extra draw calls.

Share this post


Link to post
Share on other sites
Turns out I was already 90% there towards using multi draw indirect. Just had to stick the parameters into a draw indirect buffer.

        def commands(mesh):
            mesh_count = len(mesh.meshes)
            for i in range(mesh_count):
                count = mesh.index_count(i)
                first_index = mesh.index_offset(i)
                base_vertex = mesh.vertex_offset(i)
                instance_count = self.instance_count//mesh_count
                base_instance = i*instance_count
                yield count, instance_count, first_index, base_vertex, base_instance
        
        self.command_data = np.array(list(commands(self.mesh)), dtype=[
            ("count", np.uint32, 1),
            ("instanceCount", np.uint32, 1),
            ("firstIndex", np.uint32, 1),
            ("baseVertex", np.uint32, 1),
            ("baseInstance", np.uint32, 1),
        ])

It does wonders for performance, especially with the Python bindings, since it eliminates almost all of the interpreter/wrapper overhead. Nice. Edited by kloffy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement