Fixed: depth tested FBO did not work

This commit is contained in:
ZZYZX 2019-12-15 18:48:38 +02:00
parent 6f122d28c3
commit 99a90512f1
4 changed files with 28 additions and 14 deletions

View file

@ -218,12 +218,18 @@ void RenderDevice::SetSamplerState(TextureAddress addressU, TextureAddress addre
}
}
void RenderDevice::ApplyViewport()
{
glViewport(0, 0, mViewportWidth, mViewportHeight);
}
void RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
{
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 };
static const int toVertexStart[] = { 0, 0, 2 };
ApplyViewport();
if (mNeedApply) ApplyChanges();
glDrawArrays(modes[(int)type], startIndex, toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type]);
}
@ -234,6 +240,7 @@ void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive
static const int toVertexCount[] = { 2, 3, 1 };
static const int toVertexStart[] = { 0, 0, 2 };
ApplyViewport();
if (mNeedApply) ApplyChanges();
glDrawElements(modes[(int)type], toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type], GL_UNSIGNED_INT, (const void*)(startIndex * sizeof(uint32_t)));
}
@ -246,6 +253,7 @@ void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCou
int vertcount = toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type];
ApplyViewport();
if (mNeedApply) ApplyChanges();
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
@ -263,12 +271,16 @@ void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bo
if (target)
{
glBindFramebuffer(GL_FRAMEBUFFER, target->GetFramebuffer(usedepthbuffer));
glViewport(0, 0, target->GetWidth(), target->GetHeight());
mViewportWidth = target->GetWidth();
mViewportHeight = target->GetHeight();
ApplyViewport();
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, Context->GetWidth(), Context->GetHeight());
mViewportWidth = Context->GetWidth();
mViewportHeight = Context->GetHeight();
ApplyViewport();
}
if (clear && usedepthbuffer)

View file

@ -115,6 +115,7 @@ public:
void InvalidateTexture(Texture* texture);
void ApplyViewport();
void ApplyChanges();
void ApplyVertexBuffer();
void ApplyIndexBuffer();
@ -184,4 +185,7 @@ public:
bool mRasterizerStateChanged = true;
bool mContextIsCurrent = false;
int mViewportWidth = 0;
int mViewportHeight = 0;
};

View file

@ -42,11 +42,9 @@ void Texture::Invalidate()
{
if (mDepthRenderbuffer) glDeleteRenderbuffers(1, &mDepthRenderbuffer);
if (mFramebuffer) glDeleteFramebuffers(1, &mFramebuffer);
if (mFramebufferDepth) glDeleteFramebuffers(1, &mFramebufferDepth);
if (mTexture) glDeleteTextures(1, &mTexture);
mDepthRenderbuffer = 0;
mFramebuffer = 0;
mFramebufferDepth = 0;
mTexture = 0;
}
@ -111,8 +109,6 @@ GLuint Texture::GetFramebuffer(bool usedepthbuffer)
return mFramebuffer;
}
else
{
if (mFramebuffer == mFramebufferDepth)
{
if (mDepthRenderbuffer == 0)
{
@ -122,6 +118,8 @@ GLuint Texture::GetFramebuffer(bool usedepthbuffer)
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
if (mFramebuffer == 0)
{
GLuint texture = GetTexture();
glGenFramebuffers(1, &mFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
@ -129,8 +127,9 @@ GLuint Texture::GetFramebuffer(bool usedepthbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthRenderbuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("glCheckFramebufferStatus did not return GL_FRAMEBUFFER_COMPLETE");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
return mFramebufferDepth;
return mFramebuffer;
}
}

View file

@ -39,6 +39,5 @@ private:
std::map<int, std::vector<uint32_t>> mPixels;
GLuint mTexture = 0;
GLuint mFramebuffer = 0;
GLuint mFramebufferDepth = 0;
GLuint mDepthRenderbuffer = 0;
};