设置字体颜色,Vulkan

我是新的使用Vulkan,我试图学习它看教程,并看他们提供的vulkan例子(vulkan-master: https : //github.com/SaschaWillems/Vulkan )。

其中之一是Overlaytext项目。 在这个项目中,他们教你如何在Vulkan中实现重叠文本。

使用这个代码,我想改变它来添加一个额外的统一,以便能够改变字体的颜色。 问题是统一没有被设置为颜色,总是一个0 vec4(0,0,0,0),我没有得到任何警告或错误。

如果你能告诉我我错过了什么,我会非常感激,我不知道该怎么办,我没有想法。

我添加或改变了这个项目来添加这个制服的代码如下:

课堂上的变数:

glm::vec4 _color = glm::vec4(1.0f,1.0f,1.0f,1.0f); struct { VkBuffer buffer; VkDeviceMemory memory; 

} _colorBuffer;

input管道的绑定和属性(preparePipeline方法):

 std::array<VkVertexInputBindingDescription, 3> vertexBindings = {}; vertexBindings[0] = vertexInputBindingDescription(0, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX); vertexBindings[1] = vertexInputBindingDescription(1, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX); vertexBindings[2] = vertexInputBindingDescription(2, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX); std::array<VkVertexInputAttributeDescription, 3> vertexAttribs = {}; // Position vertexAttribs[0] = vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, 0); // UV vertexAttribs[1] = vertexInputAttributeDescription(1, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(glm::vec2)); // Color vertexAttribs[2] = vertexInputAttributeDescription(2, 2, VK_FORMAT_R32G32B32A32_SFLOAT, 0); 

添加starging缓冲区以将vec4颜色的内容复制到本地设备缓冲区(prepareResources方法):

 struct { VkBuffer buffer; VkDeviceMemory memory; } colorStaging; ErrorCheck(renderer->CreateBuffer( VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, sizeof(glm::vec4), &colorStaging.buffer, &colorStaging.memory, &_color)); ErrorCheck(renderer->CreateBuffer( VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, sizeof(glm::vec4), &_colorBuffer.buffer, &_colorBuffer.memory)); 

将暂存缓冲区复制到colorBuffer统一(prepareResources,紧跟在setImageLayout方法之后,结束命令缓冲区之前):

 VkBufferCopy copyRegion = {}; copyRegion.size = sizeof(glm::vec4); vkCmdCopyBuffer( copyCmd, colorStaging.buffer, _colorBuffer.buffer, 1, &copyRegion); 

清理stagingBuffer.destroy()和vkFreeCommandBuffers(_device,_command_pool,1,&copyCmd)之间的临时缓冲区;在prepareResources方法中):

 vkDestroyBuffer(renderer->GetVulkanDevice(), colorStaging.buffer, nullptr); vkFreeMemory(renderer->GetVulkanDevice(), colorStaging.memory, nullptr); 

将新绑定顶点添加到更新命令缓冲区(更新命令缓冲区方法,紧挨着其他绑定):

 vkCmdBindVertexBuffers(_commandBuffers[i], 2, 1, &_colorBuffer.buffer, &offsets); 

顶点着色器:

 #version 450 core layout (location = 0) in vec2 inPos; layout (location = 1) in vec2 inUV; layout (location = 2) in vec4 color; layout (location = 0) out vec2 outUV; layout (location = 1) out vec4 outColor; out gl_PerVertex { vec4 gl_Position; }; void main(void) { gl_Position = vec4(inPos, 0.0, 1.0); outUV = inUV; outColor = color; } 

片段着色器:

 #version 450 core layout (location = 0) in vec2 inUV; layout (location = 1) in vec4 inColor; layout (binding = 0) uniform sampler2D samplerFont; layout (location = 0) out vec4 outFragColor; void main(void) { float color = texture(samplerFont, inUV).r; outFragColor = inColor*color; } 

我重新编译了两个着色器。

如果我没有添加意味着我没有改变它。

感谢您的关注。

我试图使用一个input顶点,而不是一个常量推动vec4。 我已经改变了实施使用一个不断推动,它的工作原理。