Skip to content

Commit

Permalink
-got rid of shared_ptr in postprocessing system
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Jun 7, 2022
1 parent c7798d5 commit 40872a2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/common/rendering/gl/gl_renderbuffers.cpp
Expand Up @@ -807,8 +807,8 @@ PPGLTextureBackend *GLPPRenderState::GetGLTexture(PPTexture *texture)
case PixelFormat::Rgba16_snorm: glformat = GL_RGBA16_SNORM; break;
}

if (texture->Data)
backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height, texture->Data.get());
if (texture->Data.Size())
backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height, texture->Data.Data());
else
backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height);

Expand Down Expand Up @@ -990,4 +990,4 @@ int FGLRenderBuffers::NextEye(int eyeCount)
return mCurrentEye;
}

} // namespace OpenGLRenderer
} // namespace OpenGLRenderer
24 changes: 7 additions & 17 deletions src/common/rendering/hwrenderer/postprocessing/hw_postprocess.cpp
Expand Up @@ -570,11 +570,9 @@ void PPColormap::Render(PPRenderState *renderstate, int fixedcm, float flash)

void PPTonemap::UpdateTextures()
{
if (gl_tonemap == Palette && !PaletteTexture.Data)
if (gl_tonemap == Palette && !PaletteTexture.Data.Size())
{
std::shared_ptr<void> data(new uint32_t[512 * 512], [](void *p) { delete[](uint32_t*)p; });

uint8_t *lut = (uint8_t *)data.get();
uint8_t *lut = PaletteTexture.SetBuffer(512, 512, PixelFormat::Rgba8);
for (int r = 0; r < 64; r++)
{
for (int g = 0; g < 64; g++)
Expand All @@ -591,8 +589,6 @@ void PPTonemap::UpdateTextures()
}
}
}

PaletteTexture = { 512, 512, PixelFormat::Rgba8, data };
}
}

Expand Down Expand Up @@ -642,8 +638,7 @@ PPAmbientOcclusion::PPAmbientOcclusion()
std::uniform_real_distribution<double> distribution(0.0, 1.0);
for (int quality = 0; quality < NumAmbientRandomTextures; quality++)
{
std::shared_ptr<void> data(new int16_t[16 * 4], [](void *p) { delete[](int16_t*)p; });
int16_t *randomValues = (int16_t *)data.get();
int16_t *randomValues = (int16_t *)AmbientRandomTexture[quality].SetBuffer(4, 4, PixelFormat::Rgba16_snorm);

for (int i = 0; i < 16; i++)
{
Expand All @@ -658,8 +653,6 @@ PPAmbientOcclusion::PPAmbientOcclusion()
randomValues[i * 4 + 2] = (int16_t)clamp(z * 32767.0, -32768.0, 32767.0);
randomValues[i * 4 + 3] = (int16_t)clamp(w * 32767.0, -32768.0, 32767.0);
}

AmbientRandomTexture[quality] = { 4, 4, PixelFormat::Rgba16_snorm, data };
}
}

Expand Down Expand Up @@ -864,9 +857,8 @@ PPPresent::PPPresent()
.8515625, .6015625, .9765625, .7265625, .8671875, .6171875, .9921875, .7421875,
};

