1
0
mirror of https://github.com/Yours3lf/rpi-vk-driver.git synced 2025-02-19 16:54:18 +01:00

stencil example now works

This commit is contained in:
Unknown 2019-09-23 17:51:31 +01:00
parent f191969b48
commit 52a04ab6a1
4 changed files with 205 additions and 9 deletions

View File

@ -401,7 +401,7 @@ int isDepthStencilFormat(VkFormat format)
}
}
uint32_t getDepthCompareOp(VkCompareOp op)
uint32_t getCompareOp(VkCompareOp op)
{
switch(op)
{
@ -426,6 +426,31 @@ uint32_t getDepthCompareOp(VkCompareOp op)
}
}
uint32_t getStencilOp(VkStencilOp op)
{
switch(op)
{
case VK_STENCIL_OP_ZERO:
return 0;
case VK_STENCIL_OP_KEEP:
return 1;
case VK_STENCIL_OP_REPLACE:
return 2;
case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
return 3;
case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
return 4;
case VK_STENCIL_OP_INVERT:
return 5;
case VK_STENCIL_OP_INCREMENT_AND_WRAP:
return 6;
case VK_STENCIL_OP_DECREMENT_AND_WRAP:
return 7;
default:
return -1;
};
}
uint32_t getTopology(VkPrimitiveTopology topology)
{
switch(topology)
@ -632,6 +657,126 @@ void encodeTextureUniform(uint32_t* params, //array of 4 uint32_t
params[3] = 0;
}
void encodeDepthStencilValue(uint32_t *values, uint32_t* numValues, VkStencilOpState front, VkStencilOpState back)
{
assert(values);
assert(numValues);
if(front.compareMask == back.compareMask &&
front.compareOp == back.compareOp &&
front.depthFailOp == back.depthFailOp &&
front.failOp == back.failOp &&
front.passOp == back.passOp &&
front.reference == back.reference &&
front.writeMask == back.writeMask
)
{
*numValues = 1;
values[0] = 0
| (front.compareMask & 0xff)
| (front.reference & 0xff) << 0x8
| (getCompareOp(front.compareOp) & 0x3) << 16
| (getStencilOp(front.failOp) & 0x3) << 19
| (getStencilOp(front.passOp) & 0x3) << 22
| (getStencilOp(front.depthFailOp) & 0x3) << 25
| 3 << 30; //front and back
switch(front.writeMask)
{
case 0x1:
values[0] |= 0 << 28;
break;
case 0x3:
values[0] |= 1 << 28;
break;
case 0xf:
values[0] |= 2 << 28;
break;
case 0xff:
values[0] |= 3 << 28;
break;
default:
values[1] = 0
| front.writeMask & 0xff
| (front.writeMask & 0xff) << 8;
*numValues = 2;
break;
};
}
else
{
*numValues = 2;
values[0] = 0
| (front.compareMask & 0xff)
| (front.reference & 0xff) << 0x8
| (getCompareOp(front.compareOp) & 0x3) << 16
| (getStencilOp(front.failOp) & 0x3) << 19
| (getStencilOp(front.passOp) & 0x3) << 22
| (getStencilOp(front.depthFailOp) & 0x3) << 25
| 1 << 30; //front
values[1] = 0
| (back.compareMask & 0xff)
| (back.reference & 0xff) << 0x8
| (getCompareOp(back.compareOp) & 0x3) << 16
| (getStencilOp(back.failOp) & 0x3) << 19
| (getStencilOp(back.passOp) & 0x3) << 22
| (getStencilOp(back.depthFailOp) & 0x3) << 25
| 2 << 30; //front
if((front.writeMask == 0x1 ||
front.writeMask == 0x3 ||
front.writeMask == 0xf ||
front.writeMask == 0xff) &&
(back.writeMask == 0x1 ||
back.writeMask == 0x3 ||
back.writeMask == 0xf ||
back.writeMask == 0xff))
{
switch(front.writeMask)
{
case 0x1:
values[0] |= 0 << 28;
break;
case 0x3:
values[0] |= 1 << 28;
break;
case 0xf:
values[0] |= 2 << 28;
break;
case 0xff:
values[0] |= 3 << 28;
break;
};
switch(back.writeMask)
{
case 0x1:
values[1] |= 0 << 28;
break;
case 0x3:
values[1] |= 1 << 28;
break;
case 0xf:
values[1] |= 2 << 28;
break;
case 0xff:
values[1] |= 3 << 28;
break;
};
}
else
{
values[2] = 0
| front.writeMask & 0xff
| (back.writeMask & 0xff) << 8;
*numValues = 3;
}
}
}
uint8_t getTextureDataType(VkFormat format)
{
switch(format)

View File

@ -458,7 +458,8 @@ int findInstanceExtension(char* name);
int findDeviceExtension(char* name);
void getPaddedTextureDimensionsT(uint32_t width, uint32_t height, uint32_t bpp, uint32_t* paddedWidth, uint32_t* paddedHeight);
int isDepthStencilFormat(VkFormat format);
uint32_t getDepthCompareOp(VkCompareOp op);
uint32_t getCompareOp(VkCompareOp op);
uint32_t getStencilOp(VkStencilOp op);
uint32_t getTopology(VkPrimitiveTopology topology);
uint32_t getPrimitiveMode(VkPrimitiveTopology topology);
uint32_t getFormatByteSize(VkFormat format);
@ -476,6 +477,7 @@ void encodeTextureUniform(uint32_t* params,
uint8_t wrapT,
uint8_t wrapS,
uint8_t noAutoLod);
void encodeDepthStencilValue(uint32_t* values, uint32_t* numValues, VkStencilOpState front, VkStencilOpState back);
uint8_t getTextureDataType(VkFormat format);
uint8_t getMinFilterType(VkFilter minFilter, VkSamplerMipmapMode mipFilter, float maxLod);
uint8_t getWrapMode(VkSamplerAddressMode mode);

View File

@ -88,7 +88,7 @@ void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t ins
1, //earlyz updates enable
1, //earlyz enable
cb->graphicsPipeline->depthWriteEnable, //z updates enable
cb->graphicsPipeline->depthTestEnable ? getDepthCompareOp(cb->graphicsPipeline->depthCompareOp) : V3D_COMPARE_FUNC_ALWAYS, //depth compare func
cb->graphicsPipeline->depthTestEnable ? getCompareOp(cb->graphicsPipeline->depthCompareOp) : V3D_COMPARE_FUNC_ALWAYS, //depth compare func
0, //coverage read mode
0, //coverage pipe select
0, //coverage update mode

View File

@ -63,7 +63,8 @@ std::vector<VkCommandBuffer> presentCommandBuffers; //
std::vector<VkImage> swapChainImages; //
VkRenderPass renderPass; //
std::vector<VkFramebuffer> fbs; //
VkShaderModule shaderModule; //
VkShaderModule shaderModule1; //
VkShaderModule shaderModule2; //
VkPipeline pipeline1; //
VkPipeline pipeline2; //
VkQueue graphicsQueue;
@ -103,7 +104,7 @@ void cleanup() {
vkDestroyRenderPass(device, renderPass, 0);
vkDestroyShaderModule(device, shaderModule, 0);
//vkDestroyShaderModule(device, shaderModule, 0);
//vkDestroyPipeline(device, pipeline, 0);
@ -1087,7 +1088,47 @@ void CreateShaders()
//display a color
char fs_asm_code[] =
"sig_none ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;"
///stencil setup state
/// 28 24 20 16 12 8 4 0
///1111 0100 1001 0111 1110 1110 1111 1111
/// -> 0xF497EEFF
///selection = front and back (0x3)
///write mask = 0xff
///z test fail op = replace (0x2)
///z test pass op = replace (0x2)
///stencil test fail op = replace (0x2)
///stencil function = always (0x7)
///stencil ref value = 0xee
///stencil function mask = 0xff
"sig_load_imm ; r0 = load32.always(0xF497EEFF) ; nop = load32() ;" //stencil setup state
"sig_none ; tlb_stencil_setup = or.always(r0, r0) ; nop = nop(r0, r0) ;"
"sig_none ; tlb_z = or.always(b, b, nop, rb15) ; nop = nop(r0, r0) ;"
///omit color write
"sig_none ; r0 = or.always(a, a, uni, nop) ; 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";
/**/
/**/
//display a color
char fs_asm_code2[] =
"sig_none ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;"
///stencil setup state
/// 28 24 20 16 12 8 4 0
///1111 0010 0100 1101 1110 1110 1111 1111
/// -> 0xF24DEEFF
///selection = front and back (0x3)
///write mask = 0xff
///z test fail op = keep (0x1)
///z test pass op = keep (0x1)
///stencil test fail op = keep (0x1)
///stencil function = not equal (0x5)
///stencil ref value = 0xee
///stencil function mask = 0xff
"sig_load_imm ; r0 = load32.always(0xF24DEEFF) ; nop = load32() ;" //stencil setup state
"sig_none ; tlb_stencil_setup = or.always(r0, r0) ; nop = nop(r0, r0) ;"
"sig_none ; tlb_z = or.always(b, b, nop, rb15) ; nop = nop(r0, r0) ;"
"sig_none ; tlb_color_all = or.always(a, a, uni, nop) ; nop = nop(r0, r0) ;"
"sig_end ; nop = nop(r0, r0) ; nop = nop(r0, r0) ;"
@ -1164,8 +1205,13 @@ void CreateShaders()
shaderModuleCreateInfo.mappings = mappings;
shaderModuleCreateInfo.numMappings = sizeof(mappings) / sizeof(VkRpiAssemblyMappingEXT);
VkResult res = vkCreateShaderModuleFromRpiAssemblyEXT(device, &shaderModuleCreateInfo, 0, &shaderModule);
assert(shaderModule);
VkResult res = vkCreateShaderModuleFromRpiAssemblyEXT(device, &shaderModuleCreateInfo, 0, &shaderModule1);
assert(shaderModule1);
asm_strings[2] = (char*)fs_asm_code2;
res = vkCreateShaderModuleFromRpiAssemblyEXT(device, &shaderModuleCreateInfo, 0, &shaderModule2);
assert(shaderModule2);
}
@ -1194,11 +1240,11 @@ void CreatePipeline()
shaderStageCreateInfo[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfo[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
shaderStageCreateInfo[0].module = shaderModule;
shaderStageCreateInfo[0].module = shaderModule1;
shaderStageCreateInfo[0].pName = "main";
shaderStageCreateInfo[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfo[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStageCreateInfo[1].module = shaderModule;
shaderStageCreateInfo[1].module = shaderModule1;
shaderStageCreateInfo[1].pName = "main";
VkVertexInputBindingDescription vertexInputBindingDescription =
@ -1297,6 +1343,9 @@ void CreatePipeline()
blendAttachState.colorWriteMask = 0xf;
shaderStageCreateInfo[0].module = shaderModule2;
shaderStageCreateInfo[1].module = shaderModule2;
res = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &pipeline2);
printf("Graphics pipeline created\n");