From 67c25b0b41129ee2ae95c373ca6279883994b1ae Mon Sep 17 00:00:00 2001 From: Unknown <0.tamas.marton@gmail.com> Date: Mon, 23 Sep 2019 20:27:07 +0100 Subject: [PATCH] indexed draw example works --- driver/draw.c | 47 ++++++++++++++++++++---- test/indexedTriangle/indexedTriangle.cpp | 45 ++++++++++++++++++++++- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/driver/draw.c b/driver/draw.c index 6faa12b..46af3cd 100644 --- a/driver/draw.c +++ b/driver/draw.c @@ -2,7 +2,8 @@ #include "kernel/vc4_packet.h" -static void drawCommon(VkCommandBuffer commandBuffer) +//returns max index +static uint32_t drawCommon(VkCommandBuffer commandBuffer) { assert(commandBuffer); @@ -207,10 +208,27 @@ static void drawCommon(VkCommandBuffer commandBuffer) coordCode //coordinate shader code address ); + uint32_t maxIndex = 0xffff; for(uint32_t c = 0 ; c < cb->graphicsPipeline->vertexAttributeDescriptionCount; ++c) { if(cb->vertexBuffers[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding]) { + uint32_t formatByteSize = getFormatByteSize(cb->graphicsPipeline->vertexAttributeDescriptions[c].format); + + //TODO the indexing here is incorrect.... + uint32_t stride = cb->graphicsPipeline->vertexBindingDescriptions[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding].stride; + + if(stride > 0) + { + //TODO offset + uint32_t usedIndices = (cb->vertexBuffers[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding]->boundMem->size - formatByteSize) / stride; + + if(usedIndices < maxIndex) + { + maxIndex = usedIndices; + } + } + ControlListAddress vertexBuffer = { .handle = cb->vertexBuffers[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding]->boundMem->bo, .offset = cb->graphicsPipeline->vertexAttributeDescriptions[c].offset, @@ -223,8 +241,8 @@ static void drawCommon(VkCommandBuffer commandBuffer) cb->binCl.currMarker->handlesBuf, cb->binCl.currMarker->handlesSize, vertexBuffer, //reloc address - getFormatByteSize(cb->graphicsPipeline->vertexAttributeDescriptions[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding].format), - cb->graphicsPipeline->vertexBindingDescriptions[cb->graphicsPipeline->vertexAttributeDescriptions[c].binding].stride, //stride + formatByteSize, + stride, cb->graphicsPipeline->vertexAttributeDescriptions[c].offset, //vertex vpm offset cb->graphicsPipeline->vertexAttributeDescriptions[c].offset //coordinte vpm offset ); @@ -389,6 +407,9 @@ static void drawCommon(VkCommandBuffer commandBuffer) } } } + + + return maxIndex; } /* @@ -398,6 +419,12 @@ void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t ins { assert(commandBuffer); + if(instanceCount != 1 || firstInstance != 0) + { + unsigned instancing; + UNSUPPORTED(instancing); + } + drawCommon(commandBuffer); _commandBuffer* cb = commandBuffer; @@ -419,19 +446,25 @@ VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexed( { assert(commandBuffer); - drawCommon(commandBuffer); + if(instanceCount != 1 || firstInstance != 0) + { + unsigned instancing; + UNSUPPORTED(instancing); + } + + uint32_t maxIndex = drawCommon(commandBuffer); _commandBuffer* cb = commandBuffer; clFit(commandBuffer, &commandBuffer->handlesCl, 4); - uint32_t idx = clGetHandleIndex(&commandBuffer->handlesCl, cb->binCl.currMarker->handlesBuf, cb->binCl.currMarker->handlesSize, cb->indexBuffer); + uint32_t idx = clGetHandleIndex(&commandBuffer->handlesCl, cb->binCl.currMarker->handlesBuf, cb->binCl.currMarker->handlesSize, cb->indexBuffer->boundMem->bo); - clInsertGEMRelocations(&commandBuffer->binCl, cb->indexBuffer->boundMem->bo, 0); + clInsertGEMRelocations(&commandBuffer->binCl, idx, 0); //Submit draw call: vertex Array Primitives clFit(commandBuffer, &commandBuffer->binCl, V3D21_VERTEX_ARRAY_PRIMITIVES_length); clInsertIndexedPrimitiveList(&commandBuffer->binCl, - 0xffff, //TODO max index? + maxIndex, //max index 0, //TODO we can pass an offset here indexCount, 1, //we only support 16 bit indices diff --git a/test/indexedTriangle/indexedTriangle.cpp b/test/indexedTriangle/indexedTriangle.cpp index 78ec64a..6a5c33b 100644 --- a/test/indexedTriangle/indexedTriangle.cpp +++ b/test/indexedTriangle/indexedTriangle.cpp @@ -67,6 +67,8 @@ VkQueue graphicsQueue; VkQueue presentQueue; VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; +VkBuffer indexBuffer; +VkDeviceMemory indexBufferMemory; VkPhysicalDeviceMemoryProperties pdmp; std::vector views; //? VkSurfaceFormatKHR swapchainFormat; @@ -647,6 +649,8 @@ void recordCommandBuffers() VkDeviceSize offsets = 0; vkCmdBindVertexBuffers(presentCommandBuffers[i], 0, 1, &vertexBuffer, &offsets ); + vkCmdBindIndexBuffer(presentCommandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16); + float Wcoeff = 1.0f; //1.0f / Wc = 2.0 - Wcoeff float viewportScaleX = (float)(swapChainExtent.width) * 0.5f * 16.0f; float viewportScaleY = -1.0f * (float)(swapChainExtent.height) * 0.5f * 16.0f; @@ -660,7 +664,7 @@ void recordCommandBuffers() vkCmdPushConstants(presentCommandBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(pushConstants), &pushConstants); - vkCmdDraw(presentCommandBuffers[i], 3, 1, 0, 0); + vkCmdDrawIndexed(presentCommandBuffers[i], 3, 1, 0, 0, 0); vkCmdEndRenderPass(presentCommandBuffers[i]); @@ -1063,7 +1067,7 @@ void CreatePipeline() VkPipelineRasterizationStateCreateInfo rastCreateInfo = {}; rastCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rastCreateInfo.polygonMode = VK_POLYGON_MODE_FILL; - rastCreateInfo.cullMode = VK_CULL_MODE_NONE; + rastCreateInfo.cullMode = VK_CULL_MODE_BACK_BIT; rastCreateInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rastCreateInfo.lineWidth = 1.0f; @@ -1124,6 +1128,7 @@ uint32_t getMemoryTypeIndex(VkPhysicalDeviceMemoryProperties deviceMemoryPropert void CreateVertexBuffer() { unsigned vboSize = sizeof(float) * 2 * 3; //3 x vec2 + unsigned iboSize = sizeof(uint16_t) * 3; //3 x ushort VkMemoryRequirements mr; @@ -1146,6 +1151,12 @@ void CreateVertexBuffer() float vertices[] = { + //ccw order + //0, 1, + //1, -1, + //-1, -1 + + //cw order -1, -1, 1, -1, 0, 1 @@ -1159,6 +1170,36 @@ void CreateVertexBuffer() res = vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0); } + { //create staging buffer + VkBufferCreateInfo ci = {}; + ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; + ci.size = iboSize; + ci.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT; + + VkResult res = vkCreateBuffer(device, &ci, 0, &indexBuffer); + + vkGetBufferMemoryRequirements(device, indexBuffer, &mr); + + VkMemoryAllocateInfo mai = {}; + mai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + mai.allocationSize = mr.size; + mai.memoryTypeIndex = getMemoryTypeIndex(pdmp, mr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + + res = vkAllocateMemory(device, &mai, 0, &indexBufferMemory); + + uint16_t indices[] = + { + 2, 1, 0 + }; + + void* data; + res = vkMapMemory(device, indexBufferMemory, 0, mr.size, 0, &data); + memcpy(data, indices, iboSize); + vkUnmapMemory(device, indexBufferMemory); + + res = vkBindBufferMemory(device, indexBuffer, indexBufferMemory, 0); + } + printf("Vertex buffer created\n"); }