Skip to content

Commit

Permalink
bugfix: general post processing fixes
Browse files Browse the repository at this point in the history
- correctly support BGR --> RGB
- fix color conv
- fix memory leak with tweaks to TextureManager
- move queue events to a seperate special handler

Signed-off-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
pollend committed Apr 3, 2023
1 parent bb0d8c4 commit 0d1a4a5
Show file tree
Hide file tree
Showing 45 changed files with 609 additions and 671 deletions.
10 changes: 3 additions & 7 deletions HPL2/core/include/graphics/ImmediateDrawBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace hpl
{

class ImmediateDrawBatch;

class ImmediateDrawBatch {
class ImmediateDrawBatch {
public:
struct DebugDrawOptions {
public:
Expand All @@ -24,8 +22,7 @@ class ImmediateDrawBatch;
inline GraphicsContext& GetContext() const { return m_context; }

ImmediateDrawBatch(GraphicsContext& context, RenderTarget& target, const cMatrixf& view, const cMatrixf& projection);



// takes 3 points and the other 1 is calculated
[[deprecated("Use DrawQuad with Eigen")]]
void DrawQuad(const cVector3f& v1, const cVector3f& v2, const cVector3f& v3, const cVector3f& v4, const cVector2f& uv0,const cVector2f& uv1, hpl::Image* image , const cColor& aTint, const DebugDrawOptions& options = DebugDrawOptions());
Expand Down Expand Up @@ -111,12 +108,11 @@ class ImmediateDrawBatch;
};
// Orthgraphic projection
std::vector<Line2DSegmentRequest> m_line2DSegments;

// Perspective projection
std::vector<UVQuadRequest> m_uvQuads;
std::vector<ColorQuadRequest> m_colorQuads;
std::vector<LineSegmentRequest> m_lineSegments;
std::vector<ColorTriRequest> m_colorTriangles;

cMatrixf m_view;
cMatrixf m_projection;

Expand Down
10 changes: 5 additions & 5 deletions HPL2/core/include/graphics/PostEffect_ColorConvTex.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ namespace hpl {
public:
virtual void RenderEffect(cPostEffectComposite& compositor, cViewport& viewport, GraphicsContext& context, Image& input, RenderTarget& target) override;
cPostEffect_ColorConvTex(cGraphics *apGraphics, cResources *apResources, iPostEffectType *apType);
~cPostEffect_ColorConvTex();
virtual ~cPostEffect_ColorConvTex();

private:
void OnSetParams();
iPostEffectParams *GetTypeSpecificParams() { return &mParams; }
virtual void OnSetParams() override;
virtual iPostEffectParams *GetTypeSpecificParams() override { return &mParams; }

Image* mpColorConvTex;
Image* mpColorConvTex = nullptr;

cPostEffectType_ColorConvTex* mpSpecificType;
cPostEffectType_ColorConvTex* mpSpecificType = nullptr;

cPostEffectParams_ColorConvTex mParams;
};
Expand Down
16 changes: 10 additions & 6 deletions HPL2/core/include/graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace hpl {
class iRenderableContainerNode;
class cVisibleRCNodeTracker;
class RenderCallbackMessage;
class iRenderer;

namespace rendering::detail {
eShadowMapResolution GetShadowMapResolution(eShadowMapResolution aWanted, eShadowMapResolution aMax);
Expand All @@ -67,6 +68,14 @@ namespace hpl {
* @param occlusionPlanes The planes to check against.
*/
bool IsObjectVisible(iRenderable *apObject, tRenderableFlag alNeededFlags, std::span<cPlanef> occlusionPlanes);

void RenderableMaterialIter(
iRenderer* renderer,
std::span<iRenderable*> iter,
cViewport& viewport,
eMaterialRenderMode mode,
std::function<void(iRenderable* obj, GraphicsContext::LayoutStream&, GraphicsContext::ShaderProgram&)> handler);

}

class cNodeOcclusionPair
Expand Down Expand Up @@ -110,7 +119,6 @@ namespace hpl {
};


class iRenderer;
class cRenderSettings
{
public:
Expand Down Expand Up @@ -325,8 +333,6 @@ namespace hpl {

cShadowMapData* GetShadowMapData(eShadowMapResolution aResolution, iLight *apLight);
bool ShadowMapNeedsUpdate(iLight *apLight, cShadowMapData *apShadowData);
void DestroyShadowMaps();


void RenderZObject(GraphicsContext& context, iRenderable *apObject, cFrustum *apCustomFrustum);

Expand Down Expand Up @@ -359,9 +365,7 @@ namespace hpl {
void GetShadowCasters(iRenderableContainer *apContainer, tRenderableVec& avObjectVec, cFrustum *apLightFrustum);
bool SetupShadowMapRendering(iLight *apLight);

bool RenderShadowCasterCHC(iRenderable *apObject);
void RenderShadowCaster(iRenderable *apObject, cFrustum *apLightFrustum);


/**
* Only depth is needed for framebuffer. All objects needs to be added to renderlist!
*/
Expand Down
2 changes: 0 additions & 2 deletions HPL2/core/include/graphics/RendererDeferred.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ namespace hpl {
// takes the contents of the gbuffer and renders the lights
void RenderLightPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderDiffusePass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderDecalPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderIlluminationPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderFogPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderFullScreenFogPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
void RenderEdgeSmoothPass(GraphicsContext& context, cViewport& viewport, RenderTarget& rt);
Expand Down
15 changes: 9 additions & 6 deletions HPL2/core/include/scene/Viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ namespace hpl {
cFrustum* m_frustum;
GraphicsContext* m_context;
RenderTarget* m_outputTarget;
cViewport* m_viewport;
cRenderSettings* m_renderSettings;
ImmediateDrawBatch* m_immediateDrawBatch;
};
struct PostSolidDrawPayload : public DrawPayloadCommon {
struct PostSolidDrawPacket : public DrawPayloadCommon {
};
struct PostTranslucenceDrawPayload : public DrawPayloadCommon {
struct PostTranslucenceDrawPacket : public DrawPayloadCommon {
};
using PostSolidDraw = hpl::Event<PostSolidDrawPayload&>;
using PostTranslucenceDraw = hpl::Event<PostTranslucenceDrawPayload&>;
using PostSolidDraw = hpl::Event<PostSolidDrawPacket&>;
using PostTranslucenceDraw = hpl::Event<PostTranslucenceDrawPacket&>;

cViewport(cScene* apScene);

Expand Down Expand Up @@ -159,8 +162,8 @@ namespace hpl {
inline void ConnectDraw(PostSolidDraw::Handler& handler) { handler.Connect(m_postSolidDraw); }
inline void ConnectDraw(PostTranslucenceDraw::Handler& handler) { handler.Connect(m_postTranslucenceDraw); }

inline void SignalDraw(PostSolidDrawPayload& payload) { m_postSolidDraw.Signal(payload);}
inline void SignalDraw(PostTranslucenceDrawPayload& payload) { m_postTranslucenceDraw.Signal(payload);}
inline void SignalDraw(PostSolidDrawPacket& payload) { m_postSolidDraw.Signal(payload);}
inline void SignalDraw(PostTranslucenceDrawPacket& payload) { m_postTranslucenceDraw.Signal(payload);}


private:
Expand Down
1 change: 0 additions & 1 deletion HPL2/core/resource/fs_posteffect_color_conv.sc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ uniform vec4 u_params;
void main()
{
vec3 diffuseColor = texture2D(s_diffuseMap, v_texcoord0).xyz;

vec3 outputColor = vec3(texture2D(s_convMap, vec2(diffuseColor.x, 0)).x,
texture2D(s_convMap, vec2(diffuseColor.y, 0)).y,
texture2D(s_convMap, vec2(diffuseColor.z, 0)).z);
Expand Down
5 changes: 2 additions & 3 deletions HPL2/core/sources/graphics/GraphicsContex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include "graphics/Enum.h"
#include "graphics/ShaderUtil.h"
#include <graphics/GraphicsTypes.h>
#include "graphics/Layouts.h"
#include "graphics/GraphicsTypes.h"

#include "bx/math.h"
#include <bgfx/bgfx.h>
Expand All @@ -26,7 +27,6 @@
#include <cstdint>
#include <variant>

#include "graphics/Layouts.h"

namespace hpl {

Expand Down Expand Up @@ -70,7 +70,6 @@ namespace hpl {

void GraphicsContext::Frame() {
m_current = 0;
// m_windowEvent.Process();
bgfx::frame();
}

Expand Down
34 changes: 26 additions & 8 deletions HPL2/core/sources/graphics/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,12 @@ namespace hpl
return bgfx::TextureFormat::R32F;
case ePixelFormat_LuminanceAlpha32:
return bgfx::TextureFormat::RG32F;
case ePixelFormat_RGB32:
break;
case ePixelFormat_RGBA32:
return bgfx::TextureFormat::RGBA32F;
case ePixelFormat_RGB16:
return bgfx::TextureFormat::BC6H;
case ePixelFormat_BGR:
return bgfx::TextureFormat::RGB8;
return bgfx::TextureFormat::RGB8; // this is not supported by bgfx so we swap it under Image::InitializeFromBitmap
default:
BX_ASSERT(false, "Unsupported texture format: %d", format)
break;
Expand Down Expand Up @@ -378,6 +376,21 @@ namespace hpl
}

void Image::InitializeFromBitmap(Image& image, cBitmap& bitmap, const ImageDescriptor& desc) {

auto copyDataToChunk = [&](unsigned char* m_begin, unsigned char* m_end, unsigned char* dest) {

// BGR is not supported by bgfx, so we need to convert it to RGB
if(bitmap.GetPixelFormat() == ePixelFormat_BGR) {
for(auto* src = m_begin; src < m_end; src += 3) {
*(dest++) = src[2];
*(dest++) = src[1];
*(dest++) = src[0];
}
} else {
std::copy(m_begin, m_end, dest);
}
};

if(desc.m_isCubeMap) {
BX_ASSERT(bitmap.GetNumOfImages() == 6, "Cube map must have 6 images");

Expand All @@ -398,14 +411,16 @@ namespace hpl
for(auto imageIdx = 0; imageIdx < bitmap.GetNumOfImages(); ++imageIdx) {
for(auto mipIndex = 0; mipIndex < bitmap.GetNumOfMipMaps(); ++mipIndex) {
auto data = bitmap.GetData(imageIdx, mipIndex);
std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
// std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
copyDataToChunk(data->mpData, data->mpData + data->mlSize, memory->data + offset);
offset += data->mlSize;
}
}
} else {
for(auto i = 0; i < bitmap.GetNumOfImages(); ++i) {
auto data = bitmap.GetData(i, 0);
std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
// std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
copyDataToChunk(data->mpData, data->mpData + data->mlSize, memory->data + offset);
offset += data->mlSize;
}
}
Expand All @@ -425,15 +440,18 @@ namespace hpl
size_t offset = 0;
for(auto i = 0; i < bitmap.GetNumOfMipMaps(); ++i) {
auto data = bitmap.GetData(0, i);
std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
// std::copy(data->mpData, data->mpData + data->mlSize, memory->data + offset);
copyDataToChunk(data->mpData, data->mpData + data->mlSize, memory->data + offset);
offset += data->mlSize;
}
image.Initialize(desc, memory);
return;
}

auto data = bitmap.GetData(0, 0);
image.Initialize(desc, bgfx::copy(data->mpData, data->mlSize));
auto* memory = bgfx::alloc(data->mlSize);
copyDataToChunk(data->mpData, data->mpData + data->mlSize, memory->data);
image.Initialize(desc, memory);
}

bgfx::TextureHandle Image::GetHandle() const
Expand Down
16 changes: 11 additions & 5 deletions HPL2/core/sources/graphics/ImmediateDrawBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ namespace hpl {


void ImmediateDrawBatch::flush() {
if (m_lineSegments.empty() && m_colorQuads.empty() && m_uvQuads.empty() && m_colorTriangles.empty() && m_line2DSegments.empty()) {
if (m_line2DSegments.empty() &&
m_uvQuads.empty() &&
m_colorQuads.empty() &&
m_lineSegments.empty() &&
m_colorTriangles.empty()) {
return;
}

Expand Down Expand Up @@ -719,9 +723,11 @@ namespace hpl {
m_context.Submit(m_perspectiveView, request);
}
}
m_colorTriangles.clear();
m_lineSegments.clear();
m_colorQuads.clear();
m_uvQuads.clear();
m_line2DSegments.clear();
m_uvQuads.clear();
m_colorQuads.clear();
m_lineSegments.clear();
m_colorTriangles.clear();

}
} // namespace hpl
21 changes: 15 additions & 6 deletions HPL2/core/sources/graphics/PostEffect_ColorConvTex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace hpl
: iPostEffectType("ColorConvTex", apGraphics, apResources)
{
m_colorConv = hpl::loadProgram("vs_post_effect", "fs_posteffect_color_conv");
m_u_param = bgfx::createUniform("u_param", bgfx::UniformType::Vec4);
m_u_param = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
m_u_colorConvTex = bgfx::createUniform("s_convMap", bgfx::UniformType::Sampler);
m_u_diffuseTex = bgfx::createUniform("s_diffuseMap", bgfx::UniformType::Sampler);
}
Expand Down Expand Up @@ -79,14 +79,17 @@ namespace hpl

void cPostEffect_ColorConvTex::OnSetParams()
{
if (mParams.msTextureFile == "")
if (mParams.msTextureFile == "") {
return;
}

if (mpColorConvTex)
{
if (mpColorConvTex) {
mpResources->GetTextureManager()->Destroy(mpColorConvTex);
}
mpColorConvTex = mpResources->GetTextureManager()->Create1DImage(mParams.msTextureFile, false);
cTextureManager::ImageOptions options;
options.m_UWrap = WrapMode::Clamp;
options.m_VWrap = WrapMode::Clamp;
mpColorConvTex = mpResources->GetTextureManager()->Create1DImage(mParams.msTextureFile, false, eTextureUsage_Normal, 0, options);
}

void cPostEffect_ColorConvTex::RenderEffect(cPostEffectComposite& compositor, cViewport& viewport, GraphicsContext& context, Image& input, RenderTarget& target)
Expand All @@ -105,14 +108,20 @@ namespace hpl
GraphicsContext::ShaderProgram shaderProgram;
// shaderProgram.m_projection = projMtx;
shaderProgram.m_handle = mpSpecificType->m_colorConv;

struct
{
float u_alphaFade;
float pad;
float pad1;
float pad2;
} uniform = { cMath::Min(1.0f, cMath::Max(mParams.mfFadeAlpha, 0.0f)), 0, 0, 0 };
} uniform = {
cMath::Clamp(mParams.mfFadeAlpha, 0.0f, 1.0f),
0,
0,
0 };
shaderProgram.m_uniforms.push_back({ mpSpecificType->m_u_param, &uniform, 1 });

shaderProgram.m_textures.push_back({ mpSpecificType->m_u_colorConvTex, mpColorConvTex->GetHandle(), 0 });
shaderProgram.m_textures.push_back({ mpSpecificType->m_u_diffuseTex, input.GetHandle(), 1 });

Expand Down
4 changes: 2 additions & 2 deletions HPL2/core/sources/graphics/PostEffect_ImageTrail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace hpl {
cPostEffect_ImageTrail::cPostEffect_ImageTrail(cGraphics* apGraphics, cResources* apResources, iPostEffectType* apType)
: iPostEffect(apGraphics, apResources, apType) {
m_boundImageTrailData = UniqueViewportData<ImageTrailData>([&](cViewport& viewport) {
auto desc = ImageDescriptor::CreateTexture2D(viewport.GetSize().x / 4.0f, viewport.GetSize().y/ 4.0f , false, bgfx::TextureFormat::Enum::RGBA8);
auto desc = ImageDescriptor::CreateTexture2D(viewport.GetSize().x, viewport.GetSize().y , false, bgfx::TextureFormat::Enum::RGBA8);
desc.m_configuration.m_rt = RTType::RT_Write;
auto image = std::make_shared<Image>();
image->Initialize(desc);
Expand Down Expand Up @@ -103,7 +103,7 @@ namespace hpl {
void cPostEffect_ImageTrail::RenderEffect(
cPostEffectComposite& compositor, cViewport& viewport, GraphicsContext& context, Image& input, RenderTarget& target) {
cVector2l vRenderTargetSize = viewport.GetSize();
auto& imageTrailData = m_boundImageTrailData.resolve(viewport);
auto& imageTrailData = m_boundImageTrailData.resolve(viewport);


GraphicsContext::LayoutStream layoutStream;
Expand Down
Loading

0 comments on commit 0d1a4a5

Please sign in to comment.