Skip to content

Commit

Permalink
Apply gamma to the screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Nov 3, 2020
1 parent 549788d commit d23e654
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
28 changes: 24 additions & 4 deletions VulkanDrv/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ void Renderer::ClearTextureCache()
TextureCache.clear();
}

void Renderer::CopyScreenToBuffer(int w, int h, void* data)
void Renderer::CopyScreenToBuffer(int w, int h, void* data, float gamma)
{
// Convert from rgba16f to rgba8 using the GPU:
// Convert from rgba16f to bgra8 using the GPU:
ImageBuilder imgbuilder;
imgbuilder.setFormat(VK_FORMAT_R8G8B8A8_UNORM);
imgbuilder.setFormat(VK_FORMAT_B8G8R8A8_UNORM);
imgbuilder.setUsage(VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
imgbuilder.setSize(w, h);
auto image = imgbuilder.create(Device);
Expand All @@ -360,6 +360,26 @@ void Renderer::CopyScreenToBuffer(int w, int h, void* data)
SubmitCommands(false);

uint8_t* pixels = (uint8_t*)staging->Map(0, w * h * 4);
memcpy(data, pixels, w * h * 4);
if (gamma != 1.0f)
{
float invGamma = 1.0f / gamma;

uint8_t gammatable[256];
for (int i = 0; i < 256; i++)
gammatable[i] = (int)clamp(std::round(std::pow(i / 255.0f, invGamma) * 255.0f), 0.0f, 255.0f);

uint8_t* dest = (uint8_t*)data;
for (int i = 0; i < w * h * 4; i += 4)
{
dest[i] = gammatable[pixels[i]];
dest[i + 1] = gammatable[pixels[i + 1]];
dest[i + 2] = gammatable[pixels[i + 2]];
dest[i + 3] = pixels[i + 3];
}
}
else
{
memcpy(data, pixels, w * h * 4);
}
staging->Unmap();
}
2 changes: 1 addition & 1 deletion VulkanDrv/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Renderer
VulkanCommandBuffer* GetDrawCommands();
void DeleteFrameObjects();

void CopyScreenToBuffer(int w, int h, void* data);
void CopyScreenToBuffer(int w, int h, void* data, float gamma);

static std::unique_ptr<VulkanShader> CreateVertexShader(VulkanDevice* device, const std::string& name, const std::string& defines = {});
static std::unique_ptr<VulkanShader> CreateFragmentShader(VulkanDevice* device, const std::string& name, const std::string& defines = {});
Expand Down
2 changes: 1 addition & 1 deletion VulkanDrv/UVulkanRenderDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ void UVulkanRenderDevice::GetStats(TCHAR* Result)
void UVulkanRenderDevice::ReadPixels(FColor* Pixels)
{
guard(UVulkanRenderDevice::GetStats);
renderer->CopyScreenToBuffer(Viewport->SizeX, Viewport->SizeY, Pixels);
renderer->CopyScreenToBuffer(Viewport->SizeX, Viewport->SizeY, Pixels, 2.5f * Viewport->GetOuterUClient()->Brightness);
unguard;
}

Expand Down

0 comments on commit d23e654

Please sign in to comment.