2020-01-03 04:52:01 +00:00
|
|
|
/*
|
|
|
|
** Vulkan backend
|
|
|
|
** Copyright (c) 2016-2020 Magnus Norddahl
|
|
|
|
**
|
|
|
|
** This software is provided 'as-is', without any express or implied
|
|
|
|
** warranty. In no event will the authors be held liable for any damages
|
|
|
|
** arising from the use of this software.
|
|
|
|
**
|
|
|
|
** Permission is granted to anyone to use this software for any purpose,
|
|
|
|
** including commercial applications, and to alter it and redistribute it
|
|
|
|
** freely, subject to the following restrictions:
|
|
|
|
**
|
|
|
|
** 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
** claim that you wrote the original software. If you use this software
|
|
|
|
** in a product, an acknowledgment in the product documentation would be
|
|
|
|
** appreciated but is not required.
|
|
|
|
** 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
** misrepresented as being the original software.
|
|
|
|
** 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
**
|
|
|
|
*/
|
2019-02-20 20:21:57 +00:00
|
|
|
|
|
|
|
#include "volk/volk.h"
|
|
|
|
|
2021-07-01 10:19:18 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
#include "v_video.h"
|
|
|
|
#include "m_png.h"
|
2021-11-12 08:36:19 +00:00
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
#include "r_videoscale.h"
|
2019-03-01 18:01:06 +00:00
|
|
|
#include "i_time.h"
|
2020-04-11 17:43:17 +00:00
|
|
|
#include "v_text.h"
|
2020-04-26 12:44:19 +00:00
|
|
|
#include "version.h"
|
2020-04-28 15:22:17 +00:00
|
|
|
#include "v_draw.h"
|
2019-02-20 20:21:57 +00:00
|
|
|
|
2020-04-29 15:19:17 +00:00
|
|
|
#include "hw_clock.h"
|
2020-04-26 08:26:29 +00:00
|
|
|
#include "hw_vrmodes.h"
|
2020-04-26 09:38:38 +00:00
|
|
|
#include "hw_cvars.h"
|
2020-04-28 22:21:30 +00:00
|
|
|
#include "hw_skydome.h"
|
2019-02-21 11:31:14 +00:00
|
|
|
#include "hwrenderer/data/hw_viewpointbuffer.h"
|
2020-04-26 10:30:36 +00:00
|
|
|
#include "flatvertices.h"
|
2019-02-26 10:27:29 +00:00
|
|
|
#include "hwrenderer/data/shaderuniforms.h"
|
2020-04-26 10:30:36 +00:00
|
|
|
#include "hw_lightbuffer.h"
|
2019-02-20 20:21:57 +00:00
|
|
|
|
|
|
|
#include "vk_framebuffer.h"
|
2019-02-21 11:31:14 +00:00
|
|
|
#include "vk_buffers.h"
|
2019-02-21 21:49:00 +00:00
|
|
|
#include "vulkan/renderer/vk_renderstate.h"
|
2019-02-26 10:27:29 +00:00
|
|
|
#include "vulkan/renderer/vk_renderpass.h"
|
2022-06-06 19:30:55 +00:00
|
|
|
#include "vulkan/renderer/vk_descriptorset.h"
|
2019-07-28 14:28:43 +00:00
|
|
|
#include "vulkan/renderer/vk_streambuffer.h"
|
2019-03-05 03:59:17 +00:00
|
|
|
#include "vulkan/renderer/vk_postprocess.h"
|
2022-06-05 21:24:53 +00:00
|
|
|
#include "vulkan/renderer/vk_raytrace.h"
|
2019-02-21 21:49:00 +00:00
|
|
|
#include "vulkan/shaders/vk_shader.h"
|
2022-06-09 20:21:04 +00:00
|
|
|
#include "vulkan/textures/vk_renderbuffers.h"
|
2019-02-20 20:21:57 +00:00
|
|
|
#include "vulkan/textures/vk_samplers.h"
|
2019-02-26 14:29:08 +00:00
|
|
|
#include "vulkan/textures/vk_hwtexture.h"
|
2019-02-20 23:25:51 +00:00
|
|
|
#include "vulkan/system/vk_builders.h"
|
2019-02-21 09:19:59 +00:00
|
|
|
#include "vulkan/system/vk_swapchain.h"
|
2022-06-09 20:21:04 +00:00
|
|
|
#include "vulkan/system/vk_commandbuffer.h"
|
2020-04-11 10:56:55 +00:00
|
|
|
#include "engineerrors.h"
|
2020-07-23 14:02:14 +00:00
|
|
|
#include "c_dispatch.h"
|
2019-02-20 23:25:51 +00:00
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
EXTERN_CVAR(Bool, r_drawvoxels)
|
|
|
|
EXTERN_CVAR(Int, gl_tonemap)
|
2019-03-01 18:01:06 +00:00
|
|
|
EXTERN_CVAR(Int, screenblocks)
|
|
|
|
EXTERN_CVAR(Bool, cl_capfps)
|
|
|
|
|
2020-07-23 14:02:14 +00:00
|
|
|
CCMD(vk_memstats)
|
|
|
|
{
|
|
|
|
VmaStats stats = {};
|
|
|
|
vmaCalculateStats(GetVulkanFrameBuffer()->device->allocator, &stats);
|
|
|
|
Printf("Allocated objects: %d, used bytes: %d MB\n", (int)stats.total.allocationCount, (int)stats.total.usedBytes / (1024 * 1024));
|
|
|
|
Printf("Unused range count: %d, unused bytes: %d MB\n", (int)stats.total.unusedRangeCount, (int)stats.total.unusedBytes / (1024 * 1024));
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevice *dev) :
|
|
|
|
Super(hMonitor, fullscreen)
|
|
|
|
{
|
2019-02-21 09:19:59 +00:00
|
|
|
device = dev;
|
|
|
|
}
|
2019-02-20 23:25:51 +00:00
|
|
|
|
2019-02-21 09:19:59 +00:00
|
|
|
VulkanFrameBuffer::~VulkanFrameBuffer()
|
|
|
|
{
|
2019-08-08 18:56:11 +00:00
|
|
|
vkDeviceWaitIdle(device->device); // make sure the GPU is no longer using any objects before RAII tears them down
|
|
|
|
|
2019-04-30 18:02:00 +00:00
|
|
|
// screen is already null at this point, but VkHardwareTexture::ResetAll needs it during clean up. Is there a better way we can do this?
|
|
|
|
auto tmp = screen;
|
|
|
|
screen = this;
|
|
|
|
|
2019-03-01 17:31:33 +00:00
|
|
|
// All descriptors must be destroyed before the descriptor pool in renderpass manager is destroyed
|
2019-04-30 17:49:29 +00:00
|
|
|
VkHardwareTexture::ResetAll();
|
|
|
|
VKBuffer::ResetAll();
|
2019-03-15 22:24:31 +00:00
|
|
|
PPResource::ResetAll();
|
|
|
|
|
2019-07-28 14:28:43 +00:00
|
|
|
delete MatrixBuffer;
|
|
|
|
delete StreamBuffer;
|
2019-02-26 19:19:54 +00:00
|
|
|
delete mVertexData;
|
|
|
|
delete mSkyData;
|
|
|
|
delete mViewpoints;
|
|
|
|
delete mLights;
|
2019-03-15 06:54:34 +00:00
|
|
|
mShadowMap.Reset();
|
2019-03-17 19:36:08 +00:00
|
|
|
|
2019-04-30 18:02:00 +00:00
|
|
|
screen = tmp;
|
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->DeleteFrameObjects();
|
2019-02-21 09:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VulkanFrameBuffer::InitializeState()
|
|
|
|
{
|
2019-03-03 13:32:03 +00:00
|
|
|
static bool first = true;
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
PrintStartupLog();
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2019-08-11 19:16:09 +00:00
|
|
|
// Use the same names here as OpenGL returns.
|
2019-06-04 09:42:01 +00:00
|
|
|
switch (device->PhysicalDevice.Properties.vendorID)
|
|
|
|
{
|
2019-08-11 19:16:09 +00:00
|
|
|
case 0x1002: vendorstring = "ATI Technologies Inc."; break;
|
|
|
|
case 0x10DE: vendorstring = "NVIDIA Corporation"; break;
|
2019-06-04 09:42:01 +00:00
|
|
|
case 0x8086: vendorstring = "Intel"; break;
|
|
|
|
default: vendorstring = "Unknown"; break;
|
|
|
|
}
|
|
|
|
|
2019-02-26 19:19:54 +00:00
|
|
|
hwcaps = RFL_SHADER_STORAGE_BUFFER | RFL_BUFFER_STORAGE;
|
2019-03-05 18:49:06 +00:00
|
|
|
glslversion = 4.50f;
|
2019-03-09 09:20:14 +00:00
|
|
|
uniformblockalignment = (unsigned int)device->PhysicalDevice.Properties.limits.minUniformBufferOffsetAlignment;
|
|
|
|
maxuniformblock = device->PhysicalDevice.Properties.limits.maxUniformBufferRange;
|
2019-02-26 19:19:54 +00:00
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands.reset(new VkCommandBufferManager(this));
|
2019-02-21 11:31:14 +00:00
|
|
|
|
2019-03-05 03:59:17 +00:00
|
|
|
mScreenBuffers.reset(new VkRenderBuffers());
|
|
|
|
mSaveBuffers.reset(new VkRenderBuffers());
|
|
|
|
mActiveRenderBuffers = mScreenBuffers.get();
|
|
|
|
|
|
|
|
mPostprocess.reset(new VkPostprocess());
|
2022-06-06 19:30:55 +00:00
|
|
|
mDescriptorSetManager.reset(new VkDescriptorSetManager());
|
2019-03-01 14:37:13 +00:00
|
|
|
mRenderPassManager.reset(new VkRenderPassManager());
|
2022-06-05 21:24:53 +00:00
|
|
|
mRaytrace.reset(new VkRaytrace());
|
2019-03-01 14:37:13 +00:00
|
|
|
|
2019-02-21 11:31:14 +00:00
|
|
|
mVertexData = new FFlatVertexBuffer(GetWidth(), GetHeight());
|
|
|
|
mSkyData = new FSkyVertexBuffer;
|
2019-06-10 08:50:17 +00:00
|
|
|
mViewpoints = new HWViewpointBuffer;
|
2019-02-21 11:31:14 +00:00
|
|
|
mLights = new FLightBuffer();
|
|
|
|
|
2019-03-08 20:34:21 +00:00
|
|
|
CreateFanToTrisIndexBuffer();
|
|
|
|
|
2019-02-26 15:50:54 +00:00
|
|
|
// To do: move this to HW renderer interface maybe?
|
2019-07-28 15:04:56 +00:00
|
|
|
MatrixBuffer = new VkStreamBuffer(sizeof(MatricesUBO), 50000);
|
2019-07-28 17:05:39 +00:00
|
|
|
StreamBuffer = new VkStreamBuffer(sizeof(StreamUBO), 300);
|
2019-02-26 15:50:54 +00:00
|
|
|
|
2019-02-22 10:30:48 +00:00
|
|
|
mShaderManager.reset(new VkShaderManager(device));
|
2022-06-09 22:12:03 +00:00
|
|
|
mSamplerManager.reset(new VkSamplerManager(this));
|
2022-06-06 19:30:55 +00:00
|
|
|
mDescriptorSetManager->Init();
|
2019-03-08 20:34:21 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
mRenderState.reset(new VkRenderStateMolten());
|
|
|
|
#else
|
2019-02-21 21:49:00 +00:00
|
|
|
mRenderState.reset(new VkRenderState());
|
2019-03-08 20:34:21 +00:00
|
|
|
#endif
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VulkanFrameBuffer::Update()
|
|
|
|
{
|
|
|
|
twoD.Reset();
|
|
|
|
Flush3D.Reset();
|
|
|
|
|
|
|
|
Flush3D.Clock();
|
|
|
|
|
2019-03-07 17:05:12 +00:00
|
|
|
GetPostprocess()->SetActiveRenderTarget();
|
|
|
|
|
2019-02-21 09:19:59 +00:00
|
|
|
Draw2D();
|
2020-04-28 15:22:17 +00:00
|
|
|
twod->Clear();
|
2019-02-26 10:27:29 +00:00
|
|
|
|
|
|
|
mRenderState->EndRenderPass();
|
2019-03-07 17:05:12 +00:00
|
|
|
mRenderState->EndFrame();
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-03-13 13:10:13 +00:00
|
|
|
Flush3D.Unclock();
|
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->WaitForCommands(true);
|
|
|
|
mCommands->UpdateGpuStats();
|
2019-03-13 13:10:13 +00:00
|
|
|
|
|
|
|
Super::Update();
|
|
|
|
}
|
|
|
|
|
2022-05-12 22:45:27 +00:00
|
|
|
bool VulkanFrameBuffer::CompileNextShader()
|
|
|
|
{
|
|
|
|
return mShaderManager->CompileNextShader();
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:13:36 +00:00
|
|
|
void VulkanFrameBuffer::RenderTextureView(FCanvasTexture* tex, std::function<void(IntRect &)> renderFunc)
|
2019-03-13 10:14:42 +00:00
|
|
|
{
|
2020-04-16 21:37:22 +00:00
|
|
|
auto BaseLayer = static_cast<VkHardwareTexture*>(tex->GetHardwareTexture(0, 0));
|
2019-03-13 10:14:42 +00:00
|
|
|
|
2019-05-16 16:26:47 +00:00
|
|
|
VkTextureImage *image = BaseLayer->GetImage(tex, 0, 0);
|
|
|
|
VkTextureImage *depthStencil = BaseLayer->GetDepthStencil(tex);
|
2019-03-13 10:14:42 +00:00
|
|
|
|
|
|
|
mRenderState->EndRenderPass();
|
|
|
|
|
2019-05-16 16:26:47 +00:00
|
|
|
VkImageTransition barrier0;
|
|
|
|
barrier0.addImage(image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, true);
|
2022-06-09 20:21:04 +00:00
|
|
|
barrier0.execute(mCommands->GetDrawCommands());
|
2019-03-13 10:14:42 +00:00
|
|
|
|
2019-06-10 20:14:02 +00:00
|
|
|
mRenderState->SetRenderTarget(image, depthStencil->View.get(), image->Image->width, image->Image->height, VK_FORMAT_R8G8B8A8_UNORM, VK_SAMPLE_COUNT_1_BIT);
|
2019-03-13 10:14:42 +00:00
|
|
|
|
|
|
|
IntRect bounds;
|
|
|
|
bounds.left = bounds.top = 0;
|
2021-10-30 08:46:17 +00:00
|
|
|
bounds.width = min(tex->GetWidth(), image->Image->width);
|
|
|
|
bounds.height = min(tex->GetHeight(), image->Image->height);
|
2019-03-13 10:14:42 +00:00
|
|
|
|
2020-04-25 12:13:36 +00:00
|
|
|
renderFunc(bounds);
|
2019-03-13 10:14:42 +00:00
|
|
|
|
|
|
|
mRenderState->EndRenderPass();
|
|
|
|
|
2019-05-16 16:26:47 +00:00
|
|
|
VkImageTransition barrier1;
|
|
|
|
barrier1.addImage(image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false);
|
2022-06-09 20:21:04 +00:00
|
|
|
barrier1.execute(mCommands->GetDrawCommands());
|
2019-03-13 10:14:42 +00:00
|
|
|
|
2019-06-10 20:14:02 +00:00
|
|
|
mRenderState->SetRenderTarget(&GetBuffers()->SceneColor, GetBuffers()->SceneDepthStencil.View.get(), GetBuffers()->GetWidth(), GetBuffers()->GetHeight(), VK_FORMAT_R16G16B16A16_SFLOAT, GetBuffers()->GetSceneSamples());
|
2019-03-13 10:14:42 +00:00
|
|
|
|
|
|
|
tex->SetUpdated(true);
|
|
|
|
}
|
|
|
|
|
2020-06-07 07:16:56 +00:00
|
|
|
void VulkanFrameBuffer::PostProcessScene(bool swscene, int fixedcm, float flash, const std::function<void()> &afterBloomDrawEndScene2D)
|
2019-03-07 17:05:12 +00:00
|
|
|
{
|
2020-04-25 08:51:45 +00:00
|
|
|
if (!swscene) mPostprocess->BlitSceneToPostprocess(); // Copy the resulting scene to the current post process texture
|
2020-06-07 07:16:56 +00:00
|
|
|
mPostprocess->PostProcessScene(fixedcm, flash, afterBloomDrawEndScene2D);
|
2019-03-07 17:05:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 19:16:09 +00:00
|
|
|
const char* VulkanFrameBuffer::DeviceName() const
|
|
|
|
{
|
|
|
|
const auto &props = device->PhysicalDevice.Properties;
|
|
|
|
return props.deviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
void VulkanFrameBuffer::SetVSync(bool vsync)
|
|
|
|
{
|
2019-04-11 02:26:43 +00:00
|
|
|
// This is handled in VulkanSwapChain::AcquireImage.
|
2019-10-01 18:50:15 +00:00
|
|
|
cur_vsync = vsync;
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 21:41:02 +00:00
|
|
|
void VulkanFrameBuffer::PrecacheMaterial(FMaterial *mat, int translation)
|
|
|
|
{
|
2020-04-15 16:59:14 +00:00
|
|
|
if (mat->Source()->GetUseType() == ETextureType::SWCanvas) return;
|
2019-03-17 21:41:02 +00:00
|
|
|
|
2020-04-19 08:08:24 +00:00
|
|
|
MaterialLayerInfo* layer;
|
2019-03-17 21:41:02 +00:00
|
|
|
|
2020-04-13 15:05:49 +00:00
|
|
|
auto systex = static_cast<VkHardwareTexture*>(mat->GetLayer(0, translation, &layer));
|
2020-04-19 08:08:24 +00:00
|
|
|
systex->GetImage(layer->layerTexture, translation, layer->scaleFlags);
|
2020-04-13 15:05:49 +00:00
|
|
|
|
2020-04-19 08:08:24 +00:00
|
|
|
int numLayers = mat->NumLayers();
|
2020-04-13 15:05:49 +00:00
|
|
|
for (int i = 1; i < numLayers; i++)
|
|
|
|
{
|
2021-12-29 09:25:31 +00:00
|
|
|
auto syslayer = static_cast<VkHardwareTexture*>(mat->GetLayer(i, 0, &layer));
|
|
|
|
syslayer->GetImage(layer->layerTexture, 0, layer->scaleFlags);
|
2020-04-13 15:05:49 +00:00
|
|
|
}
|
2019-03-17 21:41:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:51:17 +00:00
|
|
|
IHardwareTexture *VulkanFrameBuffer::CreateHardwareTexture(int numchannels)
|
2019-02-26 14:29:08 +00:00
|
|
|
{
|
2021-04-19 10:58:35 +00:00
|
|
|
return new VkHardwareTexture(numchannels);
|
2019-02-26 14:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 21:56:22 +00:00
|
|
|
FMaterial* VulkanFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags)
|
|
|
|
{
|
|
|
|
return new VkMaterial(tex, scaleflags);
|
|
|
|
}
|
|
|
|
|
2019-02-21 11:31:14 +00:00
|
|
|
IVertexBuffer *VulkanFrameBuffer::CreateVertexBuffer()
|
|
|
|
{
|
|
|
|
return new VKVertexBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
IIndexBuffer *VulkanFrameBuffer::CreateIndexBuffer()
|
|
|
|
{
|
|
|
|
return new VKIndexBuffer();
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:01:42 +00:00
|
|
|
IDataBuffer *VulkanFrameBuffer::CreateDataBuffer(int bindingpoint, bool ssbo, bool needsresize)
|
2019-02-21 11:31:14 +00:00
|
|
|
{
|
2019-04-18 20:01:42 +00:00
|
|
|
auto buffer = new VKDataBuffer(bindingpoint, ssbo, needsresize);
|
2019-03-15 06:54:34 +00:00
|
|
|
|
|
|
|
switch (bindingpoint)
|
2019-02-26 10:27:29 +00:00
|
|
|
{
|
2019-03-15 06:54:34 +00:00
|
|
|
case LIGHTBUF_BINDINGPOINT: LightBufferSSO = buffer; break;
|
|
|
|
case VIEWPOINT_BINDINGPOINT: ViewpointUBO = buffer; break;
|
|
|
|
case LIGHTNODES_BINDINGPOINT: LightNodes = buffer; break;
|
|
|
|
case LIGHTLINES_BINDINGPOINT: LightLines = buffer; break;
|
|
|
|
case LIGHTLIST_BINDINGPOINT: LightList = buffer; break;
|
|
|
|
case POSTPROCESS_BINDINGPOINT: break;
|
|
|
|
default: break;
|
2019-02-26 10:27:29 +00:00
|
|
|
}
|
2019-03-15 06:54:34 +00:00
|
|
|
|
2019-02-26 10:27:29 +00:00
|
|
|
return buffer;
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 21:27:48 +00:00
|
|
|
void VulkanFrameBuffer::SetTextureFilterMode()
|
2019-02-20 20:21:57 +00:00
|
|
|
{
|
2019-03-06 21:59:21 +00:00
|
|
|
if (mSamplerManager)
|
|
|
|
{
|
2022-06-09 22:12:03 +00:00
|
|
|
mDescriptorSetManager->FilterModeChanged();
|
|
|
|
mSamplerManager->FilterModeChanged();
|
2019-03-06 21:59:21 +00:00
|
|
|
}
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 20:57:39 +00:00
|
|
|
void VulkanFrameBuffer::StartPrecaching()
|
|
|
|
{
|
|
|
|
// Destroy the texture descriptors to avoid problems with potentially stale textures.
|
2020-04-18 21:56:22 +00:00
|
|
|
VkMaterial::ResetAllDescriptors();
|
2019-03-21 20:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
void VulkanFrameBuffer::BlurScene(float amount)
|
|
|
|
{
|
2019-03-06 21:59:21 +00:00
|
|
|
if (mPostprocess)
|
|
|
|
mPostprocess->BlurScene(amount);
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VulkanFrameBuffer::UpdatePalette()
|
|
|
|
{
|
2019-03-06 21:59:21 +00:00
|
|
|
if (mPostprocess)
|
|
|
|
mPostprocess->ClearTonemapPalette();
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 01:23:33 +00:00
|
|
|
FTexture *VulkanFrameBuffer::WipeStartScreen()
|
|
|
|
{
|
2019-06-10 06:40:49 +00:00
|
|
|
SetViewportRects(nullptr);
|
|
|
|
|
|
|
|
auto tex = new FWrapperTexture(mScreenViewport.width, mScreenViewport.height, 1);
|
2019-03-13 01:23:33 +00:00
|
|
|
auto systex = static_cast<VkHardwareTexture*>(tex->GetSystemTexture());
|
|
|
|
|
2019-06-10 06:40:49 +00:00
|
|
|
systex->CreateWipeTexture(mScreenViewport.width, mScreenViewport.height, "WipeStartScreen");
|
2019-03-13 01:23:33 +00:00
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
FTexture *VulkanFrameBuffer::WipeEndScreen()
|
|
|
|
{
|
|
|
|
GetPostprocess()->SetActiveRenderTarget();
|
|
|
|
Draw2D();
|
2020-04-28 15:22:17 +00:00
|
|
|
twod->Clear();
|
2019-03-13 01:23:33 +00:00
|
|
|
|
2019-06-10 06:40:49 +00:00
|
|
|
auto tex = new FWrapperTexture(mScreenViewport.width, mScreenViewport.height, 1);
|
2019-03-13 01:23:33 +00:00
|
|
|
auto systex = static_cast<VkHardwareTexture*>(tex->GetSystemTexture());
|
|
|
|
|
2019-06-10 06:40:49 +00:00
|
|
|
systex->CreateWipeTexture(mScreenViewport.width, mScreenViewport.height, "WipeEndScreen");
|
2019-03-13 01:23:33 +00:00
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2020-04-25 11:18:57 +00:00
|
|
|
void VulkanFrameBuffer::CopyScreenToBuffer(int w, int h, uint8_t *data)
|
2019-03-13 13:10:13 +00:00
|
|
|
{
|
2019-05-16 16:26:47 +00:00
|
|
|
VkTextureImage image;
|
|
|
|
|
2019-03-13 13:10:13 +00:00
|
|
|
// Convert from rgba16f to rgba8 using the GPU:
|
|
|
|
ImageBuilder imgbuilder;
|
|
|
|
imgbuilder.setFormat(VK_FORMAT_R8G8B8A8_UNORM);
|
|
|
|
imgbuilder.setUsage(VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
|
|
|
imgbuilder.setSize(w, h);
|
2019-05-16 16:26:47 +00:00
|
|
|
image.Image = imgbuilder.create(device);
|
|
|
|
GetPostprocess()->BlitCurrentToImage(&image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
2019-03-13 13:10:13 +00:00
|
|
|
|
|
|
|
// Staging buffer for download
|
|
|
|
BufferBuilder bufbuilder;
|
|
|
|
bufbuilder.setSize(w * h * 4);
|
|
|
|
bufbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_GPU_TO_CPU);
|
|
|
|
auto staging = bufbuilder.create(device);
|
|
|
|
|
|
|
|
// Copy from image to buffer
|
|
|
|
VkBufferImageCopy region = {};
|
|
|
|
region.imageExtent.width = w;
|
|
|
|
region.imageExtent.height = h;
|
|
|
|
region.imageExtent.depth = 1;
|
|
|
|
region.imageSubresource.layerCount = 1;
|
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->GetDrawCommands()->copyImageToBuffer(image.Image->image, image.Layout, staging->buffer, 1, ®ion);
|
2019-03-13 13:10:13 +00:00
|
|
|
|
|
|
|
// Submit command buffers and wait for device to finish the work
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->WaitForCommands(false);
|
2019-03-13 13:10:13 +00:00
|
|
|
|
|
|
|
// Map and convert from rgba8 to rgb8
|
2019-03-14 22:07:52 +00:00
|
|
|
uint8_t *dest = (uint8_t*)data;
|
2019-03-13 13:10:13 +00:00
|
|
|
uint8_t *pixels = (uint8_t*)staging->Map(0, w * h * 4);
|
|
|
|
int dindex = 0;
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
{
|
|
|
|
int sindex = (h - y - 1) * w * 4;
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
2019-03-14 22:07:52 +00:00
|
|
|
dest[dindex] = pixels[sindex];
|
|
|
|
dest[dindex + 1] = pixels[sindex + 1];
|
|
|
|
dest[dindex + 2] = pixels[sindex + 2];
|
2019-03-13 13:10:13 +00:00
|
|
|
dindex += 3;
|
|
|
|
sindex += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
staging->Unmap();
|
2019-03-14 22:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 15:58:26 +00:00
|
|
|
void VulkanFrameBuffer::SetActiveRenderTarget()
|
|
|
|
{
|
|
|
|
mPostprocess->SetActiveRenderTarget();
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:07:52 +00:00
|
|
|
TArray<uint8_t> VulkanFrameBuffer::GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma)
|
|
|
|
{
|
|
|
|
int w = SCREENWIDTH;
|
|
|
|
int h = SCREENHEIGHT;
|
|
|
|
|
2019-05-20 10:36:45 +00:00
|
|
|
IntRect box;
|
|
|
|
box.left = 0;
|
|
|
|
box.top = 0;
|
|
|
|
box.width = w;
|
|
|
|
box.height = h;
|
|
|
|
mPostprocess->DrawPresentTexture(box, true, true);
|
|
|
|
|
2019-03-14 22:07:52 +00:00
|
|
|
TArray<uint8_t> ScreenshotBuffer(w * h * 3, true);
|
|
|
|
CopyScreenToBuffer(w, h, ScreenshotBuffer.Data());
|
2019-03-13 13:10:13 +00:00
|
|
|
|
|
|
|
pitch = w * 3;
|
|
|
|
color_type = SS_RGB;
|
2019-05-10 16:02:27 +00:00
|
|
|
gamma = 1.0f;
|
2019-03-13 13:10:13 +00:00
|
|
|
return ScreenshotBuffer;
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
void VulkanFrameBuffer::BeginFrame()
|
|
|
|
{
|
2019-03-07 11:24:56 +00:00
|
|
|
SetViewportRects(nullptr);
|
2019-03-05 03:59:17 +00:00
|
|
|
mScreenBuffers->BeginFrame(screen->mScreenViewport.width, screen->mScreenViewport.height, screen->mSceneViewport.width, screen->mSceneViewport.height);
|
|
|
|
mSaveBuffers->BeginFrame(SAVEPICWIDTH, SAVEPICHEIGHT, SAVEPICWIDTH, SAVEPICHEIGHT);
|
2019-03-17 20:14:51 +00:00
|
|
|
mRenderState->BeginFrame();
|
2022-06-06 19:30:55 +00:00
|
|
|
|
|
|
|
WaitForCommands(false);
|
|
|
|
mDescriptorSetManager->UpdateFixedSet();
|
|
|
|
mDescriptorSetManager->UpdateDynamicSet();
|
2019-05-10 00:16:26 +00:00
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->BeginFrame();
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 22:01:30 +00:00
|
|
|
void VulkanFrameBuffer::InitLightmap(int LMTextureSize, int LMTextureCount, TArray<uint16_t>& LMTextureData)
|
2021-09-24 22:13:25 +00:00
|
|
|
{
|
2022-04-10 22:01:30 +00:00
|
|
|
if (LMTextureData.Size() > 0)
|
2021-09-24 22:13:25 +00:00
|
|
|
{
|
2022-04-10 22:01:30 +00:00
|
|
|
int w = LMTextureSize;
|
|
|
|
int h = LMTextureSize;
|
|
|
|
int count = LMTextureCount;
|
2021-09-24 22:13:25 +00:00
|
|
|
int pixelsize = 8;
|
|
|
|
auto& lightmap = mActiveRenderBuffers->Lightmap;
|
|
|
|
|
|
|
|
if (lightmap.Image)
|
|
|
|
{
|
2022-06-09 20:21:04 +00:00
|
|
|
GetCommands()->FrameDeleteList.Images.push_back(std::move(lightmap.Image));
|
|
|
|
GetCommands()->FrameDeleteList.ImageViews.push_back(std::move(lightmap.View));
|
2021-09-24 22:13:25 +00:00
|
|
|
lightmap.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageBuilder builder;
|
|
|
|
builder.setSize(w, h, 1, count);
|
|
|
|
builder.setFormat(VK_FORMAT_R16G16B16A16_SFLOAT);
|
|
|
|
builder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
|
|
|
lightmap.Image = builder.create(device);
|
|
|
|
lightmap.Image->SetDebugName("VkRenderBuffers.Lightmap");
|
|
|
|
|
|
|
|
ImageViewBuilder viewbuilder;
|
|
|
|
viewbuilder.setType(VK_IMAGE_VIEW_TYPE_2D_ARRAY);
|
|
|
|
viewbuilder.setImage(lightmap.Image.get(), VK_FORMAT_R16G16B16A16_SFLOAT);
|
|
|
|
lightmap.View = viewbuilder.create(device);
|
|
|
|
lightmap.View->SetDebugName("VkRenderBuffers.LightmapView");
|
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
auto cmdbuffer = GetCommands()->GetTransferCommands();
|
2021-09-24 22:13:25 +00:00
|
|
|
|
|
|
|
int totalSize = w * h * count * pixelsize;
|
|
|
|
|
|
|
|
BufferBuilder bufbuilder;
|
|
|
|
bufbuilder.setSize(totalSize);
|
|
|
|
bufbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
|
|
|
|
std::unique_ptr<VulkanBuffer> stagingBuffer = bufbuilder.create(device);
|
|
|
|
stagingBuffer->SetDebugName("VkHardwareTexture.mStagingBuffer");
|
|
|
|
|
|
|
|
uint16_t one = 0x3c00; // half-float 1.0
|
2022-04-10 22:01:30 +00:00
|
|
|
uint16_t* src = &LMTextureData[0];
|
2021-09-24 22:13:25 +00:00
|
|
|
uint16_t* data = (uint16_t*)stagingBuffer->Map(0, totalSize);
|
|
|
|
for (int i = w * h * count; i > 0; i--)
|
|
|
|
{
|
|
|
|
*(data++) = *(src++);
|
|
|
|
*(data++) = *(src++);
|
|
|
|
*(data++) = *(src++);
|
|
|
|
*(data++) = one;
|
|
|
|
}
|
|
|
|
stagingBuffer->Unmap();
|
|
|
|
|
|
|
|
VkImageTransition imageTransition;
|
2021-11-14 18:53:10 +00:00
|
|
|
imageTransition.addImage(&lightmap, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, true, 0, count);
|
2021-09-24 22:13:25 +00:00
|
|
|
imageTransition.execute(cmdbuffer);
|
|
|
|
|
|
|
|
VkBufferImageCopy region = {};
|
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
region.imageSubresource.layerCount = count;
|
|
|
|
region.imageExtent.depth = 1;
|
|
|
|
region.imageExtent.width = w;
|
|
|
|
region.imageExtent.height = h;
|
|
|
|
cmdbuffer->copyBufferToImage(stagingBuffer->buffer, lightmap.Image->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
|
|
|
|
|
|
|
VkImageTransition barrier;
|
2021-11-14 18:53:10 +00:00
|
|
|
barrier.addImage(&lightmap, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false, 0, count);
|
2021-09-24 22:13:25 +00:00
|
|
|
barrier.execute(cmdbuffer);
|
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
GetCommands()->FrameTextureUpload.Buffers.push_back(std::move(stagingBuffer));
|
|
|
|
GetCommands()->FrameTextureUpload.TotalSize += totalSize;
|
2021-09-24 22:13:25 +00:00
|
|
|
|
2022-04-10 22:01:30 +00:00
|
|
|
LMTextureData.Reset(); // We no longer need this, release the memory
|
2021-09-24 22:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:21:57 +00:00
|
|
|
void VulkanFrameBuffer::Draw2D()
|
|
|
|
{
|
2020-04-28 15:22:17 +00:00
|
|
|
::Draw2D(twod, *mRenderState);
|
2019-02-20 20:21:57 +00:00
|
|
|
}
|
2019-02-21 11:31:14 +00:00
|
|
|
|
2022-06-09 20:21:04 +00:00
|
|
|
void VulkanFrameBuffer::WaitForCommands(bool finish)
|
2019-02-21 11:31:14 +00:00
|
|
|
{
|
2022-06-09 20:21:04 +00:00
|
|
|
mCommands->WaitForCommands(finish);
|
2019-02-21 11:31:14 +00:00
|
|
|
}
|
2019-03-03 13:32:03 +00:00
|
|
|
|
2019-03-05 01:06:20 +00:00
|
|
|
unsigned int VulkanFrameBuffer::GetLightBufferBlockSize() const
|
|
|
|
{
|
|
|
|
return mLights->GetBlockSize();
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:32:03 +00:00
|
|
|
void VulkanFrameBuffer::PrintStartupLog()
|
|
|
|
{
|
2019-08-11 19:16:09 +00:00
|
|
|
const auto &props = device->PhysicalDevice.Properties;
|
2019-03-09 09:20:14 +00:00
|
|
|
|
2019-03-03 13:32:03 +00:00
|
|
|
FString deviceType;
|
2019-03-09 09:20:14 +00:00
|
|
|
switch (props.deviceType)
|
2019-03-03 13:32:03 +00:00
|
|
|
{
|
|
|
|
case VK_PHYSICAL_DEVICE_TYPE_OTHER: deviceType = "other"; break;
|
|
|
|
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: deviceType = "integrated gpu"; break;
|
|
|
|
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: deviceType = "discrete gpu"; break;
|
|
|
|
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: deviceType = "virtual gpu"; break;
|
|
|
|
case VK_PHYSICAL_DEVICE_TYPE_CPU: deviceType = "cpu"; break;
|
2019-03-09 09:20:14 +00:00
|
|
|
default: deviceType.Format("%d", (int)props.deviceType); break;
|
2019-03-03 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FString apiVersion, driverVersion;
|
2019-03-09 09:20:14 +00:00
|
|
|
apiVersion.Format("%d.%d.%d", VK_VERSION_MAJOR(props.apiVersion), VK_VERSION_MINOR(props.apiVersion), VK_VERSION_PATCH(props.apiVersion));
|
|
|
|
driverVersion.Format("%d.%d.%d", VK_VERSION_MAJOR(props.driverVersion), VK_VERSION_MINOR(props.driverVersion), VK_VERSION_PATCH(props.driverVersion));
|
2019-03-03 13:32:03 +00:00
|
|
|
|
2019-03-09 09:20:14 +00:00
|
|
|
Printf("Vulkan device: " TEXTCOLOR_ORANGE "%s\n", props.deviceName);
|
2019-03-03 13:32:03 +00:00
|
|
|
Printf("Vulkan device type: %s\n", deviceType.GetChars());
|
|
|
|
Printf("Vulkan version: %s (api) %s (driver)\n", apiVersion.GetChars(), driverVersion.GetChars());
|
|
|
|
|
|
|
|
Printf(PRINT_LOG, "Vulkan extensions:");
|
2019-03-09 09:20:14 +00:00
|
|
|
for (const VkExtensionProperties &p : device->PhysicalDevice.Extensions)
|
2019-03-03 13:32:03 +00:00
|
|
|
{
|
|
|
|
Printf(PRINT_LOG, " %s", p.extensionName);
|
|
|
|
}
|
|
|
|
Printf(PRINT_LOG, "\n");
|
|
|
|
|
2019-03-09 09:20:14 +00:00
|
|
|
const auto &limits = props.limits;
|
2019-03-03 13:32:03 +00:00
|
|
|
Printf("Max. texture size: %d\n", limits.maxImageDimension2D);
|
|
|
|
Printf("Max. uniform buffer range: %d\n", limits.maxUniformBufferRange);
|
2021-07-01 10:19:18 +00:00
|
|
|
Printf("Min. uniform buffer offset alignment: %" PRIu64 "\n", limits.minUniformBufferOffsetAlignment);
|
2019-03-03 13:32:03 +00:00
|
|
|
}
|
2019-03-08 20:34:21 +00:00
|
|
|
|
|
|
|
void VulkanFrameBuffer::CreateFanToTrisIndexBuffer()
|
|
|
|
{
|
|
|
|
TArray<uint32_t> data;
|
|
|
|
for (int i = 2; i < 1000; i++)
|
|
|
|
{
|
|
|
|
data.Push(0);
|
|
|
|
data.Push(i - 1);
|
|
|
|
data.Push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
FanToTrisIndexBuffer.reset(CreateIndexBuffer());
|
2021-10-27 01:38:02 +00:00
|
|
|
FanToTrisIndexBuffer->SetData(sizeof(uint32_t) * data.Size(), data.Data(), BufferUsageType::Static);
|
2019-03-08 20:34:21 +00:00
|
|
|
}
|
2019-03-15 06:54:34 +00:00
|
|
|
|
2022-06-05 21:24:53 +00:00
|
|
|
void VulkanFrameBuffer::SetLevelMesh(hwrenderer::LevelMesh* mesh)
|
|
|
|
{
|
|
|
|
mRaytrace->SetLevelMesh(mesh);
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:54:34 +00:00
|
|
|
void VulkanFrameBuffer::UpdateShadowMap()
|
|
|
|
{
|
|
|
|
mPostprocess->UpdateShadowMap();
|
|
|
|
}
|
2020-04-24 14:15:18 +00:00
|
|
|
|
2020-04-25 11:18:57 +00:00
|
|
|
void VulkanFrameBuffer::SetSaveBuffers(bool yes)
|
|
|
|
{
|
|
|
|
if (yes) mActiveRenderBuffers = mSaveBuffers.get();
|
|
|
|
else mActiveRenderBuffers = mScreenBuffers.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VulkanFrameBuffer::ImageTransitionScene(bool unknown)
|
|
|
|
{
|
|
|
|
mPostprocess->ImageTransitionScene(unknown);
|
|
|
|
}
|
|
|
|
|
2020-04-24 14:15:18 +00:00
|
|
|
FRenderState* VulkanFrameBuffer::RenderState()
|
|
|
|
{
|
|
|
|
return mRenderState.get();
|
|
|
|
}
|
2020-04-24 19:47:18 +00:00
|
|
|
|
|
|
|
void VulkanFrameBuffer::AmbientOccludeScene(float m5)
|
|
|
|
{
|
|
|
|
mPostprocess->AmbientOccludeScene(m5);
|
|
|
|
}
|
2020-04-25 08:51:45 +00:00
|
|
|
|
|
|
|
void VulkanFrameBuffer::SetSceneRenderTarget(bool useSSAO)
|
|
|
|
{
|
|
|
|
mRenderState->SetRenderTarget(&GetBuffers()->SceneColor, GetBuffers()->SceneDepthStencil.View.get(), GetBuffers()->GetWidth(), GetBuffers()->GetHeight(), VK_FORMAT_R16G16B16A16_SFLOAT, GetBuffers()->GetSceneSamples());
|
|
|
|
}
|