Hook in gamma shader

This commit is contained in:
Magnus Norddahl 2016-10-11 13:09:32 +02:00
parent 01dc9de8d1
commit 7911302ad8
3 changed files with 86 additions and 34 deletions

View file

@ -150,7 +150,6 @@ OpenGLSWFrameBuffer::OpenGLSWFrameBuffer(void *hMonitor, int width, int height,
{ {
Shaders[i] = nullptr; Shaders[i] = nullptr;
} }
GammaShader = nullptr;
VSync = vid_vsync; VSync = vid_vsync;
BlendingRect.left = 0; BlendingRect.left = 0;
BlendingRect.top = 0; BlendingRect.top = 0;
@ -213,6 +212,12 @@ OpenGLSWFrameBuffer::~OpenGLSWFrameBuffer()
delete[] QuadExtra; delete[] QuadExtra;
} }
OpenGLSWFrameBuffer::HWFrameBuffer::~HWFrameBuffer()
{
if (Framebuffer != 0) glDeleteFramebuffers(1, (GLuint*)&Framebuffer);
delete Texture;
}
OpenGLSWFrameBuffer::HWTexture::~HWTexture() OpenGLSWFrameBuffer::HWTexture::~HWTexture()
{ {
if (Texture != 0) glDeleteTextures(1, (GLuint*)&Texture); if (Texture != 0) glDeleteTextures(1, (GLuint*)&Texture);
@ -262,6 +267,43 @@ OpenGLSWFrameBuffer::HWPixelShader::~HWPixelShader()
if (FragmentShader != 0) glDeleteShader(FragmentShader); if (FragmentShader != 0) glDeleteShader(FragmentShader);
} }
bool OpenGLSWFrameBuffer::CreateFrameBuffer(const FString &name, int width, int height, HWFrameBuffer **outFramebuffer)
{
auto fb = std::make_unique<HWFrameBuffer>();
if (!CreateTexture(name, width, height, 1, GL_RGBA16F, &fb->Texture))
{
outFramebuffer = nullptr;
return false;
}
glGenFramebuffers(1, (GLuint*)&fb->Framebuffer);
GLint oldFramebufferBinding = 0, oldTextureBinding = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFramebufferBinding);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureBinding);
glBindFramebuffer(GL_FRAMEBUFFER, fb->Framebuffer);
FGLDebug::LabelObject(GL_FRAMEBUFFER, fb->Framebuffer, name);
glBindTexture(GL_TEXTURE_2D, fb->Texture->Texture);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->Texture->Texture, 0);
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
glBindFramebuffer(GL_FRAMEBUFFER, oldFramebufferBinding);
glBindTexture(GL_TEXTURE_2D, oldTextureBinding);
if (result != GL_FRAMEBUFFER_COMPLETE)
{
outFramebuffer = nullptr;
return false;
}
*outFramebuffer = fb.release();
return true;
}
bool OpenGLSWFrameBuffer::CreatePixelShader(FString vertexsrc, FString fragmentsrc, const FString &defines, HWPixelShader **outShader) bool OpenGLSWFrameBuffer::CreatePixelShader(FString vertexsrc, FString fragmentsrc, const FString &defines, HWPixelShader **outShader)
{ {
auto shader = std::make_unique<HWPixelShader>(); auto shader = std::make_unique<HWPixelShader>();
@ -398,6 +440,7 @@ bool OpenGLSWFrameBuffer::CreateTexture(const FString &name, int width, int heig
{ {
case GL_R8: srcformat = GL_RED; break; case GL_R8: srcformat = GL_RED; break;
case GL_RGBA8: srcformat = GL_BGRA; break; case GL_RGBA8: srcformat = GL_BGRA; break;
case GL_RGBA16F: srcformat = GL_RGBA; break;
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: srcformat = GL_RGB; break; case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: srcformat = GL_RGB; break;
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: srcformat = GL_RGBA; break; case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: srcformat = GL_RGBA; break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: srcformat = GL_RGBA; break; case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: srcformat = GL_RGBA; break;
@ -552,11 +595,28 @@ void OpenGLSWFrameBuffer::DrawTriangleList(int minIndex, int numVertices, int st
void OpenGLSWFrameBuffer::Present() void OpenGLSWFrameBuffer::Present()
{ {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
FBVERTEX verts[4];
CalcFullscreenCoords(verts, false, true, 0, 0xFFFFFFFF);
for (int i = 0; i < 4; i++)
verts[i].tv = 1.0f - verts[i].tv;
SetTexture(0, OutputFB->Texture);
SetPixelShader(Shaders[SHADER_GammaCorrection]);
SetAlphaBlend(0);
EnableAlphaTest(false);
DrawTriangleFans(2, verts);
SwapBuffers(); SwapBuffers();
Debug->Update();
glViewport(0, 0, GetClientWidth(), GetClientHeight()); glViewport(0, 0, GetClientWidth(), GetClientHeight());
float screensize[4] = { (float)GetClientWidth(), (float)GetClientHeight(), 1.0f, 1.0f }; float screensize[4] = { (float)GetClientWidth(), (float)GetClientHeight(), 1.0f, 1.0f };
SetPixelShaderConstantF(PSCONST_ScreenSize, screensize, 1); SetPixelShaderConstantF(PSCONST_ScreenSize, screensize, 1);
Debug->Update();
glBindFramebuffer(GL_FRAMEBUFFER, OutputFB->Framebuffer);
} }
//========================================================================== //==========================================================================
@ -617,6 +677,11 @@ bool OpenGLSWFrameBuffer::CreateResources()
{ {
return false; return false;
} }
if (!CreateFrameBuffer("OutputFB", Width, Height, &OutputFB))
return false;
glBindFramebuffer(GL_FRAMEBUFFER, OutputFB->Framebuffer);
if (!CreateFBTexture() || if (!CreateFBTexture() ||
!CreatePaletteTexture()) !CreatePaletteTexture())
{ {
@ -698,7 +763,6 @@ void OpenGLSWFrameBuffer::ReleaseResources()
{ {
SafeRelease(Shaders[i]); SafeRelease(Shaders[i]);
} }
GammaShader = nullptr;
if (ScreenWipe != nullptr) if (ScreenWipe != nullptr)
{ {
delete ScreenWipe; delete ScreenWipe;
@ -720,11 +784,17 @@ void OpenGLSWFrameBuffer::ReleaseDefaultPoolItems()
SafeRelease(InitialWipeScreen); SafeRelease(InitialWipeScreen);
SafeRelease(VertexBuffer); SafeRelease(VertexBuffer);
SafeRelease(IndexBuffer); SafeRelease(IndexBuffer);
SafeRelease(OutputFB);
} }
bool OpenGLSWFrameBuffer::Reset() bool OpenGLSWFrameBuffer::Reset()
{ {
ReleaseDefaultPoolItems(); ReleaseDefaultPoolItems();
if (!CreateFrameBuffer("OutputFB", Width, Height, &OutputFB))
return false;
glBindFramebuffer(GL_FRAMEBUFFER, OutputFB->Framebuffer);
if (!CreateFBTexture() || !CreateVertexes()) if (!CreateFBTexture() || !CreateVertexes())
{ {
return false; return false;
@ -1086,7 +1156,6 @@ void OpenGLSWFrameBuffer::Flip()
assert(InScene); assert(InScene);
DrawLetterbox(); DrawLetterbox();
DoWindowedGamma();
Present(); Present();
InScene = false; InScene = false;
@ -1243,33 +1312,6 @@ void OpenGLSWFrameBuffer::DrawLetterbox()
} }
} }
//==========================================================================
//
// OpenGLSWFrameBuffer :: DoWindowedGamma
//
// Draws the render target texture to the real back buffer using a gamma-
// correcting pixel shader.
//
//==========================================================================
void OpenGLSWFrameBuffer::DoWindowedGamma()
{
/*if (OldRenderTarget != nullptr)
{
FBVERTEX verts[4];
CalcFullscreenCoords(verts, false, true, 0, 0xFFFFFFFF);
SetRenderTarget(0, OldRenderTarget);
SetTexture(0, TempRenderTexture);
SetPixelShader(Windowed && GammaShader ? GammaShader : Shaders[SHADER_NormalColor]);
SetAlphaBlend(0);
EnableAlphaTest(false);
DrawTriangleFans(2, verts);
delete OldRenderTarget;
OldRenderTarget = nullptr;
}*/
}
void OpenGLSWFrameBuffer::UploadPalette() void OpenGLSWFrameBuffer::UploadPalette()
{ {
if (PaletteTexture->Buffers[0] == 0) if (PaletteTexture->Buffers[0] == 0)

View file

@ -113,6 +113,16 @@ private:
int Format = 0; int Format = 0;
}; };
class HWFrameBuffer
{
public:
~HWFrameBuffer();
int Framebuffer = 0;
HWTexture *Texture = nullptr;
};
class HWVertexBuffer class HWVertexBuffer
{ {
public: public:
@ -157,6 +167,7 @@ private:
int BurnLocation = -1; int BurnLocation = -1;
}; };
bool CreateFrameBuffer(const FString &name, int width, int height, HWFrameBuffer **outFramebuffer);
bool CreatePixelShader(FString vertexsrc, FString fragmentsrc, const FString &defines, HWPixelShader **outShader); bool CreatePixelShader(FString vertexsrc, FString fragmentsrc, const FString &defines, HWPixelShader **outShader);
bool CreateVertexBuffer(int size, HWVertexBuffer **outVertexBuffer); bool CreateVertexBuffer(int size, HWVertexBuffer **outVertexBuffer);
bool CreateIndexBuffer(int size, HWIndexBuffer **outIndexBuffer); bool CreateIndexBuffer(int size, HWIndexBuffer **outIndexBuffer);
@ -376,7 +387,6 @@ private:
bool SetStyle(OpenGLTex *tex, DrawParms &parms, uint32_t &color0, uint32_t &color1, BufferedTris &quad); bool SetStyle(OpenGLTex *tex, DrawParms &parms, uint32_t &color0, uint32_t &color1, BufferedTris &quad);
static int GetStyleAlpha(int type); static int GetStyleAlpha(int type);
static void SetColorOverlay(uint32_t color, float alpha, uint32_t &color0, uint32_t &color1); static void SetColorOverlay(uint32_t color, float alpha, uint32_t &color0, uint32_t &color1);
void DoWindowedGamma();
void AddColorOnlyQuad(int left, int top, int width, int height, uint32_t color); void AddColorOnlyQuad(int left, int top, int width, int height, uint32_t color);
void AddColorOnlyRect(int left, int top, int width, int height, uint32_t color); void AddColorOnlyRect(int left, int top, int width, int height, uint32_t color);
void CheckQuadBatch(int numtris = 2, int numverts = 4); void CheckQuadBatch(int numtris = 2, int numverts = 4);
@ -404,6 +414,8 @@ private:
float ShaderConstants[NumPSCONST * 4]; float ShaderConstants[NumPSCONST * 4];
HWPixelShader *CurrentShader = nullptr; HWPixelShader *CurrentShader = nullptr;
HWFrameBuffer *OutputFB = nullptr;
bool AlphaTestEnabled = false; bool AlphaTestEnabled = false;
bool AlphaBlendEnabled = false; bool AlphaBlendEnabled = false;
int AlphaBlendOp = 0; int AlphaBlendOp = 0;
@ -455,7 +467,6 @@ private:
enum { BATCH_None, BATCH_Quads, BATCH_Lines } BatchType; enum { BATCH_None, BATCH_Quads, BATCH_Lines } BatchType;
HWPixelShader *Shaders[NUM_SHADERS]; HWPixelShader *Shaders[NUM_SHADERS];
HWPixelShader *GammaShader = nullptr;
HWTexture *InitialWipeScreen = nullptr, *FinalWipeScreen = nullptr; HWTexture *InitialWipeScreen = nullptr, *FinalWipeScreen = nullptr;

View file

@ -141,7 +141,6 @@ void I_InitGraphics ()
if (currentrenderer == 1) Video = gl_CreateVideo(); if (currentrenderer == 1) Video = gl_CreateVideo();
else Video = new Win32Video(0); else Video = new Win32Video(0);
#else #else
currentrenderer = vid_renderer;
Video = gl_CreateVideo(); Video = gl_CreateVideo();
#endif #endif