std::shared_ptr<void> pixels(new float[64], [](void *p) { delete[](float*)p; });
memcpy(pixels.get(), data, 64 * sizeof(float));
Dither = { 8, 8, PixelFormat::R32f, pixels };
auto p = Dither.SetBuffer(8, 8, PixelFormat::R32f);
memcpy(p, data, 64 * sizeof(float));
}

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1023,11 +1015,10 @@ void PPCustomShaderInstance::SetTextures(PPRenderState *renderstate)
if (!pptex)
{
auto buffer = tex->CreateTexBuffer(0);

std::shared_ptr<void> data(new uint32_t[buffer.mWidth * buffer.mHeight], [](void *p) { delete[](uint32_t*)p; });
pptex = std::make_unique<PPTexture>();
auto pixels = pptex->SetBuffer(buffer.mWidth, buffer.mHeight, PixelFormat::Rgba8);

int count = buffer.mWidth * buffer.mHeight;
uint8_t *pixels = (uint8_t *)data.get();
for (int i = 0; i < count; i++)
{
int pos = i << 2;
Expand All @@ -1037,7 +1028,6 @@ void PPCustomShaderInstance::SetTextures(PPRenderState *renderstate)
pixels[pos + 3] = buffer.mBuffer[pos + 3];
}

pptex = std::make_unique<PPTexture>(buffer.mWidth, buffer.mHeight, PixelFormat::Rgba8, data);
}

renderstate->SetInputTexture(textureIndex, pptex.get(), PPFilterMode::Linear, PPWrapMode::Repeat);
Expand Down
34 changes: 29 additions & 5 deletions src/common/rendering/hwrenderer/postprocessing/hw_postprocess.h
Expand Up @@ -277,17 +277,41 @@ class PPTextureBackend
class PPTexture : public PPResource
{
public:
PPTexture() = default;
PPTexture(int width, int height, PixelFormat format, std::shared_ptr<void> data = {}) : Width(width), Height(height), Format(format), Data(data) { }

void ResetBackend() override { Backend.reset(); }

int Width;
int Height;
PixelFormat Format;
std::shared_ptr<void> Data;
TArray<uint8_t> Data;

std::unique_ptr<PPTextureBackend> Backend;

explicit PPTexture() = default;

PPTexture(int w, int h, PixelFormat f)
{
Width = w;
Height = h;
Format = f;
}

void Clear()
{
Width = Height = 0;
Format = PixelFormat::Rgba8;
Data.Reset();
}

uint8_t* SetBuffer(int w, int h, PixelFormat f)
{
Width = w;
Height = h;
Format = f;
unsigned size = f == PixelFormat::Rgba16f || f == PixelFormat::Rgba16_snorm? 8 : 4;
Data.Resize(w * h * size);
return Data.Data();
}
};

class PPShaderBackend
Expand Down Expand Up @@ -497,7 +521,7 @@ class PPCameraExposure
public:
void Render(PPRenderState *renderstate, int sceneWidth, int sceneHeight);

PPTexture CameraTexture = { 1, 1, PixelFormat::R32f };
PPTexture CameraTexture;// = { 1, 1, PixelFormat::R32f };

private:
void UpdateTextures(int width, int height);
Expand Down Expand Up @@ -542,7 +566,7 @@ class PPTonemap
{
public:
void Render(PPRenderState *renderstate);
void ClearTonemapPalette() { PaletteTexture = {}; }
void ClearTonemapPalette() { PaletteTexture.Clear(); }

private:
void UpdateTextures();
Expand Down
6 changes: 3 additions & 3 deletions src/common/rendering/vulkan/renderer/vk_postprocess.cpp
Expand Up @@ -364,7 +364,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
ImageBuilder imgbuilder;
imgbuilder.setFormat(format);
imgbuilder.setSize(texture->Width, texture->Height);
if (texture->Data)
if (texture->Data.Size())
imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
else
imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Expand All @@ -379,7 +379,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
TexImage.View = viewbuilder.create(fb->device);
TexImage.View->SetDebugName("VkPPTextureView");

if (texture->Data)
if (texture->Data.Size())
{
size_t totalsize = texture->Width * texture->Height * pixelsize;
BufferBuilder stagingbuilder;
Expand All @@ -393,7 +393,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
barrier0.execute(fb->GetTransferCommands());

void *data = Staging->Map(0, totalsize);
memcpy(data, texture->Data.get(), totalsize);
memcpy(data, texture->Data.Data(), totalsize);
Staging->Unmap();

VkBufferImageCopy region = {};
Expand Down

0 comments on commit 40872a2

Please sign in to comment.