Skip to content
Permalink
Browse files

- remove deoendency on global render state in buffer binding functions.

  • Loading branch information...
coelckers committed Jun 9, 2019
1 parent b7f4a61 commit 99d4cc18aaa33030d25fbb15859c33d743525410
@@ -235,7 +235,7 @@ void FGLRenderer::DrawPresentTexture(const IntRect &box, bool applyGamma)
}
mPresentShader->Uniforms->Scale = { screen->mScreenViewport.width / (float)mBuffers->GetWidth(), screen->mScreenViewport.height / (float)mBuffers->GetHeight() };
mPresentShader->Uniforms->Offset = { 0.0f, 0.0f };
mPresentShader->Uniforms.Set();
mPresentShader->Uniforms.SetAndBind(gl_RenderState);
RenderScreenQuad();
}

@@ -955,7 +955,7 @@ void GLPPRenderState::Draw()
if (!shader->Uniforms)
shader->Uniforms.reset(screen->CreateDataBuffer(POSTPROCESS_BINDINGPOINT, false, false));
shader->Uniforms->SetData(Uniforms.Data.Size(), Uniforms.Data.Data());
shader->Uniforms->BindBase();
shader->Uniforms->BindBase(gl_RenderState);
}

// Set shader
@@ -191,12 +191,17 @@ void FGLRenderer::UpdateShadowMap()

FGLPostProcessState savedState;

// rebind the buffers
screen->mShadowMap.mLightList->BindBase(gl_RenderState);
screen->mShadowMap.mNodesBuffer->BindBase(gl_RenderState);
screen->mShadowMap.mLinesBuffer->BindBase(gl_RenderState);

mBuffers->BindShadowMapFB();

mShadowMapShader->Bind();
mShadowMapShader->Uniforms->ShadowmapQuality = gl_shadowmap_quality;
mShadowMapShader->Uniforms->NodesCount = screen->mShadowMap.NodesCount();
mShadowMapShader->Uniforms.Set();
mShadowMapShader->Uniforms.SetAndBind(gl_RenderState);

glViewport(0, 0, gl_shadowmap_quality, 1024);
RenderScreenQuad();
@@ -189,7 +189,7 @@ bool FGLRenderState::ApplyShader()
matrixToGL(identityMatrix, activeShader->normalmodelmatrix_index);
}

auto index = screen->mLights->BindUBO(mLightIndex);
auto index = screen->mLights->BindUBO(*this, mLightIndex);
activeShader->muLightIndex.Set(index);

return true;
@@ -174,7 +174,7 @@ void FGLRenderer::prepareInterleavedPresent(FPresentShaderBase& shader)
screen->mScreenViewport.height / (float)mBuffers->GetHeight()
};
shader.Uniforms->Offset = { 0.0f, 0.0f };
shader.Uniforms.Set();
shader.Uniforms.SetAndBind(gl_RenderState); // Screw OpenGL's global state... :(
}

//==========================================================================
@@ -198,7 +198,7 @@ void FGLRenderer::PresentColumnInterleaved()
int windowHOffset = 0;

mPresent3dColumnShader->Uniforms->WindowPositionParity = windowHOffset;
mPresent3dColumnShader->Uniforms.Set();
mPresent3dColumnShader->Uniforms.SetAndBind(gl_RenderState);

RenderScreenQuad();
}
@@ -225,7 +225,7 @@ void FGLRenderer::PresentRowInterleaved()
+ screen->mOutputLetterbox.height + 1 // +1 because of origin at bottom
) % 2;

mPresent3dRowShader->Uniforms.Set();
mPresent3dRowShader->Uniforms.SetAndBind(gl_RenderState);
RenderScreenQuad();
}

@@ -256,7 +256,7 @@ void FGLRenderer::PresentCheckerInterleaved()
+ screen->mOutputLetterbox.height + 1 // +1 because of origin at bottom
) % 2; // because we want the top pixel offset, but gl_FragCoord.y is the bottom pixel offset

mPresent3dCheckerShader->Uniforms.Set();
mPresent3dCheckerShader->Uniforms.SetAndBind(gl_RenderState);
RenderScreenQuad();
}

@@ -210,13 +210,13 @@ void GLVertexBuffer::Bind(int *offsets)
}
}

void GLDataBuffer::BindRange(size_t start, size_t length)
void GLDataBuffer::BindRange(FRenderState&, size_t start, size_t length)
{
if (mUseType == GL_UNIFORM_BUFFER) // SSBO's cannot be rebound.
glBindBufferRange(mUseType, mBindingPoint, mBufferId, start, length);
}

void GLDataBuffer::BindBase()
void GLDataBuffer::BindBase(FRenderState&)
{
glBindBufferBase(mUseType, mBindingPoint, mBufferId);
}
@@ -66,8 +66,8 @@ class GLDataBuffer : public IDataBuffer, public GLBuffer
int mBindingPoint;
public:
GLDataBuffer(int bindingpoint, bool is_ssbo) : GLBuffer(is_ssbo? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER), mBindingPoint(bindingpoint) {}
void BindRange(size_t start, size_t length) override;
void BindBase() override;
void BindRange(FRenderState& state, size_t start, size_t length) override;
void BindBase(FRenderState& state) override;
};

}
@@ -3,6 +3,8 @@
#include <stddef.h>
#include <assert.h>

class FRenderState;

