1
0
mirror of https://github.com/Yours3lf/rpi-vk-driver.git synced 2025-01-18 10:52:14 +01:00

updated triangle to use vbo

This commit is contained in:
Unknown 2018-09-08 17:53:51 +01:00
parent ceab6234cd
commit 9174ad6898
2 changed files with 227 additions and 5 deletions

View File

@ -326,3 +326,88 @@ uint32_t packVec4IntoABGR8(const float rgba[4])
util_format_write_4f(format, rgba, 0, uc, 0, 0, 0, 1, 1);
}
}*/
void vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator)
{
}
void vkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, VkSubpassContents contents)
{
}
void vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline)
{
}
void vkCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports)
{
}
void vkCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount, const VkRect2D* pScissors)
{
}
void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
{
}
void vkCmdEndRenderPass(VkCommandBuffer commandBuffer)
{
}
VkResult vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass)
{
return VK_SUCCESS;
}
VkResult vkCreateImageView(VkDevice device, const VkImageViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkImageView* pView)
{
return VK_SUCCESS;
}
VkResult vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer)
{
return VK_SUCCESS;
}
VkResult vkCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule)
{
return VK_SUCCESS;
}
VkResult vkCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, const VkGraphicsPipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
{
return VK_SUCCESS;
}

View File

@ -21,13 +21,15 @@
#define WINDOW_HEIGHT 480
const char* fragShader =
"#version 400\n"
"layout(location = 0) out vec4 out_Color;\n"
"void main() { out_Color = vec4( 0.0, 0.4, 1.0, 1.0 ); }";
"#version 100\n"
"precision mediump float;\n"
"void main() { gl_FragColor = vec4(0.8, 0.3, 0.5, 1.0); }\n";
const char* vertShader =
"#version 400\n"
"void main() { vec2 pos[3] = vec2[3]( vec2(-0.7, 0.7), vec2(0.7, 0.7), vec2(0.0, -0.7) ); gl_Position = vec4( pos[gl_VertexIndex], 0.0, 1.0 ); }";
"#version 100\n"
"precision highp float;\n"
"attribute vec2 vertex;\n"
"void main(){ gl_Position = vec4(vertex, 0, 1); }\n";
// Note: support swap chain recreation (not only required for resized windows!)
// Note: window resize may not result in Vulkan telling that the swap chain should be recreated, should be handled explicitly!
@ -49,6 +51,7 @@ void CreateRenderPass();
void CreateFramebuffer();
void CreateShaders();
void CreatePipeline();
void CreateVertexBuffer();
void recordCommandBuffers();
VkSurfaceFormatKHR chooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& surfaceCapabilities);
@ -71,6 +74,9 @@ VkShaderModule fsModule; //
VkPipeline pipeline; //
VkQueue graphicsQueue;
VkQueue presentQueue;
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
VkPhysicalDeviceMemoryProperties pdmp;
std::vector<VkImageView> views; //?
uint32_t graphicsQueueFamily;
@ -142,6 +148,7 @@ void setupVulkan() {
CreateFramebuffer();
CreateShaders();
CreatePipeline();
CreateVertexBuffer();
recordCommandBuffers();
}
@ -641,6 +648,9 @@ void recordCommandBuffers()
vkCmdSetScissor(presentCommandBuffers[i], 0, 1, &scissor);
VkDeviceSize offsets = 0;
vkCmdBindVertexBuffers(presentCommandBuffers[i], 0, 1, &vertexBuffer, &offsets );
vkCmdDraw(presentCommandBuffers[i], 3, 1, 0, 0);
vkCmdEndRenderPass(presentCommandBuffers[i]);
@ -887,6 +897,133 @@ void CreatePipeline()
printf("Graphics pipeline created\n");
}
uint32_t getMemoryTypeIndex(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, uint32_t typeBits, VkMemoryPropertyFlags properties)
{
// Iterate over all memory types available for the device used in this example
for (uint32_t i = 0; i < deviceMemoryProperties.memoryTypeCount; i++)
{
if ((typeBits & 1) == 1)
{
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
{
return i;
}
}
typeBits >>= 1;
}
assert(0);
}
void CreateVertexBuffer()
{
unsigned vboSize = sizeof(float) * 2 * 3; //3 x vec2
VkMemoryRequirements mr;
VkBuffer stagingVertexBuffer;
VkDeviceMemory stagingVertexBufferMemory;
{ //create staging buffer
VkBufferCreateInfo stagingci = {};
stagingci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
stagingci.size = vboSize;
stagingci.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
VkResult res = vkCreateBuffer(device, &stagingci, 0, &stagingVertexBuffer);
vkGetBufferMemoryRequirements(device, stagingVertexBuffer, &mr);
VkMemoryAllocateInfo stagingmai = {};
stagingmai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
stagingmai.allocationSize = mr.size;
stagingmai.memoryTypeIndex = getMemoryTypeIndex(pdmp, mr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
res = vkAllocateMemory(device, &stagingmai, 0, &stagingVertexBufferMemory);
float vertices[] =
{
-1, -1,
1, -1,
0, 1
};
void* data;
res = vkMapMemory(device, stagingVertexBufferMemory, 0, mr.size, 0, &data);
memcpy(data, vertices, mr.size);
vkUnmapMemory(device, stagingVertexBufferMemory);
res = vkBindBufferMemory(device, stagingVertexBuffer, stagingVertexBufferMemory, 0);
}
{ //final vertex buffer
VkBufferCreateInfo ci = {};
ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
ci.size = vboSize;
ci.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
VkResult res = vkCreateBuffer(device, &ci, 0, &vertexBuffer);
VkMemoryAllocateInfo mai = {};
mai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
mai.allocationSize = mr.size;
mai.memoryTypeIndex = getMemoryTypeIndex(pdmp, mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
res = vkAllocateMemory(device, &mai, 0, &vertexBufferMemory);
res = vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);
}
{ //copy from staging to final
VkCommandBuffer cmdBuffer;
VkCommandBufferAllocateInfo cmdBufAllocateInfo = {};
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufAllocateInfo.commandPool = commandPool;
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufAllocateInfo.commandBufferCount = 1;
VkResult res = vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &cmdBuffer);
// If requested, also start the new command buffer
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
res = vkBeginCommandBuffer(cmdBuffer, &beginInfo);
VkBufferCopy copyRegion = {};
copyRegion.size = vboSize;
vkCmdCopyBuffer(cmdBuffer, stagingVertexBuffer, vertexBuffer, 1, &copyRegion);
res = vkEndCommandBuffer(cmdBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdBuffer;
// Create fence to ensure that the command buffer has finished executing
VkFenceCreateInfo fenceCreateInfo = {};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceCreateInfo.flags = 0;
VkFence fence;
res = vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);
// Submit to the queue
res = vkQueueSubmit(graphicsQueue, 1, &submitInfo, fence);
// Wait for the fence to signal that command buffer has finished executing
res = vkWaitForFences(device, 1, &fence, VK_TRUE, -1);
vkDestroyFence(device, fence, nullptr);
vkFreeCommandBuffers(device, commandPool, 1, &cmdBuffer);
//clean up staging
vkDestroyBuffer(device, stagingVertexBuffer, 0);
vkFreeMemory(device, stagingVertexBufferMemory, 0);
}
printf("Vertex buffer created\n");
}
int main() {
// Note: dynamically loading loader may be a better idea to fail gracefully when Vulkan is not supported