-got rid of shared_ptr in postprocessing system

This commit is contained in:
Christoph Oelckers 2022-06-07 13:59:00 +02:00
parent c7798d5503
commit 40872a2b21
4 changed files with 42 additions and 28 deletions

View file

@ -807,8 +807,8 @@ PPGLTextureBackend *GLPPRenderState::GetGLTexture(PPTexture *texture)
case PixelFormat::Rgba16_snorm: glformat = GL_RGBA16_SNORM; break; case PixelFormat::Rgba16_snorm: glformat = GL_RGBA16_SNORM; break;
} }
if (texture->Data) if (texture->Data.Size())
backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height, texture->Data.get()); backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height, texture->Data.Data());
else else
backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height); backend->Tex = buffers->Create2DTexture("PPTexture", glformat, texture->Width, texture->Height);

View file

@ -570,11 +570,9 @@ void PPColormap::Render(PPRenderState *renderstate, int fixedcm, float flash)
void PPTonemap::UpdateTextures() 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 = PaletteTexture.SetBuffer(512, 512, PixelFormat::Rgba8);
uint8_t *lut = (uint8_t *)data.get();
for (int r = 0; r < 64; r++) for (int r = 0; r < 64; r++)
{ {
for (int g = 0; g < 64; g++) for (int g = 0; g < 64; g++)
@ -591,8 +589,6 @@ void PPTonemap::UpdateTextures()
} }
} }
} }
PaletteTexture = { 512, 512, PixelFormat::Rgba8, data };
} }
} }
@ -642,8 +638,7 @@ PPAmbientOcclusion::PPAmbientOcclusion()
std::uniform_real_distribution<double> distribution(0.0, 1.0); std::uniform_real_distribution<double> distribution(0.0, 1.0);
for (int quality = 0; quality < NumAmbientRandomTextures; quality++) 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 *)AmbientRandomTexture[quality].SetBuffer(4, 4, PixelFormat::Rgba16_snorm);
int16_t *randomValues = (int16_t *)data.get();
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
{ {
@ -658,8 +653,6 @@ PPAmbientOcclusion::PPAmbientOcclusion()
randomValues[i * 4 + 2] = (int16_t)clamp(z * 32767.0, -32768.0, 32767.0); 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); randomValues[i * 4 + 3] = (int16_t)clamp(w * 32767.0, -32768.0, 32767.0);
} }
AmbientRandomTexture[quality] = { 4, 4, PixelFormat::Rgba16_snorm, data };
} }
} }
@ -864,9 +857,8 @@ PPPresent::PPPresent()
.8515625, .6015625, .9765625, .7265625, .8671875, .6171875, .9921875, .7421875, .8515625, .6015625, .9765625, .7265625, .8671875, .6171875, .9921875, .7421875,
}; };
std::shared_ptr<void> pixels(new float[64], [](void *p) { delete[](float*)p; }); auto p = Dither.SetBuffer(8, 8, PixelFormat::R32f);
memcpy(pixels.get(), data, 64 * sizeof(float)); memcpy(p, data, 64 * sizeof(float));
Dither = { 8, 8, PixelFormat::R32f, pixels };
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -1023,11 +1015,10 @@ void PPCustomShaderInstance::SetTextures(PPRenderState *renderstate)
if (!pptex) if (!pptex)
{ {
auto buffer = tex->CreateTexBuffer(0); auto buffer = tex->CreateTexBuffer(0);
pptex = std::make_unique<PPTexture>();
std::shared_ptr<void> data(new uint32_t[buffer.mWidth * buffer.mHeight], [](void *p) { delete[](uint32_t*)p; }); auto pixels = pptex->SetBuffer(buffer.mWidth, buffer.mHeight, PixelFormat::Rgba8);
int count = buffer.mWidth * buffer.mHeight; int count = buffer.mWidth * buffer.mHeight;
uint8_t *pixels = (uint8_t *)data.get();
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
int pos = i << 2; int pos = i << 2;
@ -1037,7 +1028,6 @@ void PPCustomShaderInstance::SetTextures(PPRenderState *renderstate)
pixels[pos + 3] = buffer.mBuffer[pos + 3]; 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); renderstate->SetInputTexture(textureIndex, pptex.get(), PPFilterMode::Linear, PPWrapMode::Repeat);

View file

@ -277,17 +277,41 @@ public:
class PPTexture : public PPResource class PPTexture : public PPResource
{ {
public: 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(); } void ResetBackend() override { Backend.reset(); }
int Width; int Width;
int Height; int Height;
PixelFormat Format; PixelFormat Format;
std::shared_ptr<void> Data; TArray<uint8_t> Data;
std::unique_ptr<PPTextureBackend> Backend; 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 class PPShaderBackend
@ -497,7 +521,7 @@ class PPCameraExposure
public: public:
void Render(PPRenderState *renderstate, int sceneWidth, int sceneHeight); void Render(PPRenderState *renderstate, int sceneWidth, int sceneHeight);
PPTexture CameraTexture = { 1, 1, PixelFormat::R32f }; PPTexture CameraTexture;// = { 1, 1, PixelFormat::R32f };
private: private:
void UpdateTextures(int width, int height); void UpdateTextures(int width, int height);
@ -542,7 +566,7 @@ class PPTonemap
{ {
public: public:
void Render(PPRenderState *renderstate); void Render(PPRenderState *renderstate);
void ClearTonemapPalette() { PaletteTexture = {}; } void ClearTonemapPalette() { PaletteTexture.Clear(); }
private: private:
void UpdateTextures(); void UpdateTextures();

View file

@ -364,7 +364,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
ImageBuilder imgbuilder; ImageBuilder imgbuilder;
imgbuilder.setFormat(format); imgbuilder.setFormat(format);
imgbuilder.setSize(texture->Width, texture->Height); 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); imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
else else
imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
@ -379,7 +379,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
TexImage.View = viewbuilder.create(fb->device); TexImage.View = viewbuilder.create(fb->device);
TexImage.View->SetDebugName("VkPPTextureView"); TexImage.View->SetDebugName("VkPPTextureView");
if (texture->Data) if (texture->Data.Size())
{ {
size_t totalsize = texture->Width * texture->Height * pixelsize; size_t totalsize = texture->Width * texture->Height * pixelsize;
BufferBuilder stagingbuilder; BufferBuilder stagingbuilder;
@ -393,7 +393,7 @@ VkPPTexture::VkPPTexture(PPTexture *texture)
barrier0.execute(fb->GetTransferCommands()); barrier0.execute(fb->GetTransferCommands());
void *data = Staging->Map(0, totalsize); void *data = Staging->Map(0, totalsize);
memcpy(data, texture->Data.get(), totalsize); memcpy(data, texture->Data.Data(), totalsize);
Staging->Unmap(); Staging->Unmap();
VkBufferImageCopy region = {}; VkBufferImageCopy region = {};