Skip to content

Commit 2b10474

Browse files
committed
Use V2 in RendererAPI_Vulkan
1 parent c0576db commit 2b10474

File tree

1 file changed

+129
-126
lines changed

1 file changed

+129
-126
lines changed

PluginSource/source/RenderAPI_Vulkan.cpp

Lines changed: 129 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ static PFN_vkGetInstanceProcAddr UNITY_INTERFACE_API InterceptVulkanInitializati
132132

133133
extern "C" void RenderAPI_Vulkan_OnPluginLoad(IUnityInterfaces* interfaces)
134134
{
135-
interfaces->Get<IUnityGraphicsVulkan>()->InterceptInitialization(InterceptVulkanInitialization, NULL);
135+
if (IUnityGraphicsVulkanV2* vulkanInterface = interfaces->Get<IUnityGraphicsVulkanV2>())
136+
vulkanInterface->AddInterceptInitialization(InterceptVulkanInitialization, NULL, 0);
137+
else if (IUnityGraphicsVulkan* vulkanInterface = interfaces->Get<IUnityGraphicsVulkan>())
138+
vulkanInterface->InterceptInitialization(InterceptVulkanInitialization, NULL);
136139
}
137140

138141
struct VulkanBuffer
@@ -151,35 +154,35 @@ static VkPipelineLayout CreateTrianglePipelineLayout(VkDevice device)
151154
pushConstantRange.offset = 0;
152155
pushConstantRange.size = 64; // single matrix
153156
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
154-
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {};
155-
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
156-
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
157-
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
158-
159-
VkPipelineLayout pipelineLayout;
157+
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {};
158+
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
159+
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
160+
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
161+
162+
VkPipelineLayout pipelineLayout;
160163
return vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, NULL, &pipelineLayout) == VK_SUCCESS ? pipelineLayout : VK_NULL_HANDLE;
161164
}
162165

