mirror of
https://github.com/ZDoom/gzdoom.git
synced 2025-01-19 16:11:23 +00:00
- upload and bind some textures
This commit is contained in:
parent
347339b254
commit
d958c4fec5
5 changed files with 208 additions and 8 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include "vulkan/system/vk_framebuffer.h"
|
#include "vulkan/system/vk_framebuffer.h"
|
||||||
#include "vulkan/system/vk_builders.h"
|
#include "vulkan/system/vk_builders.h"
|
||||||
#include "vulkan/renderer/vk_renderpass.h"
|
#include "vulkan/renderer/vk_renderpass.h"
|
||||||
|
#include "vulkan/textures/vk_hwtexture.h"
|
||||||
#include "templates.h"
|
#include "templates.h"
|
||||||
#include "doomstat.h"
|
#include "doomstat.h"
|
||||||
#include "r_data/colormaps.h"
|
#include "r_data/colormaps.h"
|
||||||
|
@ -165,6 +166,15 @@ void VkRenderState::Apply(int dt)
|
||||||
mCommandBuffer->bindIndexBuffer(static_cast<VKIndexBuffer*>(mIndexBuffer)->mBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
|
mCommandBuffer->bindIndexBuffer(static_cast<VKIndexBuffer*>(mIndexBuffer)->mBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||||
|
|
||||||
BindDescriptorSets();
|
BindDescriptorSets();
|
||||||
|
|
||||||
|
if (mMaterial.mChanged)
|
||||||
|
{
|
||||||
|
auto base = static_cast<VkHardwareTexture*>(mMaterial.mMaterial->GetLayer(0, mMaterial.mTranslation));
|
||||||
|
if (base)
|
||||||
|
mCommandBuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, passManager->PipelineLayout.get(), 1, base->GetDescriptorSet(mMaterial));
|
||||||
|
|
||||||
|
mMaterial.mChanged = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VkRenderState::Bind(int bindingpoint, uint32_t offset)
|
void VkRenderState::Bind(int bindingpoint, uint32_t offset)
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
#include "vulkan/renderer/vk_renderpass.h"
|
#include "vulkan/renderer/vk_renderpass.h"
|
||||||
#include "vulkan/shaders/vk_shader.h"
|
#include "vulkan/shaders/vk_shader.h"
|
||||||
#include "vulkan/textures/vk_samplers.h"
|
#include "vulkan/textures/vk_samplers.h"
|
||||||
|
#include "vulkan/textures/vk_hwtexture.h"
|
||||||
#include "vulkan/system/vk_builders.h"
|
#include "vulkan/system/vk_builders.h"
|
||||||
#include "vulkan/system/vk_swapchain.h"
|
#include "vulkan/system/vk_swapchain.h"
|
||||||
#include "doomerrors.h"
|
#include "doomerrors.h"
|
||||||
|
@ -62,6 +63,8 @@ VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevi
|
||||||
|
|
||||||
VulkanFrameBuffer::~VulkanFrameBuffer()
|
VulkanFrameBuffer::~VulkanFrameBuffer()
|
||||||
{
|
{
|
||||||
|
for (auto tex : AllTextures)
|
||||||
|
tex->Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanFrameBuffer::InitializeState()
|
void VulkanFrameBuffer::InitializeState()
|
||||||
|
@ -232,6 +235,13 @@ void VulkanFrameBuffer::CleanForRestart()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IHardwareTexture *VulkanFrameBuffer::CreateHardwareTexture()
|
||||||
|
{
|
||||||
|
auto texture = new VkHardwareTexture();
|
||||||
|
AllTextures.Push(texture);
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
FModelRenderer *VulkanFrameBuffer::CreateModelRenderer(int mli)
|
FModelRenderer *VulkanFrameBuffer::CreateModelRenderer(int mli)
|
||||||
{
|
{
|
||||||
I_FatalError("VulkanFrameBuffer::CreateModelRenderer not implemented\n");
|
I_FatalError("VulkanFrameBuffer::CreateModelRenderer not implemented\n");
|
||||||
|
|
|
@ -9,6 +9,7 @@ class VkShaderManager;
|
||||||
class VkRenderPassManager;
|
class VkRenderPassManager;
|
||||||
class VkRenderState;
|
class VkRenderState;
|
||||||
class VKDataBuffer;
|
class VKDataBuffer;
|
||||||
|
class VkHardwareTexture;
|
||||||
|
|
||||||
class VulkanFrameBuffer : public SystemBaseFrameBuffer
|
class VulkanFrameBuffer : public SystemBaseFrameBuffer
|
||||||
{
|
{
|
||||||
|
@ -21,6 +22,7 @@ public:
|
||||||
VulkanCommandBuffer *GetUploadCommands();
|
VulkanCommandBuffer *GetUploadCommands();
|
||||||
VulkanCommandBuffer *GetDrawCommands();
|
VulkanCommandBuffer *GetDrawCommands();
|
||||||
VkShaderManager *GetShaderManager() { return mShaderManager.get(); }
|
VkShaderManager *GetShaderManager() { return mShaderManager.get(); }
|
||||||
|
VkSamplerManager *GetSamplerManager() { return mSamplerManager.get(); }
|
||||||
VkRenderPassManager *GetRenderPassManager() { return mRenderPassManager.get(); }
|
VkRenderPassManager *GetRenderPassManager() { return mRenderPassManager.get(); }
|
||||||
VkRenderState *GetRenderState() { return mRenderState.get(); }
|
VkRenderState *GetRenderState() { return mRenderState.get(); }
|
||||||
|
|
||||||
|
@ -44,6 +46,7 @@ public:
|
||||||
void BeginFrame() override;
|
void BeginFrame() override;
|
||||||
void BlurScene(float amount) override;
|
void BlurScene(float amount) override;
|
||||||
|
|
||||||
|
IHardwareTexture *CreateHardwareTexture() override;
|
||||||
FModelRenderer *CreateModelRenderer(int mli) override;
|
FModelRenderer *CreateModelRenderer(int mli) override;
|
||||||
IShaderProgram *CreateShaderProgram() override;
|
IShaderProgram *CreateShaderProgram() override;
|
||||||
IVertexBuffer *CreateVertexBuffer() override;
|
IVertexBuffer *CreateVertexBuffer() override;
|
||||||
|
@ -75,6 +78,8 @@ private:
|
||||||
|
|
||||||
int lastSwapWidth = 0;
|
int lastSwapWidth = 0;
|
||||||
int lastSwapHeight = 0;
|
int lastSwapHeight = 0;
|
||||||
|
|
||||||
|
TArray<VkHardwareTexture*> AllTextures;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline VulkanFrameBuffer *GetVulkanFrameBuffer() { return static_cast<VulkanFrameBuffer*>(screen); }
|
inline VulkanFrameBuffer *GetVulkanFrameBuffer() { return static_cast<VulkanFrameBuffer*>(screen); }
|
||||||
|
|
|
@ -28,10 +28,155 @@
|
||||||
#include "c_cvars.h"
|
#include "c_cvars.h"
|
||||||
#include "r_data/colormaps.h"
|
#include "r_data/colormaps.h"
|
||||||
#include "hwrenderer/textures/hw_material.h"
|
#include "hwrenderer/textures/hw_material.h"
|
||||||
|
|
||||||
#include "hwrenderer/utility/hw_cvars.h"
|
#include "hwrenderer/utility/hw_cvars.h"
|
||||||
|
#include "vulkan/system/vk_objects.h"
|
||||||
|
#include "vulkan/system/vk_builders.h"
|
||||||
|
#include "vulkan/system/vk_framebuffer.h"
|
||||||
|
#include "vulkan/textures/vk_samplers.h"
|
||||||
|
#include "vulkan/renderer/vk_renderpass.h"
|
||||||
#include "vk_hwtexture.h"
|
#include "vk_hwtexture.h"
|
||||||
|
|
||||||
|
VkHardwareTexture::VkHardwareTexture()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
VkHardwareTexture::~VkHardwareTexture()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void VkHardwareTexture::Reset()
|
||||||
|
{
|
||||||
|
mDescriptorSet.reset();
|
||||||
|
mImage.reset();
|
||||||
|
mImageView.reset();
|
||||||
|
mStagingBuffer.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
VulkanDescriptorSet *VkHardwareTexture::GetDescriptorSet(const FMaterialState &state)
|
||||||
|
{
|
||||||
|
if (!mImage)
|
||||||
|
{
|
||||||
|
static const uint32_t testpixels[4 * 4] =
|
||||||
|
{
|
||||||
|
0xff0000ff, 0xff0000ff, 0xffff00ff, 0xffff00ff,
|
||||||
|
0xff0000ff, 0xff0000ff, 0xffff00ff, 0xffff00ff,
|
||||||
|
0xff0000ff, 0x00ffffff, 0x0000ffff, 0x0000ffff,
|
||||||
|
0xff0000ff, 0x00ffffff, 0x0000ffff, 0x0000ffff,
|
||||||
|
};
|
||||||
|
CreateTexture(4, 4, 4, VK_FORMAT_R8G8B8A8_UNORM, testpixels);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mDescriptorSet)
|
||||||
|
{
|
||||||
|
auto fb = GetVulkanFrameBuffer();
|
||||||
|
mDescriptorSet = fb->GetRenderPassManager()->DescriptorPool->allocate(fb->GetRenderPassManager()->TextureSetLayout.get());
|
||||||
|
|
||||||
|
WriteDescriptors update;
|
||||||
|
update.addCombinedImageSampler(mDescriptorSet.get(), 0, mImageView.get(), fb->GetSamplerManager()->Get(0), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
update.updateSets(fb->device);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mDescriptorSet.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels)
|
||||||
|
{
|
||||||
|
auto fb = GetVulkanFrameBuffer();
|
||||||
|
|
||||||
|
int totalSize = w * h * pixelsize;
|
||||||
|
|
||||||
|
BufferBuilder bufbuilder;
|
||||||
|
bufbuilder.setSize(totalSize);
|
||||||
|
bufbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
|
||||||
|
mStagingBuffer = bufbuilder.create(fb->device);
|
||||||
|
|
||||||
|
uint8_t *data = (uint8_t*)mStagingBuffer->Map(0, totalSize);
|
||||||
|
memcpy(data, pixels, totalSize);
|
||||||
|
mStagingBuffer->Unmap();
|
||||||
|
|
||||||
|
ImageBuilder imgbuilder;
|
||||||
|
imgbuilder.setFormat(format);
|
||||||
|
imgbuilder.setSize(w, h, GetMipLevels(w, h));
|
||||||
|
imgbuilder.setUsage(VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
|
||||||
|
mImage = imgbuilder.create(fb->device);
|
||||||
|
|
||||||
|
ImageViewBuilder viewbuilder;
|
||||||
|
viewbuilder.setImage(mImage.get(), format);
|
||||||
|
mImageView = viewbuilder.create(fb->device);
|
||||||
|
|
||||||
|
auto cmdbuffer = fb->GetUploadCommands();
|
||||||
|
|
||||||
|
PipelineBarrier imageTransition0;
|
||||||
|
imageTransition0.addImage(mImage.get(), VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_ACCESS_TRANSFER_WRITE_BIT);
|
||||||
|
imageTransition0.execute(cmdbuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
|
||||||
|
|
||||||
|
VkBufferImageCopy region = {};
|
||||||
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
region.imageSubresource.layerCount = 1;
|
||||||
|
region.imageExtent.depth = 1;
|
||||||
|
region.imageExtent.width = w;
|
||||||
|
region.imageExtent.height = h;
|
||||||
|
cmdbuffer->copyBufferToImage(mStagingBuffer->buffer, mImage->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||||
|
|
||||||
|
GenerateMipmaps(mImage.get(), cmdbuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VkHardwareTexture::GenerateMipmaps(VulkanImage *image, VulkanCommandBuffer *cmdbuffer)
|
||||||
|
{
|
||||||
|
int mipWidth = image->width;
|
||||||
|
int mipHeight = image->height;
|
||||||
|
int i;
|
||||||
|
for (i = 1; mipWidth > 1 || mipHeight > 1; i++)
|
||||||
|
{
|
||||||
|
PipelineBarrier barrier0;
|
||||||
|
barrier0.addImage(image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_ASPECT_COLOR_BIT, i - 1);
|
||||||
|
barrier0.execute(cmdbuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
|
||||||
|
|
||||||
|
int nextWidth = std::max(mipWidth >> 1, 1);
|
||||||
|
int nextHeight = std::max(mipHeight >> 1, 1);
|
||||||
|
|
||||||
|
VkImageBlit blit = {};
|
||||||
|
blit.srcOffsets[0] = { 0, 0, 0 };
|
||||||
|
blit.srcOffsets[1] = { mipWidth, mipHeight, 1 };
|
||||||
|
blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
blit.srcSubresource.mipLevel = i - 1;
|
||||||
|
blit.srcSubresource.baseArrayLayer = 0;
|
||||||
|
blit.srcSubresource.layerCount = 1;
|
||||||
|
blit.dstOffsets[0] = { 0, 0, 0 };
|
||||||
|
blit.dstOffsets[1] = { nextWidth, nextHeight, 1 };
|
||||||
|
blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
blit.dstSubresource.mipLevel = i;
|
||||||
|
blit.dstSubresource.baseArrayLayer = 0;
|
||||||
|
blit.dstSubresource.layerCount = 1;
|
||||||
|
cmdbuffer->blitImage(image->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, image->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_LINEAR);
|
||||||
|
|
||||||
|
PipelineBarrier barrier1;
|
||||||
|
barrier1.addImage(image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_ASPECT_COLOR_BIT, i - 1);
|
||||||
|
barrier1.execute(cmdbuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
||||||
|
|
||||||
|
mipWidth = nextWidth;
|
||||||
|
mipHeight = nextHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
PipelineBarrier barrier2;
|
||||||
|
barrier2.addImage(image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_ASPECT_COLOR_BIT, i - 1);
|
||||||
|
barrier2.execute(cmdbuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
int VkHardwareTexture::GetMipLevels(int w, int h)
|
||||||
|
{
|
||||||
|
int levels = 1;
|
||||||
|
while (w > 1 || h > 1)
|
||||||
|
{
|
||||||
|
w = std::max(w >> 1, 1);
|
||||||
|
h = std::max(h >> 1, 1);
|
||||||
|
levels++;
|
||||||
|
}
|
||||||
|
return levels;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
//
|
//
|
||||||
// Creates the low level texture object
|
// Creates the low level texture object
|
||||||
|
@ -40,7 +185,6 @@
|
||||||
|
|
||||||
VkResult VkHardwareTexture::CreateTexture(unsigned char * buffer, int w, int h, bool mipmap, int translation)
|
VkResult VkHardwareTexture::CreateTexture(unsigned char * buffer, int w, int h, bool mipmap, int translation)
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
int rh,rw;
|
int rh,rw;
|
||||||
bool deletebuffer=false;
|
bool deletebuffer=false;
|
||||||
auto tTex = GetTexID(translation);
|
auto tTex = GetTexID(translation);
|
||||||
|
@ -72,9 +216,6 @@ VkResult VkHardwareTexture::CreateTexture(unsigned char * buffer, int w, int h,
|
||||||
tTex->vkTexture = nullptr;
|
tTex->vkTexture = nullptr;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
#else
|
|
||||||
return VK_ERROR_INITIALIZATION_FAILED;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
|
@ -204,7 +345,6 @@ VkHardwareTexture::TranslatedTexture *VkHardwareTexture::GetTexID(int translatio
|
||||||
|
|
||||||
VkTexture *VkHardwareTexture::GetVkTexture(FTexture *tex, int translation, bool needmipmap, int flags)
|
VkTexture *VkHardwareTexture::GetVkTexture(FTexture *tex, int translation, bool needmipmap, int flags)
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
int usebright = false;
|
int usebright = false;
|
||||||
|
|
||||||
if (translation <= 0)
|
if (translation <= 0)
|
||||||
|
@ -227,6 +367,6 @@ VkTexture *VkHardwareTexture::GetVkTexture(FTexture *tex, int translation, bool
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
return pTex->vkTexture;
|
return pTex->vkTexture;
|
||||||
#endif
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -11,6 +11,40 @@
|
||||||
#include "hwrenderer/textures/hw_ihwtexture.h"
|
#include "hwrenderer/textures/hw_ihwtexture.h"
|
||||||
#include "volk/volk.h"
|
#include "volk/volk.h"
|
||||||
|
|
||||||
|
struct FMaterialState;
|
||||||
|
class VulkanDescriptorSet;
|
||||||
|
class VulkanImage;
|
||||||
|
class VulkanImageView;
|
||||||
|
class VulkanBuffer;
|
||||||
|
|
||||||
|
class VkHardwareTexture : public IHardwareTexture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VkHardwareTexture();
|
||||||
|
~VkHardwareTexture();
|
||||||
|
|
||||||
|
void Reset();
|
||||||
|
|
||||||
|
VulkanDescriptorSet *GetDescriptorSet(const FMaterialState &state);
|
||||||
|
|
||||||
|
// Software renderer stuff
|
||||||
|
void AllocateBuffer(int w, int h, int texelsize) override { }
|
||||||
|
uint8_t *MapBuffer() override { return nullptr; }
|
||||||
|
unsigned int CreateTexture(unsigned char * buffer, int w, int h, int texunit, bool mipmap, int translation, const char *name) override { return 0; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels);
|
||||||
|
void GenerateMipmaps(VulkanImage *image, VulkanCommandBuffer *cmdbuffer);
|
||||||
|
static int GetMipLevels(int w, int h);
|
||||||
|
|
||||||
|
std::unique_ptr<VulkanDescriptorSet> mDescriptorSet;
|
||||||
|
std::unique_ptr<VulkanImage> mImage;
|
||||||
|
std::unique_ptr<VulkanImageView> mImageView;
|
||||||
|
std::unique_ptr<VulkanBuffer> mStagingBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
class FCanvasTexture;
|
class FCanvasTexture;
|
||||||
class AActor;
|
class AActor;
|
||||||
class VkTexture;
|
class VkTexture;
|
||||||
|
@ -71,3 +105,4 @@ public:
|
||||||
VkTexture *GetVkTexture(FTexture *tex, int translation, bool needmipmap, int flags);
|
VkTexture *GetVkTexture(FTexture *tex, int translation, bool needmipmap, int flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue