144 lines
3.8 KiB
C++
144 lines
3.8 KiB
C++
#include "vulkan_state.h"
|
|
CVkMaterial::CVkMaterial( IShader *pShader )
|
|
{
|
|
m_pVkShader = (CVkShader*)pShader;
|
|
|
|
VkDescriptorPoolSize pools[4] =
|
|
{
|
|
{
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
128,
|
|
},
|
|
{
|
|
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
128,
|
|
},
|
|
{
|
|
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
128,
|
|
},
|
|
{
|
|
VK_DESCRIPTOR_TYPE_SAMPLER,
|
|
1,
|
|
}
|
|
};
|
|
VkDescriptorPoolCreateInfo stPool = {};
|
|
stPool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
|
stPool.poolSizeCount = 4;
|
|
stPool.pPoolSizes = pools;
|
|
stPool.maxSets = 16;
|
|
stPool.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT;
|
|
vkCreateDescriptorPool(m_pVkShader->m_hDevice, &stPool, NULL, &m_hPool);
|
|
|
|
VkDescriptorSetAllocateInfo stInfo = {};
|
|
stInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
|
stInfo.descriptorPool = m_hPool;
|
|
stInfo.descriptorSetCount = m_pVkShader->m_setLayouts.GetSize();
|
|
stInfo.pSetLayouts = m_pVkShader->m_setLayouts.GetData();
|
|
m_hSets.Resize(m_pVkShader->m_setLayouts.GetSize());
|
|
vkAllocateDescriptorSets(m_pVkShader->m_hDevice, &stInfo, m_hSets.GetData());
|
|
}
|
|
|
|
CVkMaterial::~CVkMaterial()
|
|
{
|
|
|
|
}
|
|
|
|
void CVkMaterial::Frame()
|
|
{
|
|
vkUpdateDescriptorSets(m_pVkShader->m_hDevice, m_writes.GetSize(), m_writes.GetData(), 0, 0);
|
|
};
|
|
|
|
void CVkMaterial::VSSetShaderResource( uint32_t uRegister, IRenderingObject *pResource )
|
|
{
|
|
SetShaderResource(uRegister, 0, pResource);
|
|
}
|
|
|
|
void CVkMaterial::PSSetShaderResource( uint32_t uRegister, IRenderingObject *pResource )
|
|
{
|
|
SetShaderResource(uRegister, 0, pResource);
|
|
}
|
|
|
|
void CVkMaterial::VSSetConstantsBuffer( uint32_t uRegister, IBuffer *pConstants )
|
|
{
|
|
SetShaderResource(uRegister, 0, pConstants);
|
|
}
|
|
|
|
void CVkMaterial::PSSetConstantsBuffer( uint32_t uRegister, IBuffer *pConstants )
|
|
{
|
|
SetShaderResource(uRegister, 0, pConstants);
|
|
}
|
|
|
|
void CVkMaterial::PSSetTextureArray( uint32_t uSet, ITextureArray *pArray )
|
|
{
|
|
SetShaderResource(0, uSet, pArray);
|
|
}
|
|
|
|
void CVkMaterial::SetShaderResource( uint32_t uRegister, uint32_t uSet, IRenderingObject *pObject)
|
|
{
|
|
union {
|
|
IRenderingObject *pVkObject;
|
|
CVkBuffer *pBuffer;
|
|
CVkTextureArray *pArray;
|
|
};
|
|
pVkObject = pObject;
|
|
if (dynamic_cast<IBuffer*>(pObject))
|
|
{
|
|
VkWriteDescriptorSet write = {};
|
|
VkDescriptorBufferInfo stInfo = {};
|
|
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
write.dstSet = m_hSets[uSet];
|
|
write.dstBinding = uRegister;
|
|
write.dstArrayElement = 0;
|
|
write.descriptorType = pBuffer->m_eDescriptorType;
|
|
write.descriptorCount = 1;
|
|
write.pBufferInfo = &stInfo;
|
|
stInfo.buffer = pBuffer->m_buffer;
|
|
stInfo.range = pBuffer->m_nSize;
|
|
vkUpdateDescriptorSets(m_pVkShader->m_hDevice, 1, &write, 0, 0);
|
|
}
|
|
if (dynamic_cast<ITextureArray*>(pObject))
|
|
{
|
|
VkWriteDescriptorSet writes[2] = {};
|
|
VkDescriptorImageInfo stWrites[128];
|
|
VkDescriptorImageInfo stSampler = {};
|
|
|
|
for ( int i = 0; i < 128; i++ )
|
|
{
|
|
if (pArray->m_pImages[i] == NULL)
|
|
{
|
|
stWrites[i] = {
|
|
.imageView = pArray->m_pImages[0]->m_imageView,
|
|
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
};
|
|
continue;
|
|
}
|
|
stWrites[i] = {
|
|
.imageView = pArray->m_pImages[i]->m_imageView,
|
|
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
};
|
|
}
|
|
|
|
writes[0] = {};
|
|
writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
writes[0].dstSet = m_hSets[uSet];
|
|
writes[0].dstBinding = 0;
|
|
writes[0].dstArrayElement = 0;
|
|
writes[0].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
|
|
writes[0].descriptorCount = 1;
|
|
writes[0].pImageInfo = &stSampler;
|
|
stSampler.sampler = pArray->m_hSampler;
|
|
|
|
writes[1] = {};
|
|
writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
writes[1].dstSet = m_hSets[uSet];
|
|
writes[1].dstBinding = 1;
|
|
writes[1].dstArrayElement = 0;
|
|
writes[1].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
|
|
writes[1].descriptorCount = 128;
|
|
writes[1].pImageInfo = stWrites;
|
|
vkUpdateDescriptorSets(m_pVkShader->m_hDevice, 2, writes, 0, 0);
|
|
}
|
|
}
|
|
|