- implemented shader support for rendering the SW renderer canvas with legacy OpenGL.

This commit is contained in:
Christoph Oelckers 2018-04-08 13:04:44 +02:00
parent 8cd3cf04e7
commit 94e8d59dde
7 changed files with 43 additions and 11 deletions

View file

@ -138,7 +138,7 @@ CUSTOM_CVAR(Int, gl_lightmode, 3, CVAR_ARCHIVE | CVAR_NOINITCALL)
if (newself > 4) newself = 8; // use 8 for software lighting to avoid conflicts with the bit mask
if (newself < 0) newself = 0;
if (self != newself) self = newself;
else if (level.info->lightmode == -1) level.lightmode = self;
else if ((level.info == nullptr || level.info->lightmode == -1)) level.lightmode = self;
}

View file

@ -130,6 +130,31 @@ void gl_PatchMenu()
void gl_SetTextureMode(int type)
{
if (type == TM_SWCANVAS)
{
int shader = V_IsTrueColor() ? 2 : 0;
float c1[4], c2[4];
if (gl_RenderState.mColormapState > CM_DEFAULT && gl_RenderState.mColormapState < CM_MAXCOLORMAP)
{
FSpecialColormap *scm = &SpecialColormaps[gl_RenderState.mColormapState - CM_FIRSTSPECIALCOLORMAP];
for (int i = 0; i < 3; i++)
{
c1[i] = scm->ColorizeStart[i];
c2[i] = scm->ColorizeEnd[i] - scm->ColorizeStart[i];
}
c1[3] = 0;
c2[3] = 0;
shader++;
}
// Type 2 (unaltered true color) can be done without activating the shader.
if (shader != 2)
{
GLRenderer->legacyShaders->BindShader(shader, c1, c2);
return;
}
else type = TM_MODULATE;
}
glUseProgram(0);
if (type == TM_MASK)
{
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

View file

@ -78,7 +78,7 @@ LegacyShaderContainer::~LegacyShaderContainer()
for (auto p : Shaders) if (p) delete p;
}
LegacyShader* LegacyShaderContainer::CreatePixelShader(const FString& vertexsrc, const FString& fragmentsrc, const FString &defines)
LegacyShader* LegacyShaderContainer::CreatePixelShader(const FString& vertex, const FString& fragment, const FString &defines)
{
auto shader = new LegacyShader();
char buffer[10000];
@ -91,6 +91,7 @@ LegacyShader* LegacyShaderContainer::CreatePixelShader(const FString& vertexsrc,
if (shader->FragmentShader == 0) { Printf("glCreateShader(GL_FRAGMENT_SHADER) failed. Disabling OpenGL hardware acceleration.\n"); return nullptr; }
{
FString vertexsrc = defines + vertex;
int lengths[1] = { (int)vertexsrc.Len() };
const char *sources[1] = { vertexsrc.GetChars() };
glShaderSource(shader->VertexShader, 1, sources, lengths);
@ -98,6 +99,7 @@ LegacyShader* LegacyShaderContainer::CreatePixelShader(const FString& vertexsrc,
}
{
FString fragmentsrc = defines + fragment;
int lengths[1] = { (int)fragmentsrc.Len() };
const char *sources[1] = { fragmentsrc.GetChars() };
glShaderSource(shader->FragmentShader, 1, sources, lengths);
@ -138,8 +140,8 @@ LegacyShader* LegacyShaderContainer::CreatePixelShader(const FString& vertexsrc,
shader->ConstantLocations[0] = glGetUniformLocation(shader->Program, "uColor1");
shader->ConstantLocations[1] = glGetUniformLocation(shader->Program, "uColor2");
shader->ImageLocation = glGetUniformLocation(shader->Program, "Image");
shader->PaletteLocation = glGetUniformLocation(shader->Program, "Palette");
shader->ImageLocation = glGetUniformLocation(shader->Program, "tex");
shader->PaletteLocation = glGetUniformLocation(shader->Program, "palette");
return shader;
}

View file

@ -802,7 +802,7 @@ void FGLRenderer::Draw2D(F2DDrawer *drawer)
{
auto index = cmd.mSpecialColormap - &SpecialColormaps[0];
if (index < 0 || (unsigned)index >= SpecialColormaps.Size()) index = 0; // if it isn't in the table FBitmap cannot use it. Shouldn't happen anyway.
if (!gl.legacyMode)
if (!gl.legacyMode || cmd.mTexture->UseType == ETextureType::SWCanvas)
{
gl_RenderState.SetFixedColormap(CM_FIRSTSPECIALCOLORMAPFORCED + int(index));
}

View file

@ -73,6 +73,7 @@ enum EPassType
class FRenderState
{
friend void gl_SetTextureMode(int type);
bool mTextureEnabled;
bool mFogEnabled;
bool mGlowEnabled;

View file

@ -59,7 +59,11 @@ public:
int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf)
{
memcpy(bmp->GetPixels(), GPalette.BaseColors, 1024);
PalEntry *pe = (PalEntry*)bmp->GetPixels();
for (int i = 0; i < 256; i++)
{
pe[i] = GPalette.BaseColors[i].d | 0xff000000;
}
return 0;
}
};

View file

@ -1,5 +1,5 @@
uniform sampler2D tex;
uniform sampler2D pal;
uniform sampler2D palette;
uniform vec4 uColor1;
uniform vec4 uColor2;
@ -7,8 +7,8 @@ vec4 TextureLookup(vec2 tex_coord)
{
#ifdef PAL_TEX
float index = texture2D(tex, tex_coord).x;
index = index * 256.0 + 0.5; // We only have full 256-color palettes here.
return texture2D(pal, vec2(index, 0.5));
index = ((index * 255.0) + 0.5) / 256.0;
return texture2D(palette, vec2(index, 0.5));
#else
return texture2D(tex, tex_coord);
#endif
@ -16,13 +16,13 @@ vec4 TextureLookup(vec2 tex_coord)
float grayscale(vec4 rgb)
{
return dot(color.rgb, vec3(0.4, 0.56, 0.14));
return dot(rgb.rgb, vec3(0.4, 0.56, 0.14));
}
void main()
{
#ifndef SPECIALCM
FragColor = TextureLookup(gl_TexCoord[0].xy);
gl_FragColor = TextureLookup(gl_TexCoord[0].xy);
#else
vec4 frag = TextureLookup(gl_TexCoord[0].xy);
float gray = grayscale(frag);