From 3a162118932175e1267cdd306657f662a0c61da7 Mon Sep 17 00:00:00 2001 From: yours3lf <0.tamas.marton@gmail.com> Date: Sun, 10 May 2020 19:03:36 +0100 Subject: [PATCH] fixed clear test --- driver/common.h | 1 + driver/stateChange.c | 57 +++++++++++++++++++++++++++++++----- test/clearTest/clearTest.cpp | 20 ++++++++----- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/driver/common.h b/driver/common.h index eebf42d..a246ace 100644 --- a/driver/common.h +++ b/driver/common.h @@ -145,6 +145,7 @@ typedef struct VkDevice_T VkShaderModule emulTextureToBufferShaderModule; //TODO VkShaderModule emulBufferToBufferShaderModule; //TODO VkShaderModule emulClearShaderModule; + VkShaderModule emulClearNoColorShaderModule; } _device; typedef struct VkRenderPass_T diff --git a/driver/stateChange.c b/driver/stateChange.c index 81f4cb5..fe94e4e 100644 --- a/driver/stateChange.c +++ b/driver/stateChange.c @@ -6,7 +6,7 @@ #include "QPUassembler/qpu_assembler.h" -void createClearShaderModule(VkDevice device, VkShaderModule* blitShaderModule) +void createClearShaderModule(VkDevice device, VkShaderModule* blitShaderModule, VkShaderModule* blitShaderModuleNoColor) { char vs_asm_code[] = ///0x40000000 = 2.0 @@ -118,10 +118,17 @@ void createClearShaderModule(VkDevice device, VkShaderModule* blitShaderModule) "sig_unlock_score ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;" "\0"; - char* blit_asm_strings[] = - { - (char*)cs_asm_code, (char*)vs_asm_code, (char*)fs_asm_code, 0 - }; + char fs_asm_code_no_color[] = + "sig_none ; r0 = or.always(a, a, uni, nop) ; nop = nop(r0, r0) ;" //clear color value + "sig_none ; r1 = or.always(a, a, uni, nop) ; nop = nop(r0, r0) ;" //stencil setup + "sig_none ; r2 = or.always(a, a, uni, nop) ; nop = nop(r0, r0) ;" //depth clear value + "sig_none ; tlb_stencil_setup = or.always(r1, r1) ; nop = nop(r0, r0) ;" + "sig_none ; tlb_z = or.always(r2, r2) ; nop = nop(r0, r0) ;" + ///"sig_none ; tlb_color_all = or.always(r0, r0) ; nop = nop(r0, r0) ;" + "sig_end ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;" + "sig_none ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;" + "sig_unlock_score ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;" + "\0"; VkRpiAssemblyMappingEXT vertexMappings[] = { //vertex shader uniforms @@ -254,6 +261,20 @@ void createClearShaderModule(VkDevice device, VkShaderModule* blitShaderModule) rpi_vkCreateShaderModule(device, &smci, 0, blitShaderModule); assert(*blitShaderModule); + { //assemble fs code + asm_sizes[2] = get_num_instructions(fs_asm_code_no_color); + uint32_t size = sizeof(uint64_t)*asm_sizes[2]; + //TODO this alloc feels kinda useless, we just copy the data anyway to kernel space + //why not map kernel space mem to user space instead? + free(asm_ptrs[2]); + asm_ptrs[2] = (uint64_t*)malloc(size); + assemble_qpu_asm(fs_asm_code_no_color, asm_ptrs[2]); + assert(asm_ptrs[2]); + } + + rpi_vkCreateShaderModule(device, &smci, 0, blitShaderModuleNoColor); + assert(*blitShaderModuleNoColor); + // _shaderModule* s = *blitShaderModule; // fprintf(stderr, "=================\n"); // fprintf(stderr, "pixel shader bo %i\n", s->bos[2]); @@ -392,7 +413,7 @@ void setupClearEmulationResources(VkDevice device) //create resources that won't change _device* dev = device; - createClearShaderModule(device, &dev->emulClearShaderModule); + createClearShaderModule(device, &dev->emulClearShaderModule, &dev->emulClearNoColorShaderModule); createClearDescriptorSetLayouts(device, &dev->emulClearDsl); } @@ -576,6 +597,19 @@ VKAPI_ATTR void VKAPI_CALL rpi_vkCmdClearAttachments( return; } + _pipeline* oldPipeline; + uint32_t oldVertexBufferOffsets[8]; + _buffer* oldVertexBuffers[8]; + char oldPushConstantBufferVertex[256]; + char oldPushConstantBufferPixel[256]; + + //save the state that we'll modify + oldPipeline = cmdBuf->graphicsPipeline; + memcpy(oldVertexBufferOffsets, cmdBuf->vertexBufferOffsets, sizeof(oldVertexBufferOffsets)); + memcpy(oldVertexBuffers, cmdBuf->vertexBuffers, sizeof(oldVertexBuffers)); + memcpy(oldPushConstantBufferVertex, cmdBuf->pushConstantBufferVertex, sizeof(oldPushConstantBufferVertex)); + memcpy(oldPushConstantBufferPixel, cmdBuf->pushConstantBufferPixel, sizeof(oldPushConstantBufferPixel)); + for(uint32_t c = 0; c < attachmentCount; ++c) { uint32_t clearColor = 0, clearDepth = 0, clearStencil = 0; @@ -613,7 +647,7 @@ VKAPI_ATTR void VKAPI_CALL rpi_vkCmdClearAttachments( dsci.front.passOp = VK_STENCIL_OP_REPLACE; dsci.back = dsci.front; - createClearPipeline(device, &dsci, device->emulClearShaderModule, device->emulClearDsl, &blitPipelineLayout, cmdBuf->currRenderPass, &blitPipeline); + createClearPipeline(device, &dsci, clearColor ? device->emulClearShaderModule : device->emulClearNoColorShaderModule, device->emulClearDsl, &blitPipelineLayout, cmdBuf->currRenderPass, &blitPipeline); // _shaderModule* s = device->emulClearShaderModule; // fprintf(stderr, "=================\n"); @@ -629,7 +663,7 @@ VKAPI_ATTR void VKAPI_CALL rpi_vkCmdClearAttachments( uint32_t clearColorValue = 0, stencilSetup = 0, depthClearValue = 0; clearColorValue = packVec4IntoABGR8(&pAttachments[c].clearValue.color.float32[0]); - depthClearValue = (uint32_t)(pAttachments[c].clearValue.depthStencil.depth * 0xffffff) & 0xffffff; + depthClearValue = (uint32_t)(pAttachments[c].clearValue.depthStencil.depth * 0xffffffu) & 0xffffffu; uint32_t numValues = 1; encodeStencilValue(&stencilSetup, &numValues, dsci.front, dsci.back, clearStencil); @@ -673,6 +707,13 @@ VKAPI_ATTR void VKAPI_CALL rpi_vkCmdClearAttachments( rpi_vkDestroyPipelineLayout(device, blitPipelineLayout, 0); rpi_vkDestroyPipeline(device, blitPipeline, 0); } + + //restore state + cmdBuf->graphicsPipeline = oldPipeline; + memcpy(cmdBuf->vertexBufferOffsets, oldVertexBufferOffsets, sizeof(oldVertexBufferOffsets)); + memcpy(cmdBuf->vertexBuffers, oldVertexBuffers, sizeof(oldVertexBuffers)); + memcpy(cmdBuf->pushConstantBufferVertex, oldPushConstantBufferVertex, sizeof(oldPushConstantBufferVertex)); + memcpy(cmdBuf->pushConstantBufferPixel, oldPushConstantBufferPixel, sizeof(oldPushConstantBufferPixel)); } /* diff --git a/test/clearTest/clearTest.cpp b/test/clearTest/clearTest.cpp index 5a745b2..e25f62b 100644 --- a/test/clearTest/clearTest.cpp +++ b/test/clearTest/clearTest.cpp @@ -825,19 +825,25 @@ void recordCommandBuffers() uint32_t fragColor = 0xffa14ccc; //purple vkCmdPushConstants(presentCommandBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(fragColor), &fragColor); - VkDeviceSize offsets = 0; - vkCmdBindVertexBuffers(presentCommandBuffers[i], 0, 1, &vertexBuffer1, &offsets ); - vkCmdDraw(presentCommandBuffers[i], 3, 1, 0, 0); + VkClearAttachment clearAttachment[2] = {}; + clearAttachment[0].aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; + clearAttachment[0].clearValue.depthStencil.depth = 0.3f; - VkClearAttachment clearAttachment = {}; - clearAttachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; - clearAttachment.clearValue.depthStencil.depth = 0.0f; + clearAttachment[1].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + clearAttachment[1].clearValue.color.float32[0] = 1.0f; + clearAttachment[1].clearValue.color.float32[1] = 1.0f; + clearAttachment[1].clearValue.color.float32[2] = 1.0f; + clearAttachment[1].clearValue.color.float32[3] = 1.0f; VkClearRect clearRect = {}; clearRect.rect.offset.x = 0; clearRect.rect.offset.y = 0; clearRect.rect.extent.width = swapChainExtent.width; clearRect.rect.extent.height = swapChainExtent.height; - vkCmdClearAttachments(presentCommandBuffers[i], 1, &clearAttachment, 1, &clearRect); + vkCmdClearAttachments(presentCommandBuffers[i], 2, clearAttachment, 1, &clearRect); + + VkDeviceSize offsets = 0; + vkCmdBindVertexBuffers(presentCommandBuffers[i], 0, 1, &vertexBuffer1, &offsets ); + vkCmdDraw(presentCommandBuffers[i], 3, 1, 0, 0); fragColor = 0xffafcd02; //yellow vkCmdPushConstants(presentCommandBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(fragColor), &fragColor);