// The low level code needs to know which attributes exist.
// OpenGL needs to change the state of all of them per buffer binding.
// VAOs are mostly useless for this because they lump buffer and binding state together which the model code does not want.
@@ -76,7 +78,7 @@ class IDataBuffer : virtual public IBuffer
{
// Can be either uniform or shader storage buffer, depending on its needs.
public:
virtual void BindRange(size_t start, size_t length) = 0;
virtual void BindBase() = 0;
virtual void BindRange(FRenderState& state, size_t start, size_t length) = 0;
virtual void BindBase(FRenderState& state) = 0;

};
@@ -62,13 +62,13 @@ void GLViewpointBuffer::CheckSize()
}
}

int GLViewpointBuffer::Bind(FRenderState &di, unsigned int index)
int GLViewpointBuffer::Bind(FRenderState &state, unsigned int index)
{
if (index != mLastMappedIndex)
{
mLastMappedIndex = index;
mBuffer->BindRange(index * mBlockAlign, mBlockAlign);
di.EnableClipDistance(0, mClipPlaneInfo[index]);
mBuffer->BindRange(state, index * mBlockAlign, mBlockAlign);
state.EnableClipDistance(0, mClipPlaneInfo[index]);
}
return index;
}
@@ -126,14 +126,13 @@ class ShaderUniforms
mBuffer = screen->CreateDataBuffer(bindingpoint, false, false);
}

void Set(bool bind = true)
// This currently only gets called by OpenGL. The parameter is merely a dummy to satisfx the interface.
void SetAndBind(FRenderState &state)
{
if (mBuffer != nullptr)
mBuffer->SetData(sizeof(T), &Values);

// Let's hope this can be done better when things have moved further ahead.
// This really is not the best place to add something that depends on API behavior.
if (bind) mBuffer->BindBase();
mBuffer->BindBase(state);
}

T *operator->() { return &Values; }
@@ -127,15 +127,15 @@ int FLightBuffer::UploadLights(FDynLightData &data)
}
}

int FLightBuffer::DoBindUBO(unsigned int index)
int FLightBuffer::DoBindUBO(FRenderState& state, unsigned int index)
{
// this function will only get called if a uniform buffer is used. For a shader storage buffer we only need to bind the buffer once at the start.
unsigned int offset = (index / mBlockAlign) * mBlockAlign;

if (offset != mLastMappedIndex)
{
mLastMappedIndex = offset;
mBuffer->BindRange(offset * ELEMENT_SIZE, mBlockSize * ELEMENT_SIZE);
mBuffer->BindRange(state, offset * ELEMENT_SIZE, mBlockSize * ELEMENT_SIZE);
}
return (index - offset);
}
@@ -35,7 +35,7 @@ class FLightBuffer
unsigned int GetBlockSize() const { return mBlockSize; }
bool GetBufferType() const { return mBufferType; }

int DoBindUBO(unsigned int index);
int DoBindUBO(FRenderState& state, unsigned int index);

int ShaderIndex(unsigned int index) const
{
@@ -46,19 +46,19 @@ class FLightBuffer
}

// Only relevant for OpenGL, so this does not need access to the render state.
int BindUBO(int index)
int BindUBO(FRenderState& state, int index)
{
if (!mBufferType && index > -1)
{
index = DoBindUBO(index);
index = DoBindUBO(state, index);
}
return index;
}

// The parameter is a reminder for Vulkan.
void BindBase()
void BindBase(FRenderState& state)
{
mBuffer->BindBase();
mBuffer->BindBase(state);
mLastMappedIndex = UINT_MAX;
}

@@ -176,9 +176,6 @@ bool IShadowMap::PerformUpdate()
UpdateCycles.Clock();
UploadAABBTree();
UploadLights();
mLightList->BindBase();
mNodesBuffer->BindBase();
mLinesBuffer->BindBase();
return true;
}
return false;
@@ -64,6 +64,8 @@ class IShadowMap
IShadowMap(const IShadowMap &) = delete;
IShadowMap &operator=(IShadowMap &) = delete;

// OpenGL needs to rebind these regularly, so they need access from the backend.
public:
// OpenGL storage buffer with the list of lights in the shadow map texture
IDataBuffer *mLightList = nullptr;

@@ -466,7 +466,7 @@ void HWDrawInfo::RenderScene(FRenderState &state)

state.SetDepthMask(true);

screen->mLights->BindBase();
screen->mLights->BindBase(state);
state.EnableFog(true);
state.SetRenderStyle(STYLE_Source);

@@ -204,12 +204,12 @@ void VKVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t s

/////////////////////////////////////////////////////////////////////////////

void VKDataBuffer::BindRange(size_t start, size_t length)
void VKDataBuffer::BindRange(FRenderState& state, size_t start, size_t length)
{
GetVulkanFrameBuffer()->GetRenderState()->Bind(bindingpoint, (uint32_t)start);
static_cast<VkRenderState&>(state).Bind(bindingpoint, (uint32_t)start);
}

void VKDataBuffer::BindBase()
void VKDataBuffer::BindBase(FRenderState& state)
{
GetVulkanFrameBuffer()->GetRenderState()->Bind(bindingpoint, 0);
static_cast<VkRenderState&>(state).Bind(bindingpoint, 0);
}
@@ -68,8 +68,8 @@ class VKDataBuffer : public IDataBuffer, public VKBuffer
}
}

void BindRange(size_t start, size_t length) override;
void BindBase() override;
void BindRange(FRenderState &state, size_t start, size_t length) override;
void BindBase(FRenderState& state) override;

int bindingpoint;
};

0 comments on commit 99d4cc1

Please sign in to comment.
You can’t perform that action at this time.