Use IUniformBuffer

This commit is contained in:
Christoph Oelckers 2018-06-12 13:00:40 +02:00
parent 20e84fff5a
commit 4ddd9dde79
3 changed files with 16 additions and 10 deletions

View file

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "hwrenderer/data/uniformbuffer.h"
#include "v_video.h"
#include "gl/data/gl_uniformbuffer.h"
#include "gl_shader.h" #include "gl_shader.h"
enum class UniformType enum class UniformType
@ -41,8 +44,8 @@ public:
~ShaderUniforms() ~ShaderUniforms()
{ {
if (mBufferHandle) if (mBuffer != nullptr)
glDeleteBuffers(1, &mBufferHandle); delete mBuffer;
} }
FString CreateDeclaration(const char *name, const std::vector<UniformFieldDesc> &fields) FString CreateDeclaration(const char *name, const std::vector<UniformFieldDesc> &fields)
@ -50,7 +53,7 @@ public:
mFields = fields; mFields = fields;
FString decl; FString decl;
decl.Format("layout(std140) uniform %s\n{\n", name); decl.Format("layout(%s) uniform %s\n{\n", screen->GetUniformLayoutString(mBindingPoint).GetChars(), name);
for (const auto &field : fields) for (const auto &field : fields)
{ {
decl.AppendFormat("\t%s %s;\n", GetTypeStr(field.Type), field.Name); decl.AppendFormat("\t%s %s;\n", GetTypeStr(field.Type), field.Name);
@ -62,16 +65,17 @@ public:
void Init() void Init()
{ {
if (!mBufferHandle) if (mBuffer == nullptr)
glGenBuffers(1, (GLuint*)&mBufferHandle); mBuffer = screen->CreateUniformBuffer(sizeof(T));
} }
void Set(int index) void Set(int index)
{ {
glBindBuffer(GL_UNIFORM_BUFFER, mBufferHandle); if (mBuffer != nullptr)
glBufferData(GL_UNIFORM_BUFFER, sizeof(T), &Values, GL_STREAM_DRAW); mBuffer->SetData(&Values);
glBindBufferBase(GL_UNIFORM_BUFFER, index, mBufferHandle); // Needs to be done in an API independent way!
glBindBufferBase(GL_UNIFORM_BUFFER, index, ((GLUniformBuffer*)mBuffer)->ID());
} }
T *operator->() { return &Values; } T *operator->() { return &Values; }
@ -103,9 +107,9 @@ private:
} }
} }
GLuint mBufferHandle = 0; IUniformBuffer *mBuffer = nullptr;
std::vector<UniformFieldDesc> mFields; std::vector<UniformFieldDesc> mFields;
std::vector<int> mUniformLocations; int mBindingPoint; // Fixme: This needs to be known on init because Vulkan wants to put it into the block declaration.
}; };
class FShaderProgram class FShaderProgram

View file

@ -45,6 +45,7 @@ public:
void SetViewportRects(IntRect *bounds) override; void SetViewportRects(IntRect *bounds) override;
void BlurScene(float amount) override; void BlurScene(float amount) override;
IUniformBuffer *CreateUniformBuffer(size_t size, bool staticuse = false) override; IUniformBuffer *CreateUniformBuffer(size_t size, bool staticuse = false) override;
virtual FString GetUniformLayoutString(int bindingpoint) { return "std140"; }
// Retrieves a buffer containing image data for a screenshot. // Retrieves a buffer containing image data for a screenshot.

View file

@ -416,6 +416,7 @@ public:
// Interface to hardware rendering resources // Interface to hardware rendering resources
virtual IUniformBuffer *CreateUniformBuffer(size_t size, bool staticuse = false) { return nullptr; } virtual IUniformBuffer *CreateUniformBuffer(size_t size, bool staticuse = false) { return nullptr; }
virtual FString GetUniformLayoutString(int bindingpoint) { return ""; }
// Begin 2D drawing operations. // Begin 2D drawing operations.
// Returns true if hardware-accelerated 2D has been entered, false if not. // Returns true if hardware-accelerated 2D has been entered, false if not.