163166
namespace Shader {
164167
// Source of vertex shader (filename: shader.vert)
165168
/*
166-
#version 310 es
167-
layout(location = 0) in highp vec3 vpos;
168-
layout(location = 1) in highp vec4 vcol;
169-
layout(location = 0) out highp vec4 color;
170-
layout(push_constant) uniform PushConstants { mat4 matrix; };
171-
void main() {
172-
gl_Position = matrix * vec4(vpos, 1.0);
173-
color = vcol;
169+
#version 310 es
170+
layout(location = 0) in highp vec3 vpos;
171+
layout(location = 1) in highp vec4 vcol;
172+
layout(location = 0) out highp vec4 color;
173+
layout(push_constant) uniform PushConstants { mat4 matrix; };
174+
void main() {
175+
gl_Position = matrix * vec4(vpos, 1.0);
176+
color = vcol;
174177
}
175178
*/
176179

177180
// Source of fragment shader (filename: shader.frag)
178181
/*
179-
#version 310 es
180-
layout(location = 0) out highp vec4 fragColor;
181-
layout(location = 0) in highp vec4 color;
182-
void main() { fragColor = color; }
182+
#version 310 es
183+
layout(location = 0) out highp vec4 fragColor;
184+
layout(location = 0) in highp vec4 color;
185+
void main() { fragColor = color; }
183186
*/
184187
// compiled to SPIR-V using:
185188
// %VULKAN_SDK%\bin\glslc -mfmt=num shader.frag shader.vert -c
@@ -302,131 +305,131 @@ static VkPipeline CreateTrianglePipeline(VkDevice device, VkPipelineLayout pipel
302305
bool success = true;
303306
VkGraphicsPipelineCreateInfo pipelineCreateInfo = {};
304307

305-
VkPipelineShaderStageCreateInfo shaderStages[2] = {};
306-
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
307-
shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
308-
shaderStages[0].pName = "main";
309-
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
310-
shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
308+
VkPipelineShaderStageCreateInfo shaderStages[2] = {};
309+
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
310+
shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
311+
shaderStages[0].pName = "main";
312+
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
313+
shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
311314
shaderStages[1].pName = "main";
312315

313316
if (success)
314317
{
315-
VkShaderModuleCreateInfo moduleCreateInfo = {};
316-
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
317-
moduleCreateInfo.codeSize = sizeof(Shader::vertexShaderSpirv);
318-
moduleCreateInfo.pCode = Shader::vertexShaderSpirv;
318+
VkShaderModuleCreateInfo moduleCreateInfo = {};
319+
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
320+
moduleCreateInfo.codeSize = sizeof(Shader::vertexShaderSpirv);
321+
moduleCreateInfo.pCode = Shader::vertexShaderSpirv;
319322
success = vkCreateShaderModule(device, &moduleCreateInfo, NULL, &shaderStages[0].module) == VK_SUCCESS;
320323
}
321324

322325
if (success)
323326
{
324-
VkShaderModuleCreateInfo moduleCreateInfo = {};
325-
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
326-
moduleCreateInfo.codeSize = sizeof(Shader::fragmentShaderSpirv);
327-
moduleCreateInfo.pCode = Shader::fragmentShaderSpirv;
327+
VkShaderModuleCreateInfo moduleCreateInfo = {};
328+
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
329+
moduleCreateInfo.codeSize = sizeof(Shader::fragmentShaderSpirv);
330+
moduleCreateInfo.pCode = Shader::fragmentShaderSpirv;
328331
success = vkCreateShaderModule(device, &moduleCreateInfo, NULL, &shaderStages[1].module) == VK_SUCCESS;
329332
}
330333

331334
VkPipeline pipeline;
332335
if (success)
333336
{
334-
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
335-
pipelineCreateInfo.layout = pipelineLayout;
337+
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
338+
pipelineCreateInfo.layout = pipelineLayout;
336339
pipelineCreateInfo.renderPass = renderPass;
337340

338-
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = {};
339-
inputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
340-
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
341-
342-
VkPipelineRasterizationStateCreateInfo rasterizationState = {};
343-
rasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
344-
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
345-
rasterizationState.cullMode = VK_CULL_MODE_NONE;
346-
rasterizationState.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
347-
rasterizationState.depthClampEnable = VK_FALSE;
348-
rasterizationState.rasterizerDiscardEnable = VK_FALSE;
349-
rasterizationState.depthBiasEnable = VK_FALSE;
350-
rasterizationState.lineWidth = 1.0f;
351-
352-
VkPipelineColorBlendAttachmentState blendAttachmentState[1] = {};
353-
blendAttachmentState[0].colorWriteMask = 0xf;
354-
blendAttachmentState[0].blendEnable = VK_FALSE;
355-
VkPipelineColorBlendStateCreateInfo colorBlendState = {};
356-
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
357-
colorBlendState.attachmentCount = 1;
358-
colorBlendState.pAttachments = blendAttachmentState;
359-
360-
VkPipelineViewportStateCreateInfo viewportState = {};
361-
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
362-
viewportState.viewportCount = 1;
363-
viewportState.scissorCount = 1;
364-
365-
const VkDynamicState dynamicStateEnables[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
366-
VkPipelineDynamicStateCreateInfo dynamicState = {};
367-
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
368-
dynamicState.pDynamicStates = dynamicStateEnables;
369-
dynamicState.dynamicStateCount = sizeof(dynamicStateEnables) / sizeof(*dynamicStateEnables);
370-
371-
VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
372-
depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
373-
depthStencilState.depthTestEnable = VK_TRUE;
374-
depthStencilState.depthWriteEnable = VK_TRUE;
375-
depthStencilState.depthBoundsTestEnable = VK_FALSE;
376-
depthStencilState.stencilTestEnable = VK_FALSE;
377-
depthStencilState.depthCompareOp = VK_COMPARE_OP_GREATER_OR_EQUAL; // Unity/Vulkan uses reverse Z
378-
depthStencilState.back.failOp = VK_STENCIL_OP_KEEP;
379-
depthStencilState.back.passOp = VK_STENCIL_OP_KEEP;
380-
depthStencilState.back.compareOp = VK_COMPARE_OP_ALWAYS;
381-
depthStencilState.front = depthStencilState.back;
382-
383-
VkPipelineMultisampleStateCreateInfo multisampleState = {};
384-
multisampleState.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
385-
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
386-
multisampleState.pSampleMask = NULL;
387-
388-
// Vertex:
389-
// float3 vpos;
390-
// byte4 vcol;
391-
VkVertexInputBindingDescription vertexInputBinding = {};
392-
vertexInputBinding.binding = 0;
393-
vertexInputBinding.stride = 16;
394-
vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
395-
396-
VkVertexInputAttributeDescription vertexInputAttributes[2];
397-
vertexInputAttributes[0].binding = 0;
398-
vertexInputAttributes[0].location = 0;
399-
vertexInputAttributes[0].format = VK_FORMAT_R32G32B32_SFLOAT;
400-
vertexInputAttributes[0].offset = 0;
401-
vertexInputAttributes[1].binding = 0;
402-
vertexInputAttributes[1].location = 1;
403-
vertexInputAttributes[1].format = VK_FORMAT_R8G8B8A8_UNORM;
404-
vertexInputAttributes[1].offset = 12;
405-
406-
VkPipelineVertexInputStateCreateInfo vertexInputState = {};
407-
vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
408-
vertexInputState.vertexBindingDescriptionCount = 1;
409-
vertexInputState.pVertexBindingDescriptions = &vertexInputBinding;
410-
vertexInputState.vertexAttributeDescriptionCount = 2;
411-
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes;
412-
413-
pipelineCreateInfo.stageCount = sizeof(shaderStages) / sizeof(*shaderStages);
414-
pipelineCreateInfo.pStages = shaderStages;
415-
pipelineCreateInfo.pVertexInputState = &vertexInputState;
416-
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
417-
pipelineCreateInfo.pRasterizationState = &rasterizationState;
418-
pipelineCreateInfo.pColorBlendState = &colorBlendState;
419-
pipelineCreateInfo.pMultisampleState = &multisampleState;
420-
pipelineCreateInfo.pViewportState = &viewportState;
421-
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
422-
pipelineCreateInfo.pDynamicState = &dynamicState;
423-
341+
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = {};
342+
inputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
343+
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
344+
345+
VkPipelineRasterizationStateCreateInfo rasterizationState = {};
346+
rasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
347+
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
348+
rasterizationState.cullMode = VK_CULL_MODE_NONE;
349+
rasterizationState.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
350+
rasterizationState.depthClampEnable = VK_FALSE;
351+
rasterizationState.rasterizerDiscardEnable = VK_FALSE;
352+
rasterizationState.depthBiasEnable = VK_FALSE;
353+
rasterizationState.lineWidth = 1.0f;
354+
355+
VkPipelineColorBlendAttachmentState blendAttachmentState[1] = {};
356+
blendAttachmentState[0].colorWriteMask = 0xf;
357+
blendAttachmentState[0].blendEnable = VK_FALSE;
358+
VkPipelineColorBlendStateCreateInfo colorBlendState = {};
359+
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
360+
colorBlendState.attachmentCount = 1;
361+
colorBlendState.pAttachments = blendAttachmentState;
362+
363+
VkPipelineViewportStateCreateInfo viewportState = {};
364+
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
365+
viewportState.viewportCount = 1;
366+
viewportState.scissorCount = 1;
367+
368+
const VkDynamicState dynamicStateEnables[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
369+
VkPipelineDynamicStateCreateInfo dynamicState = {};
370+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
371+
dynamicState.pDynamicStates = dynamicStateEnables;
372+
dynamicState.dynamicStateCount = sizeof(dynamicStateEnables) / sizeof(*dynamicStateEnables);
373+
374+
VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
375+
depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
376+
depthStencilState.depthTestEnable = VK_TRUE;
377+
depthStencilState.depthWriteEnable = VK_TRUE;
378+
depthStencilState.depthBoundsTestEnable = VK_FALSE;
379+
depthStencilState.stencilTestEnable = VK_FALSE;
380+
depthStencilState.depthCompareOp = VK_COMPARE_OP_GREATER_OR_EQUAL; // Unity/Vulkan uses reverse Z
381+
depthStencilState.back.failOp = VK_STENCIL_OP_KEEP;
382+
depthStencilState.back.passOp = VK_STENCIL_OP_KEEP;
383+
depthStencilState.back.compareOp = VK_COMPARE_OP_ALWAYS;
384+
depthStencilState.front = depthStencilState.back;
385+
386+
VkPipelineMultisampleStateCreateInfo multisampleState = {};
387+
multisampleState.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
388+
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
389+
multisampleState.pSampleMask = NULL;
390+
391+
// Vertex:
392+
// float3 vpos;
393+
// byte4 vcol;
394+
VkVertexInputBindingDescription vertexInputBinding = {};
395+
vertexInputBinding.binding = 0;
396+
vertexInputBinding.stride = 16;
397+
vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
398+
399+
VkVertexInputAttributeDescription vertexInputAttributes[2];
400+
vertexInputAttributes[0].binding = 0;
401+
vertexInputAttributes[0].location = 0;
402+
vertexInputAttributes[0].format = VK_FORMAT_R32G32B32_SFLOAT;
403+
vertexInputAttributes[0].offset = 0;
404+
vertexInputAttributes[1].binding = 0;
405+
vertexInputAttributes[1].location = 1;
406+
vertexInputAttributes[1].format = VK_FORMAT_R8G8B8A8_UNORM;
407+
vertexInputAttributes[1].offset = 12;
408+
409+
VkPipelineVertexInputStateCreateInfo vertexInputState = {};
410+
vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
411+
vertexInputState.vertexBindingDescriptionCount = 1;
412+
vertexInputState.pVertexBindingDescriptions = &vertexInputBinding;
413+
vertexInputState.vertexAttributeDescriptionCount = 2;
414+
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes;
415+
416+
pipelineCreateInfo.stageCount = sizeof(shaderStages) / sizeof(*shaderStages);
417+
pipelineCreateInfo.pStages = shaderStages;
418+
pipelineCreateInfo.pVertexInputState = &vertexInputState;
419+
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
420+
pipelineCreateInfo.pRasterizationState = &rasterizationState;
421+
pipelineCreateInfo.pColorBlendState = &colorBlendState;
422+
pipelineCreateInfo.pMultisampleState = &multisampleState;
423+
pipelineCreateInfo.pViewportState = &viewportState;
424+
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
425+
pipelineCreateInfo.pDynamicState = &dynamicState;
426+
424427
success = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, NULL, &pipeline) == VK_SUCCESS;
425-
}
426-
427-
if (shaderStages[0].module != VK_NULL_HANDLE)
428-
vkDestroyShaderModule(device, shaderStages[0].module, NULL);
429-
if (shaderStages[1].module != VK_NULL_HANDLE)
428+
}
429+
430+
if (shaderStages[0].module != VK_NULL_HANDLE)
431+
vkDestroyShaderModule(device, shaderStages[0].module, NULL);
432+
if (shaderStages[1].module != VK_NULL_HANDLE)
430433
vkDestroyShaderModule(device, shaderStages[1].module, NULL);
431434

432435
return success ? pipeline : VK_NULL_HANDLE;

0 commit comments

Comments
 (0)