質問内容
Vulkan API を使ってユニフォームバッファーブロックの配列値を送信したいのですが、単一の変数の場合と違ってうまく送信できません。下記のシェーダーコードでコメントアウトされた//vec3 color = renderColor[0].imageColor;
のほうを有効化した場合では単色が正常に描画されるのですが、vec3 color = renderColor[1].imageColor;
となると画面が黒くなり何も送信できていないものと想定されます。
知りたいこと
ユニフォームバッファーブロック配列のすべての要素に正常に値を送信する方法。
画面に描画する行為は完成しているため、何かしらの形で配列の要素が更新できていないものと推測されます。
確認したこと
- 上記の質問内容部のように
vec3 color = renderColor[0].imageColor;
とすると値が正常に送信され単色が描画されるため、GPUに値を送信すること自体は正常に行われています。 vec3 color = renderColor[1].imageColor;
では現状は真っ黒が描画されます。- ChatGPT, Geminiにて質問
コード内容
4 * 3 * 2 * 2
は、「float
変数(ベクトル成分)一つのバイト数」×「glm::vec3
における要素数」×「ユニフォームブロック一つにおけるglm::vec3
の数」×「ユニフォームブロック配列の要素の数」です。
main
関数はディスクリプタの作成をしてユニフォームバッファーを作成してGPUにアップロードする処理です。
GLSL
#version 460
layout(location = 0) in vec2 in_uv; // 頂点からのUV座標入力
layout(location = 0) out vec4 frag_color; // フラグメントシェーダーからの出力色
layout(set = 0,binding = 0) uniform RenderColor
{
vec3 imageColor;
vec3 addColor;
}renderColor[2];
void main() {
//vec3 color = renderColor[0].imageColor;
vec3 color = renderColor[1].imageColor;
frag_color = vec4(color, 1.0);
}
main()
// ディスクリプタの作成
engine::handle uniformDescriptBindHandle_A = engine::CreateDescriptLayoutBind(0,4 * 3 * 2 * 2,engine::DescriptType::UniformBlock, engine::ShaderType::Fragment);
engine::handle descriptPoolHandle = engine::CreateDescriptPool();
engine::handle uniformDescriptLayoutHandle = engine::CreateDescriptLayout({uniformDescriptBindHandle_A});
struct imageInfo
{
glm::vec3 imageColor;
glm::vec3 addColor;
};
imageInfo info[2] = {};
info[0].imageColor = glm::vec3(0,0,100);
info[1].imageColor = glm::vec3(0,100,0);
//info[0].addColor = glm::vec3(0,100,0);
engine::WriteLog("%d",sizeof(info)); //サイズ確認
engine::handle uniformBufferHandle_A = engine::CreateUniformBuffer(sizeof(imageInfo) * 2);
engine::handle uniformDescriptSetHandle = engine::CreateDescriptSst(descriptPoolHandle, {uniformDescriptLayoutHandle});
engine::UpdateUniformBuffer(uniformBufferHandle_A,info,sizeof(imageInfo) * 2);
engine::UpdateDescriptSet(uniformDescriptSetHandle,uniformBufferHandle_A,info,2,1);
CreateDescriptLayoutBind()
handle CreateDescriptLayoutBind(const unsigned int &bindPoint,const unsigned int &elementCount,const DescriptType descriptType,const ShaderType &shaderStage)
{
internal::handle_manager::handle handle = internal::handle_manager::Create<VkDescriptorSetLayoutBinding>();
VkDescriptorSetLayoutBinding *binding = internal::handle_manager::GetData<VkDescriptorSetLayoutBinding>(handle);
if(descriptType == DescriptType::Image)
{
(*binding).descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; // ディスクリプタのタイプ
(*binding).descriptorCount = elementCount;
}
else if(descriptType == DescriptType::UniformBuffer)
{
(*binding).descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; // ディスクリプタのタイプ
(*binding).descriptorCount = elementCount;
}
else if(descriptType == DescriptType::UniformBlock)
{
(*binding).descriptorType = VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT; // ディスクリプタのタイプ
(*binding).descriptorCount = elementCount;
}
else
{
internal::log_manager::WriteEngine("Undefined descript type: %u",handle);
}
(*binding).binding = bindPoint;
if(shaderStage == ShaderType::Fragment)
{
(*binding).stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
}
else if(shaderStage == ShaderType::Vertex)
{
(*binding).stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
}
return handle;
}
CreateDescriptLayout
handle CreateDescriptLayout(std::vector<handle> descriptLayoutBindList)
{
internal::handle_manager::handle handle = internal::handle_manager::Create<VkDescriptorSetLayout>();
VkDescriptorSetLayout *descriptor = internal::handle_manager::GetData<VkDescriptorSetLayout>(handle);
std::vector<VkDescriptorSetLayoutBinding> bindList;
bindList.resize(descriptLayoutBindList.size());
for (size_t i = 0; i < descriptLayoutBindList.size(); i++)
{
bindList[i] = *internal::handle_manager::GetData<VkDescriptorSetLayoutBinding>(descriptLayoutBindList[i]);
}
VkDescriptorSetLayoutCreateInfo layoutInfo = {};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = bindList.size();
layoutInfo.pBindings = bindList.data();
if (vkCreateDescriptorSetLayout(internal::window::device, &layoutInfo, nullptr, &(*descriptor)) != VK_SUCCESS)
{
internal::log_manager::WriteVulkan("Failed to create descriptor set layout!");
}
return handle;
}
CreateUniformBuffer
handle CreateUniformBuffer(const size_t &bufferSize)
{
internal::handle_manager::handle handle = internal::handle_manager::Create<internal::handle_manager::Buffer>();
internal::handle_manager::Buffer *buffer = internal::handle_manager::GetData<internal::handle_manager::Buffer>(handle);
buffer->size = bufferSize;
// バッファ作成
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = bufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if(vkCreateBuffer(internal::window::device, &bufferInfo, nullptr, &buffer->buffer) != VK_SUCCESS)
{
internal::log_manager::WriteVulkan("Failed to create uniform buffer %u",handle);
exit(EXIT_FAILURE);
}
// メモリ割り当て
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(internal::window::device,buffer->buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = internal::window::GetFindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (vkAllocateMemory(internal::window::device, &allocInfo, nullptr, &buffer->memory) != VK_SUCCESS)
{
internal::log_manager::WriteVulkan("Failed to allocate uniform buffer memory : %u",handle);
exit(EXIT_FAILURE);
}
// メモリにバッファをマッピング
void* data;
vkMapMemory(internal::window::device, buffer->memory, 0, bufferSize, 0, &data);
memcpy(data, &buffer->buffer, bufferSize);
// バッファにデータをコピー
vkBindBufferMemory(internal::window::device,buffer->buffer,buffer->memory,0);
vkUnmapMemory(internal::window::device, buffer->memory);
return handle;
}
UpdateUniformBuffer
void UpdateUniformBuffer(const handle &unifomBufferHandle,const void* data,const size_t &dataSize)
{
internal::handle_manager::Buffer *buffer = internal::handle_manager::GetData<internal::handle_manager::Buffer>(unifomBufferHandle);
void* uniformData;
if(vkMapMemory(internal::window::device,(*buffer).memory, 0,dataSize, 0,&uniformData) != VK_SUCCESS)
{
internal::log_manager::WriteVulkan("Failed to map uniform buffer memory");
}
memcpy(uniformData,data,dataSize);
vkUnmapMemory(internal::window::device,(*buffer).memory);
}
UpdateDescriptSet
void UpdateDescriptSet(const handle &descriptSetHandle,const handle &uniformBuferHandle,const void* data,const size_t &arraySize,const size_t &elementSize)
{
internal::handle_manager::Buffer *buffer = internal::handle_manager::GetData<internal::handle_manager::Buffer>(uniformBuferHandle);
VkDescriptorSet *descriptorSet = internal::handle_manager::GetData<VkDescriptorSet>(descriptSetHandle);
VkWriteDescriptorSetInlineUniformBlock inlineUniformBlock = {};
inlineUniformBlock.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK;
inlineUniformBlock.pNext = nullptr;
inlineUniformBlock.dataSize = 4 * 3 * 2 * 2;
inlineUniformBlock.pData = data; // データへのポインタ
// バッファの更新
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = (*buffer).buffer;
bufferInfo.offset = 0;
bufferInfo.range = 4 * 3 * 2 * 2;
VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = (*descriptorSet);
descriptorWrite.dstBinding = 0;
descriptorWrite.dstArrayElement = 0;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT;
descriptorWrite.descriptorCount = 1;
//descriptorWrite.descriptorCount = 4 * 3 * 2 * 2;
//descriptorWrite.pBufferInfo = nullptr;
descriptorWrite.pBufferInfo = &bufferInfo;
descriptorWrite.pNext = &inlineUniformBlock;
vkUpdateDescriptorSets(internal::window::device, 1, &descriptorWrite, 0, nullptr);