lovr.graphics.getStats;

This commit is contained in:
bjorn 2017-12-09 20:07:32 -08:00
parent 650f100b15
commit 8eec247adb
4 changed files with 65 additions and 14 deletions

View File

@ -261,6 +261,25 @@ int l_lovrGraphicsGetDimensions(lua_State* L) {
return 2;
}
int l_lovrGraphicsGetStats(lua_State* L) {
if (lua_gettop(L) > 0) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1);
} else {
lua_createtable(L, 0, 2);
}
GraphicsStats stats = lovrGraphicsGetStats();
lua_pushinteger(L, stats.drawCalls);
lua_setfield(L, 1, "drawcalls");
lua_pushinteger(L, stats.shaderSwitches);
lua_setfield(L, 1, "shaderswitches");
return 1;
}
// State
int l_lovrGraphicsGetBackgroundColor(lua_State* L) {
@ -918,6 +937,7 @@ const luaL_Reg lovrGraphics[] = {
{ "getWidth", l_lovrGraphicsGetWidth },
{ "getHeight", l_lovrGraphicsGetHeight },
{ "getDimensions", l_lovrGraphicsGetDimensions },
{ "getStats", l_lovrGraphicsGetStats },
{ "getBackgroundColor", l_lovrGraphicsGetBackgroundColor },
{ "setBackgroundColor", l_lovrGraphicsSetBackgroundColor },
{ "getBlendMode", l_lovrGraphicsGetBlendMode },

View File

@ -86,6 +86,8 @@ void lovrGraphicsClear(bool color, bool depth) {
void lovrGraphicsPresent() {
glfwSwapBuffers(state.window);
state.stats.drawCalls = 0;
state.stats.shaderSwitches = 0;
}
void lovrGraphicsPrepare(Material* material, float* pose) {
@ -240,6 +242,10 @@ int lovrGraphicsGetHeight() {
return height;
}
GraphicsStats lovrGraphicsGetStats() {
return state.stats;
}
// State
Color lovrGraphicsGetBackgroundColor() {
@ -543,16 +549,16 @@ static void lovrGraphicsDrawPrimitive(Material* material, GLenum mode, bool hasN
if (useIndices) {
lovrGraphicsBindIndexBuffer(state.streamIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, state.streamIndices.length * sizeof(unsigned int), indices, GL_STREAM_DRAW);
glDrawElements(mode, state.streamIndices.length, GL_UNSIGNED_INT, NULL);
lovrGraphicsDrawElements(mode, state.streamIndices.length, sizeof(uint32_t), 0, 1);
} else {
glDrawArrays(mode, 0, state.streamData.length / stride);
lovrGraphicsDrawArrays(mode, 0, state.streamData.length / stride, 1);
}
}
void lovrGraphicsPoints(float* points, int count) {
lovrGraphicsSetDefaultShader(SHADER_DEFAULT);
lovrGraphicsSetStreamData(points, count);
lovrGraphicsDrawPrimitive(NULL, GL_POINTS, false, false, false);
lovrGraphicsDrawPrimitive(NULL, MESH_POINTS, false, false, false);
}
void lovrGraphicsLine(float* points, int count) {
@ -1114,6 +1120,7 @@ void lovrGraphicsUseProgram(uint32_t program) {
if (state.program != program) {
state.program = program;
glUseProgram(program);
state.stats.shaderSwitches++;
}
}
@ -1137,3 +1144,25 @@ void lovrGraphicsBindIndexBuffer(uint32_t indexBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
}
}
void lovrGraphicsDrawArrays(GLenum mode, size_t start, size_t count, int instances) {
if (instances > 1) {
glDrawArraysInstanced(mode, start, count, instances);
} else {
glDrawArrays(mode, start, count);
}
state.stats.drawCalls++;
}
void lovrGraphicsDrawElements(GLenum mode, size_t count, size_t indexSize, size_t offset, int instances) {
GLenum indexType = indexSize == sizeof(uint16_t) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
if (instances > 1) {
glDrawElementsInstanced(mode, count, indexType, (GLvoid*) offset, instances);
} else {
glDrawElements(mode, count, indexType, (GLvoid*) offset);
}
state.stats.drawCalls++;
}

View File

@ -1,5 +1,6 @@
#include "graphics/font.h"
#include "graphics/material.h"
#include "graphics/mesh.h"
#include "graphics/shader.h"
#include "graphics/texture.h"
#include "math/math.h"
@ -75,6 +76,11 @@ typedef struct {
int viewport[4];
} View;
typedef struct {
int drawCalls;
int shaderSwitches;
} GraphicsStats;
typedef struct {
GLFWwindow* window;
Shader* defaultShaders[DEFAULT_SHADER_COUNT];
@ -111,6 +117,7 @@ typedef struct {
uint32_t vertexArray;
uint32_t vertexBuffer;
uint32_t indexBuffer;
GraphicsStats stats;
} GraphicsState;
// Base
@ -123,6 +130,7 @@ void lovrGraphicsPrepare(Material* material, float* pose);
void lovrGraphicsCreateWindow(int w, int h, bool fullscreen, int msaa, const char* title, const char* icon);
int lovrGraphicsGetWidth();
int lovrGraphicsGetHeight();
GraphicsStats lovrGraphicsGetStats();
// State
Color lovrGraphicsGetBackgroundColor();
@ -192,3 +200,5 @@ void lovrGraphicsUseProgram(uint32_t program);
void lovrGraphicsBindVertexArray(uint32_t vao);
void lovrGraphicsBindVertexBuffer(uint32_t vbo);
void lovrGraphicsBindIndexBuffer(uint32_t ibo);
void lovrGraphicsDrawArrays(GLenum mode, size_t start, size_t count, int instances);
void lovrGraphicsDrawElements(GLenum mode, size_t count, size_t indexSize, size_t offset, int instances);

View File

@ -148,19 +148,11 @@ void lovrMeshDraw(Mesh* mesh, mat4 transform, float* pose, int instances) {
size_t start = mesh->rangeStart;
size_t count = mesh->rangeCount;
if (mesh->indexCount > 0) {
size_t offset = start * mesh->indexSize;
count = mesh->isRangeEnabled ? mesh->rangeCount : mesh->indexCount;
GLenum indexType = mesh->indexSize == sizeof(uint16_t) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
if (instances > 1) {
glDrawElementsInstanced(mesh->drawMode, count, indexType, (GLvoid*) (start * mesh->indexSize), instances);
} else {
glDrawElements(mesh->drawMode, count, indexType, (GLvoid*) (start * mesh->indexSize));
}
lovrGraphicsDrawElements(mesh->drawMode, count, mesh->indexSize, offset, instances);
} else {
if (instances > 1) {
glDrawArraysInstanced(mesh->drawMode, start, count, instances);
} else {
glDrawArrays(mesh->drawMode, start, count);
}
lovrGraphicsDrawArrays(mesh->drawMode, start, count, instances);
}
if (transform) {