Skip to content

Commit

Permalink
- implemented support for paletted textures to Vulkan backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Apr 5, 2021
1 parent 869433e commit 8ea13f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion source/common/rendering/vulkan/system/vk_framebuffer.cpp
Expand Up @@ -390,7 +390,7 @@ void VulkanFrameBuffer::PrecacheMaterial(FMaterial *mat, int translation)

IHardwareTexture *VulkanFrameBuffer::CreateHardwareTexture(int numchannels)
{
return new VkHardwareTexture();
return new VkHardwareTexture(numchannels);
}

FMaterial* VulkanFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags)
Expand Down
36 changes: 24 additions & 12 deletions source/common/rendering/vulkan/textures/vk_hwtexture.cpp
Expand Up @@ -37,8 +37,9 @@

VkHardwareTexture *VkHardwareTexture::First = nullptr;

VkHardwareTexture::VkHardwareTexture()
VkHardwareTexture::VkHardwareTexture(int numchannels)
{
mTexelsize = numchannels;
Next = First;
First = this;
if (Next) Next->Prev = this;
Expand Down Expand Up @@ -126,7 +127,8 @@ void VkHardwareTexture::CreateImage(FTexture *tex, int translation, int flags)
if (!tex->isHardwareCanvas())
{
FTextureBuffer texbuffer = tex->CreateTexBuffer(translation, flags | CTF_ProcessData);
CreateTexture(texbuffer.mWidth, texbuffer.mHeight, 4, VK_FORMAT_B8G8R8A8_UNORM, texbuffer.mBuffer);
bool indexed = flags & CTF_Indexed;
CreateTexture(texbuffer.mWidth, texbuffer.mHeight,indexed? 1 : 4, indexed? VK_FORMAT_R8_UNORM : VK_FORMAT_B8G8R8A8_UNORM, texbuffer.mBuffer, !indexed);
}
else
{
Expand Down Expand Up @@ -156,7 +158,7 @@ void VkHardwareTexture::CreateImage(FTexture *tex, int translation, int flags)
}
}

void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels)
void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels, bool mipmap)
{
if (w <= 0 || h <= 0)
throw CVulkanError("Trying to create zero size texture");
Expand All @@ -177,7 +179,7 @@ void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat form

ImageBuilder imgbuilder;
imgbuilder.setFormat(format);
imgbuilder.setSize(w, h, GetMipLevels(w, h));
imgbuilder.setSize(w, h, !mipmap ? 1 : GetMipLevels(w, h));
imgbuilder.setUsage(VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
mImage.Image = imgbuilder.create(fb->device);
mImage.Image->SetDebugName("VkHardwareTexture.mImage");
Expand All @@ -203,7 +205,7 @@ void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat form

fb->FrameDeleteList.Buffers.push_back(std::move(stagingBuffer));

mImage.GenerateMipmaps(cmdbuffer);
if (mipmap) mImage.GenerateMipmaps(cmdbuffer);
}

int VkHardwareTexture::GetMipLevels(int w, int h)
Expand Down Expand Up @@ -268,6 +270,7 @@ uint8_t *VkHardwareTexture::MapBuffer()

unsigned int VkHardwareTexture::CreateTexture(unsigned char * buffer, int w, int h, int texunit, bool mipmap, const char *name)
{
CreateTexture(w, h, mTexelsize, mTexelsize == 4 ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_R8_UNORM, buffer, mipmap);
return 0;
}

Expand Down Expand Up @@ -371,10 +374,6 @@ VulkanDescriptorSet* VkMaterial::GetDescriptorSet(const FMaterialState& state)
int clampmode = state.mClampMode;
int translation = state.mTranslation;

auto remap = translation <= 0 ? nullptr : GPalette.TranslationToTable(translation);
if (remap)
translation = remap->Index;

clampmode = base->GetClampMode(clampmode);

for (auto& set : mDescriptorSets)
Expand All @@ -395,10 +394,23 @@ VulkanDescriptorSet* VkMaterial::GetDescriptorSet(const FMaterialState& state)
MaterialLayerInfo *layer;
auto systex = static_cast<VkHardwareTexture*>(GetLayer(0, state.mTranslation, &layer));
update.addCombinedImageSampler(descriptor.get(), 0, systex->GetImage(layer->layerTexture, state.mTranslation, layer->scaleFlags)->View.get(), sampler, systex->mImage.Layout);
for (int i = 1; i < numLayers; i++)

if (!(layer->scaleFlags & CTF_Indexed))
{
auto systex = static_cast<VkHardwareTexture*>(GetLayer(i, 0, &layer));
update.addCombinedImageSampler(descriptor.get(), i, systex->GetImage(layer->layerTexture, 0, layer->scaleFlags)->View.get(), sampler, systex->mImage.Layout);
for (int i = 1; i < numLayers; i++)
{
auto systex = static_cast<VkHardwareTexture*>(GetLayer(i, 0, &layer));
update.addCombinedImageSampler(descriptor.get(), i, systex->GetImage(layer->layerTexture, 0, layer->scaleFlags)->View.get(), sampler, systex->mImage.Layout);
}
}
else
{
for (int i = 1; i < 3; i++)
{
auto systex = static_cast<VkHardwareTexture*>(GetLayer(i, translation, &layer));
update.addCombinedImageSampler(descriptor.get(), i, systex->GetImage(layer->layerTexture, 0, layer->scaleFlags)->View.get(), sampler, systex->mImage.Layout);
}
numLayers = 3;
}

auto dummyImage = fb->GetRenderPassManager()->GetNullTextureView();
Expand Down
4 changes: 2 additions & 2 deletions source/common/rendering/vulkan/textures/vk_hwtexture.h
Expand Up @@ -24,7 +24,7 @@ class VkHardwareTexture : public IHardwareTexture
{
friend class VkMaterial;
public:
VkHardwareTexture();
VkHardwareTexture(int numchannels);
~VkHardwareTexture();

static void ResetAll();
Expand All @@ -45,7 +45,7 @@ class VkHardwareTexture : public IHardwareTexture
private:
void CreateImage(FTexture *tex, int translation, int flags);

void CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels);
void CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels, bool mipmap);
static int GetMipLevels(int w, int h);

static VkHardwareTexture *First;
Expand Down

0 comments on commit 8ea13f7

Please sign in to comment.