Skip to content

Commit

Permalink
- Backend update from GZDoom.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Jan 15, 2023
1 parent 8001c40 commit fca0bdf
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 60 deletions.
2 changes: 1 addition & 1 deletion source/common/fonts/font.cpp
Expand Up @@ -422,7 +422,7 @@ void FFont::ReadSheetFont(TArray<FolderEntry> &folderdata, int width, int height
{
for (int x = 0; x < numtex_x; x++)
{
auto image = new FSheetTexture(sheetBitmaps.Size() - 1, x * width, y * width, width, height);
auto image = new FSheetTexture(sheetBitmaps.Size() - 1, x * width, y * height, width, height);
FImageTexture *imgtex = new FImageTexture(image);
auto gtex = MakeGameTexture(imgtex, nullptr, ETextureType::FontChar);
gtex->SetWorldPanning(true);
Expand Down
3 changes: 3 additions & 0 deletions source/common/rendering/gl/gl_shader.cpp
Expand Up @@ -235,6 +235,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
float uClipHeight;
float uClipHeightDirection;
int uShadowmapFilter;
int uLightBlendMode;
};
uniform int uTextureMode;
Expand Down Expand Up @@ -328,6 +330,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
uniform sampler2D texture9;
uniform sampler2D texture10;
uniform sampler2D texture11;
uniform sampler2D texture12;
// timer data
uniform float timer;
Expand Down
Expand Up @@ -91,6 +91,7 @@ void HWViewpointBuffer::Set2D(FRenderState &di, int width, int height, int pll)
matrices.mPalLightLevels = pll;
matrices.mClipLine.X = -10000000.0f;
matrices.mShadowmapFilter = gl_shadowmap_filter;
matrices.mLightBlendMode = 0;

matrices.mProjectionMatrix.ortho(0, (float)width, (float)height, 0, -1.0f, 1.0f);
matrices.CalcDependencies();
Expand Down
11 changes: 11 additions & 0 deletions source/common/rendering/hwrenderer/data/hw_viewpointuniforms.h
Expand Up @@ -4,6 +4,15 @@

struct HWDrawInfo;

enum class ELightBlendMode : uint8_t
{
CLAMP = 0,
CLAMP_COLOR = 1,
NOCLAMP = 2,

DEFAULT = CLAMP,
};

struct HWViewpointUniforms
{
VSMatrix mProjectionMatrix;
Expand All @@ -19,6 +28,8 @@ struct HWViewpointUniforms
float mClipHeightDirection = 0.f;
int mShadowmapFilter = 1;

int mLightBlendMode = 0;

void CalcDependencies()
{
mNormalViewMatrix.computeNormalMatrix(mViewMatrix);
Expand Down
Expand Up @@ -570,7 +570,8 @@ void PPColormap::Render(PPRenderState *renderstate, int fixedcm, float flash)

void PPTonemap::UpdateTextures()
{
if (gl_tonemap == Palette && !PaletteTexture.Data)
// level.info->tonemap cannot be ETonemapMode::Palette, so it's fine to only check gl_tonemap here
if (ETonemapMode((int)gl_tonemap) == ETonemapMode::Palette && !PaletteTexture.Data)
{
std::shared_ptr<void> data(new uint32_t[512 * 512], [](void *p) { delete[](uint32_t*)p; });

Expand Down Expand Up @@ -598,22 +599,24 @@ void PPTonemap::UpdateTextures()

void PPTonemap::Render(PPRenderState *renderstate)
{
if (gl_tonemap == 0)
ETonemapMode current_tonemap = (level_tonemap != ETonemapMode::None) ? level_tonemap : ETonemapMode((int)gl_tonemap);

if (current_tonemap == ETonemapMode::None)
{
return;
}

UpdateTextures();

PPShader *shader = nullptr;
switch (gl_tonemap)
switch (current_tonemap)
{
default:
case Linear: shader = &LinearShader; break;
case Reinhard: shader = &ReinhardShader; break;
case HejlDawson: shader = &HejlDawsonShader; break;
case Uncharted2: shader = &Uncharted2Shader; break;
case Palette: shader = &PaletteShader; break;
case ETonemapMode::Linear: shader = &LinearShader; break;
case ETonemapMode::Reinhard: shader = &ReinhardShader; break;
case ETonemapMode::HejlDawson: shader = &HejlDawsonShader; break;
case ETonemapMode::Uncharted2: shader = &Uncharted2Shader; break;
case ETonemapMode::Palette: shader = &PaletteShader; break;
}

renderstate->PushGroup("tonemap");
Expand All @@ -622,7 +625,7 @@ void PPTonemap::Render(PPRenderState *renderstate)
renderstate->Shader = shader;
renderstate->Viewport = screen->mScreenViewport;
renderstate->SetInputCurrent(0);
if (gl_tonemap == Palette)
if (current_tonemap == ETonemapMode::Palette)
renderstate->SetInputTexture(1, &PaletteTexture);
renderstate->SetOutputNext();
renderstate->SetNoBlend();
Expand Down
29 changes: 18 additions & 11 deletions source/common/rendering/hwrenderer/postprocessing/hw_postprocess.h
Expand Up @@ -13,6 +13,19 @@ typedef IntRect PPViewport;
class PPTexture;
class PPShader;

enum class ETonemapMode : uint8_t
{
None,
Uncharted2,
HejlDawson,
Reinhard,
Linear,
Palette,
NumTonemapModes
};



enum class PPFilterMode { Nearest, Linear };
enum class PPWrapMode { Clamp, Repeat };
enum class PPTextureType { CurrentPipelineTexture, NextPipelineTexture, PPTexture, SceneColor, SceneFog, SceneNormal, SceneDepth, SwapChain, ShadowMap };
Expand Down Expand Up @@ -541,6 +554,7 @@ class PPColormap
class PPTonemap
{
public:
void SetTonemapMode(ETonemapMode tm) { level_tonemap = tm; }
void Render(PPRenderState *renderstate);
void ClearTonemapPalette() { PaletteTexture = {}; }

Expand All @@ -554,17 +568,7 @@ class PPTonemap
PPShader HejlDawsonShader = { "shaders/pp/tonemap.fp", "#define HEJLDAWSON\n", {} };
PPShader Uncharted2Shader = { "shaders/pp/tonemap.fp", "#define UNCHARTED2\n", {} };
PPShader PaletteShader = { "shaders/pp/tonemap.fp", "#define PALETTE\n", {} };

enum TonemapMode
{
None,
Uncharted2,
HejlDawson,
Reinhard,
Linear,
Palette,
NumTonemapModes
};
ETonemapMode level_tonemap = ETonemapMode::None;
};

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -840,8 +844,11 @@ class Postprocess
PPCustomShaders customShaders;


void SetTonemapMode(ETonemapMode tm) { tonemap.SetTonemapMode(tm); }
void Pass1(PPRenderState *state, int fixedcm, int sceneWidth, int sceneHeight);
void Pass2(PPRenderState* state, int fixedcm, float flash, int sceneWidth, int sceneHeight);
};


extern Postprocess hw_postprocess;

3 changes: 3 additions & 0 deletions source/common/rendering/vulkan/shaders/vk_shader.cpp
Expand Up @@ -175,6 +175,8 @@ static const char *shaderBindings = R"(
float uClipHeight;
float uClipHeightDirection;
int uShadowmapFilter;
int uLightBlendMode;
};
layout(set = 1, binding = 1, std140) uniform MatricesUBO {
Expand Down Expand Up @@ -244,6 +246,7 @@ static const char *shaderBindings = R"(
layout(set = 2, binding = 8) uniform sampler2D texture9;
layout(set = 2, binding = 9) uniform sampler2D texture10;
layout(set = 2, binding = 10) uniform sampler2D texture11;
layout(set = 2, binding = 11) uniform sampler2D texture12;
// This must match the PushConstants struct
layout(push_constant) uniform PushConstants
Expand Down
6 changes: 4 additions & 2 deletions source/common/scripting/core/maps.cpp
Expand Up @@ -438,10 +438,12 @@ template<typename I> void MapIteratorSetValue(I * self, expand_types_vm<typename
PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_I32_Str ); \
ACTION_RETURN_INT( MapIteratorGetKey(self) ); \
} \
DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_I32_Str , GetValue , MapIteratorGetValue< FMapIterator_I32_Str > ) \
DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_I32_Str , GetValue , MapIteratorGetValueString< FMapIterator_I32_Str > ) \
{ \
PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_I32_Str ); \
ACTION_RETURN_STRING( MapIteratorGetValue(self) ); \
FString out; \
MapIteratorGetValueString(self , out); \
ACTION_RETURN_STRING( out ); \
}

#define DEF_MAP_IT_S_S() \
Expand Down
11 changes: 7 additions & 4 deletions source/common/textures/hw_material.cpp
Expand Up @@ -133,12 +133,15 @@ FMaterial::FMaterial(FGameTexture * tx, int scaleflags)
if (index >= FIRST_USER_SHADER)
{
const UserShaderDesc& usershader = usershaders[index - FIRST_USER_SHADER];
if (tx->Layers && usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material
if (usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material
{
for (auto& texture : tx->Layers->CustomShaderTextures)
if (tx->Layers)
{
if (texture == nullptr) continue;
mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable.
for (auto& texture : tx->Layers->CustomShaderTextures)
{
if (texture == nullptr) continue;
mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable.
}
}
mShaderIndex = index;
}
Expand Down
70 changes: 54 additions & 16 deletions source/common/utility/palette.cpp
Expand Up @@ -418,19 +418,34 @@ void MakeRemap(uint32_t* BaseColors, const uint32_t* colors, uint8_t* remap, con
// color, so find a duplicate pair of palette entries, make one of them a
// duplicate of color 0, and remap every graphic so that it uses that entry
// instead of entry 0.
void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap)
void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap, const uint8_t* lastcolormap)
{
for (int i = 0; i < 256; i++) Remap[i] = i;
PalEntry color0 = BaseColors[0];
int i;


// First try for an exact match of color 0. Only Hexen does not have one.
for (i = 1; i < 256; ++i)
if (!lastcolormap)
{
if (BaseColors[i] == color0)
for (i = 1; i < 256; ++i)
{
Remap[0] = i;
break;
if (BaseColors[i] == color0)
{
Remap[0] = i;
break;
}
}
}
else
{
for (i = 1; i < 256; ++i)
{
if ((BaseColors[i] == color0) && (lastcolormap[i] == lastcolormap[0]))
{
Remap[0] = i;
break;
}
}
}

Expand All @@ -448,21 +463,44 @@ void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap)
sortcopy[i] = (BaseColors[i] & 0xffffff) | (i << 24);
}
qsort(sortcopy, 256, 4, sortforremap);
for (i = 255; i > 0; --i)
if (!lastcolormap)
{
if ((sortcopy[i] & 0xFFFFFF) == (sortcopy[i - 1] & 0xFFFFFF))
for (i = 255; i > 0; --i)
{
int new0 = sortcopy[i].a;
int dup = sortcopy[i - 1].a;
if (new0 > dup)
if ((sortcopy[i] & 0xFFFFFF) == (sortcopy[i - 1] & 0xFFFFFF))
{
// Make the lower-numbered entry a copy of color 0. (Just because.)
std::swap(new0, dup);
int new0 = sortcopy[i].a;
int dup = sortcopy[i - 1].a;
if (new0 > dup)
{
// Make the lower-numbered entry a copy of color 0. (Just because.)
std::swap(new0, dup);
}
Remap[0] = new0;
Remap[new0] = dup;
BaseColors[new0] = color0;
break;
}
}
}
else
{
for (i = 255; i > 0; --i)
{
if (((sortcopy[i] & 0xFFFFFF) == (sortcopy[i - 1] & 0xFFFFFF)) && (lastcolormap[sortcopy[i].a] == lastcolormap[sortcopy[i - 1].a]))
{
int new0 = sortcopy[i].a;
int dup = sortcopy[i - 1].a;
if (new0 > dup)
{
// Make the lower-numbered entry a copy of color 0. (Just because.)
std::swap(new0, dup);
}
Remap[0] = new0;
Remap[new0] = dup;
BaseColors[new0] = color0;
break;
}
Remap[0] = new0;
Remap[new0] = dup;
BaseColors[new0] = color0;
break;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/utility/palutil.h
Expand Up @@ -14,7 +14,7 @@ void DoBlending(const PalEntry* from, PalEntry* to, int count, int r, int g, int
// Given an array of colors, fills in remap with values to remap the
// passed array of colors to BaseColors. Used for loading palette downconversions of PNGs.
void MakeRemap(uint32_t* BaseColors, const uint32_t* colors, uint8_t* remap, const uint8_t* useful, int numcolors);
void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap);
void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap, const uint8_t* cmapdata = nullptr);

// Colorspace conversion RGB <-> HSV
void RGBtoHSV (float r, float g, float b, float *h, float *s, float *v);
Expand Down
1 change: 1 addition & 0 deletions source/core/rendering/scene/hw_drawinfo.cpp
Expand Up @@ -250,6 +250,7 @@ void HWDrawInfo::SetupView(FRenderState &state, float vx, float vy, float vz, bo
SetViewMatrix(vp.HWAngles, vx, vy, vz, mirror, planemirror);
SetCameraPos(vp.Pos);
VPUniforms.CalcDependencies();
VPUniforms.mLightBlendMode = 0;
vpIndex = screen->mViewpoints->SetViewpoint(state, &VPUniforms);
}

Expand Down

0 comments on commit fca0bdf

Please sign in to comment.