宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

文章目录

  • 前言
  • 一、CPP文件
    • 1.类和变量
    • 2.重要函数
  • 二、Shader
    • 1.offscreen 阴影贴图渲染
    • 2.scene模型渲染
    • 3.quad 深度渲染
  • 总结

前言

  • 为定向光源渲染阴影。第一遍存储来自光的 pov 的深度值,第二遍与这些值进行比较以检查片段是否被阴影。使用深度偏差来避免阴影伪影,并应用 PCF 过滤器来平滑阴影边缘。
  • 核心原理和OpenGL中一样,只不过使用vulkanAPI。
  • 阴影映射(Shadow Mapping)背后的思路非常简单:我们以光的位置为视角进行渲染,我们能看到的东西都将被点亮,看不见的一定是在阴影之中了。假设有一个地板,在光源和它之间有一个大盒子。由于光源处向光线方向看去,可以看到这个盒子,但看不到地板的一部分,这部分就应该在阴影中了。
  • 本例子特意添加了光源的深度视角来对物体进行观察。

一、CPP文件

1.类和变量

class VulkanExample : public VulkanExampleBase
{
public:bool displayShadowMap = false;bool filterPCF = true;// Keep depth range as small as possible// for better shadow map precisionfloat zNear = 1.0f;float zFar = 96.0f;// Depth bias (and slope) are used to avoid shadowing artifacts//(深度偏置(和斜率)用于避免阴影伪像)// Constant depth bias factor (always applied)//恒定深度偏置因子(始终应用)float depthBiasConstant = 1.25f;// Slope depth bias factor, applied depending on polygon's slope//坡度深度偏置因子,根据多边形的坡度应用float depthBiasSlope = 1.75f;glm::vec3 lightPos = glm::vec3();//光源位置float lightFOV = 45.0f;std::vector<vkglTF::Model> scenes;std::vector<std::string> sceneNames;int32_t sceneIndex = 0;struct {vks::Buffer scene;vks::Buffer offscreen;} uniformBuffers;//设置UBOstruct {glm::mat4 projection;glm::mat4 view;glm::mat4 model;glm::mat4 depthBiasMVP;glm::vec4 lightPos;// Used for depth map visualizationfloat zNear;float zFar;} uboVSscene;//用于光源视角观察深度的离屏信息struct {glm::mat4 depthMVP;} uboOffscreenVS;//多个渲染管线用于渲染struct {VkPipeline offscreen;VkPipeline sceneShadow;VkPipeline sceneShadowPCF;VkPipeline debug;} pipelines;VkPipelineLayout pipelineLayout;struct {VkDescriptorSet offscreen;VkDescriptorSet scene;VkDescriptorSet debug;} descriptorSets;VkDescriptorSetLayout descriptorSetLayout;// Framebuffer for offscreen rendering// 用于屏幕外渲染的帧缓冲器struct FrameBufferAttachment {VkImage image;VkDeviceMemory mem;VkImageView view;};struct OffscreenPass {int32_t width, height;VkFramebuffer frameBuffer;FrameBufferAttachment depth;VkRenderPass renderPass;VkSampler depthSampler;VkDescriptorImageInfo descriptor;} offscreenPass;

2.重要函数

1)为屏幕外帧缓冲区设置单独的渲染通道。这是必需的,因为屏幕外帧缓冲区附件使用的格式与示例中的格式不同

	void prepareOffscreenRenderpass(){VkAttachmentDescription attachmentDescription{};attachmentDescription.format = DEPTH_FORMAT;attachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;attachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;							// Clear depth at beginning of the render passattachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;						// We will read from depth, so it's important to store the depth attachment resultsattachmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;attachmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;attachmentDescription.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;					// We don't care about initial layout of the attachmentattachmentDescription.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;// Attachment will be transitioned to shader read at render pass endVkAttachmentReference depthReference = {};depthReference.attachment = 0;depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;			// Attachment will be used as depth/stencil during render passVkSubpassDescription subpass = {};subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;subpass.colorAttachmentCount = 0;													// No color attachmentssubpass.pDepthStencilAttachment = &depthReference;									// Reference to our depth attachment// Use subpass dependencies for layout transitionsstd::array<VkSubpassDependency, 2> dependencies;dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;dependencies[0].dstSubpass = 0;dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;dependencies[0].dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;dependencies[0].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;dependencies[1].srcSubpass = 0;dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;dependencies[1].srcStageMask = VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;dependencies[1].srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;VkRenderPassCreateInfo renderPassCreateInfo = vks::initializers::renderPassCreateInfo();renderPassCreateInfo.attachmentCount = 1;renderPassCreateInfo.pAttachments = &attachmentDescription;renderPassCreateInfo.subpassCount = 1;renderPassCreateInfo.pSubpasses = &subpass;renderPassCreateInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());renderPassCreateInfo.pDependencies = dependencies.data();VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &offscreenPass.renderPass));}

2)设置屏幕外帧缓冲器,用于将场景从光源的视点渲染。然后,此帧缓冲器的深度连接将用于从阴影通道的片段着色器中采样。

	void prepareOffscreenFramebuffer(){offscreenPass.width = SHADOWMAP_DIM;offscreenPass.height = SHADOWMAP_DIM;// For shadow mapping we only need a depth attachmentVkImageCreateInfo image = vks::initializers::imageCreateInfo();image.imageType = VK_IMAGE_TYPE_2D;image.extent.width = offscreenPass.width;image.extent.height = offscreenPass.height;image.extent.depth = 1;image.mipLevels = 1;image.arrayLayers = 1;image.samples = VK_SAMPLE_COUNT_1_BIT;image.tiling = VK_IMAGE_TILING_OPTIMAL;image.format = DEPTH_FORMAT;																// Depth stencil attachmentimage.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;		// We will sample directly from the depth attachment for the shadow mappingVK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.depth.image));VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();VkMemoryRequirements memReqs;vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs);memAlloc.allocationSize = memReqs.size;memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem));VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0));VkImageViewCreateInfo depthStencilView = vks::initializers::imageViewCreateInfo();depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;depthStencilView.format = DEPTH_FORMAT;depthStencilView.subresourceRange = {};depthStencilView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;depthStencilView.subresourceRange.baseMipLevel = 0;depthStencilView.subresourceRange.levelCount = 1;depthStencilView.subresourceRange.baseArrayLayer = 0;depthStencilView.subresourceRange.layerCount = 1;depthStencilView.image = offscreenPass.depth.image;VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));// Create sampler to sample from to depth attachment// Used to sample in the fragment shader for shadowed renderingVkFilter shadowmap_filter = vks::tools::formatIsFilterable(physicalDevice, DEPTH_FORMAT, VK_IMAGE_TILING_OPTIMAL) ?DEFAULT_SHADOWMAP_FILTER :VK_FILTER_NEAREST;VkSamplerCreateInfo sampler = vks::initializers::samplerCreateInfo();sampler.magFilter = shadowmap_filter;sampler.minFilter = shadowmap_filter;sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;sampler.addressModeV = sampler.addressModeU;sampler.addressModeW = sampler.addressModeU;sampler.mipLodBias = 0.0f;sampler.maxAnisotropy = 1.0f;sampler.minLod = 0.0f;sampler.maxLod = 1.0f;sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offscreenPass.depthSampler));prepareOffscreenRenderpass();// Create frame bufferVkFramebufferCreateInfo fbufCreateInfo = vks::initializers::framebufferCreateInfo();fbufCreateInfo.renderPass = offscreenPass.renderPass;fbufCreateInfo.attachmentCount = 1;fbufCreateInfo.pAttachments = &offscreenPass.depth.view;fbufCreateInfo.width = offscreenPass.width;fbufCreateInfo.height = offscreenPass.height;fbufCreateInfo.layers = 1;VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offscreenPass.frameBuffer));}

3)该示例中的创建命令缓冲:注意用到了两个渲染通道,第一个用于生成阴影贴图,而选择是否用光源视角进行渲染则在第二个中。

	void buildCommandBuffers(){VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();VkClearValue clearValues[2];VkViewport viewport;VkRect2D scissor;for (int32_t i = 0; i < drawCmdBuffers.size(); ++i){VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));/*First render pass: Generate shadow map by rendering the scene from light's POV(第一个渲染通道:通过从光源的 POV 渲染场景来生成阴影贴图)*/{clearValues[0].depthStencil = { 1.0f, 0 };VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();renderPassBeginInfo.renderPass = offscreenPass.renderPass;renderPassBeginInfo.framebuffer = offscreenPass.frameBuffer;renderPassBeginInfo.renderArea.extent.width = offscreenPass.width;renderPassBeginInfo.renderArea.extent.height = offscreenPass.height;renderPassBeginInfo.clearValueCount = 1;renderPassBeginInfo.pClearValues = clearValues;vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);viewport = vks::initializers::viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f);vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);scissor = vks::initializers::rect2D(offscreenPass.width, offscreenPass.height, 0, 0);vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);// Set depth bias (aka "Polygon offset")// Required to avoid shadow mapping artifactsvkCmdSetDepthBias(drawCmdBuffers[i],depthBiasConstant,0.0f,depthBiasSlope);vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.offscreen, 0, nullptr);scenes[sceneIndex].draw(drawCmdBuffers[i]);vkCmdEndRenderPass(drawCmdBuffers[i]);}//Note: Explicit synchronization is not required between the render pass, as this is done implicit via sub pass dependencies//渲染通道之间不需要显式同步,因为这是通过子传递依赖项隐式完成的/*//Second pass: Scene rendering with applied shadow map*/{//注意这个渲染通道既有颜色又有深度clearValues[0].color = defaultClearColor;clearValues[1].depthStencil = { 1.0f, 0 };VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();renderPassBeginInfo.renderPass = renderPass;renderPassBeginInfo.framebuffer = frameBuffers[i];renderPassBeginInfo.renderArea.extent.width = width;renderPassBeginInfo.renderArea.extent.height = height;renderPassBeginInfo.clearValueCount = 2;renderPassBeginInfo.pClearValues = clearValues;vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);scissor = vks::initializers::rect2D(width, height, 0, 0);vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);// Visualize shadow mapif (displayShadowMap) {vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.debug, 0, nullptr);vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug);vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0);}// 3D scenevkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.scene, 0, nullptr);vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, (filterPCF) ? pipelines.sceneShadowPCF : pipelines.sceneShadow);scenes[sceneIndex].draw(drawCmdBuffers[i]);drawUI(drawCmdBuffers[i]);vkCmdEndRenderPass(drawCmdBuffers[i]);}VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));}}

4)描述符布局

	void setupDescriptorSetLayout(){// Shared pipeline layout for all pipelines used in this samplestd::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {// Binding 0 : Vertex shader uniform buffervks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0),// Binding 1 : Fragment shader image sampler (shadow map)vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)};VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));}

5)描述符集

void setupDescriptorSets(){std::vector<VkWriteDescriptorSet> writeDescriptorSets;// Image descriptor for the shadow map attachmentVkDescriptorImageInfo shadowMapDescriptor =vks::initializers::descriptorImageInfo(offscreenPass.depthSampler,offscreenPass.depth.view,VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);// Debug displayVkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.debug));writeDescriptorSets = {// Binding 0 : Parameters uniform buffervks::initializers::writeDescriptorSet(descriptorSets.debug, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.scene.descriptor),// Binding 1 : Fragment shader texture samplervks::initializers::writeDescriptorSet(descriptorSets.debug, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &shadowMapDescriptor)};vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);// Offscreen shadow map generationVK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.offscreen));writeDescriptorSets = {// Binding 0 : Vertex shader uniform buffervks::initializers::writeDescriptorSet(descriptorSets.offscreen, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.offscreen.descriptor),};vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);// Scene rendering with shadow map appliedVK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene));writeDescriptorSets = {// Binding 0 : Vertex shader uniform buffervks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.scene.descriptor),// Binding 1 : Fragment shader shadow samplervks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &shadowMapDescriptor)};vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);}

6)管线

	void preparePipelines(){VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);VkPipelineColorBlendStateCreateInfo colorBlendStateCI = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size(), 0);std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);pipelineCI.pInputAssemblyState = &inputAssemblyStateCI;pipelineCI.pRasterizationState = &rasterizationStateCI;pipelineCI.pColorBlendState = &colorBlendStateCI;pipelineCI.pMultisampleState = &multisampleStateCI;pipelineCI.pViewportState = &viewportStateCI;pipelineCI.pDepthStencilState = &depthStencilStateCI;pipelineCI.pDynamicState = &dynamicStateCI;pipelineCI.stageCount = shaderStages.size();pipelineCI.pStages = shaderStages.data();// Shadow mapping debug quad displayrasterizationStateCI.cullMode = VK_CULL_MODE_NONE;shaderStages[0] = loadShader(getShadersPath() + "shadowmapping/quad.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);shaderStages[1] = loadShader(getShadersPath() + "shadowmapping/quad.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);// Empty vertex input stateVkPipelineVertexInputStateCreateInfo emptyInputState = vks::initializers::pipelineVertexInputStateCreateInfo();pipelineCI.pVertexInputState = &emptyInputState;VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.debug));// Scene rendering with shadows appliedpipelineCI.pVertexInputState  = vkglTF::Vertex::getPipelineVertexInputState({vkglTF::VertexComponent::Position, vkglTF::VertexComponent::UV, vkglTF::VertexComponent::Color, vkglTF::VertexComponent::Normal});rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT;shaderStages[0] = loadShader(getShadersPath() + "shadowmapping/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);shaderStages[1] = loadShader(getShadersPath() + "shadowmapping/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);// Use specialization constants to select between horizontal and vertical bluruint32_t enablePCF = 0;VkSpecializationMapEntry specializationMapEntry = vks::initializers::specializationMapEntry(0, 0, sizeof(uint32_t));VkSpecializationInfo specializationInfo = vks::initializers::specializationInfo(1, &specializationMapEntry, sizeof(uint32_t), &enablePCF);shaderStages[1].pSpecializationInfo = &specializationInfo;// No filteringVK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.sceneShadow));// PCF filteringenablePCF = 1;VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.sceneShadowPCF));// Offscreen pipeline (vertex shader only)shaderStages[0] = loadShader(getShadersPath() + "shadowmapping/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);pipelineCI.stageCount = 1;// No blend attachment states (no color attachments used)colorBlendStateCI.attachmentCount = 0;// Cull front facesdepthStencilStateCI.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;// Enable depth biasrasterizationStateCI.depthBiasEnable = VK_TRUE;// Add depth bias to dynamic state, so we can change it at runtimedynamicStateEnables.push_back(VK_DYNAMIC_STATE_DEPTH_BIAS);dynamicStateCI =vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(),dynamicStateEnables.size(),0);pipelineCI.renderPass = offscreenPass.renderPass;VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.offscreen));}

7)uniform

	// Prepare and initialize uniform buffer containing shader uniformsvoid prepareUniformBuffers(){// Offscreen vertex shader uniform buffer blockVK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,&uniformBuffers.offscreen,sizeof(uboOffscreenVS)));// Scene vertex shader uniform buffer blockVK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,&uniformBuffers.scene,sizeof(uboVSscene)));// Map persistentVK_CHECK_RESULT(uniformBuffers.offscreen.map());VK_CHECK_RESULT(uniformBuffers.scene.map());updateLight();updateUniformBufferOffscreen();updateUniformBuffers();}

二、Shader

1.offscreen 阴影贴图渲染

vert

#version 450layout (location = 0) in vec3 inPos;layout (binding = 0) uniform UBO 
{mat4 depthMVP;
} ubo;out gl_PerVertex 
{vec4 gl_Position;   
};
void main()
{gl_Position =  ubo.depthMVP * vec4(inPos, 1.0);
}

frag

#version 450layout(location = 0) out vec4 color;void main() 
{	color = vec4(1.0, 0.0, 0.0, 1.0);
}

2.scene模型渲染

vert

#version 450layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inNormal;layout (binding = 0) uniform UBO 
{mat4 projection;mat4 view;mat4 model;mat4 lightSpace;vec4 lightPos;float zNear;float zFar;
} ubo;layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec4 outShadowCoord;const mat4 biasMat = mat4( 0.5, 0.0, 0.0, 0.0,0.0, 0.5, 0.0, 0.0,0.0, 0.0, 1.0, 0.0,0.5, 0.5, 0.0, 1.0 );void main() 
{outColor = inColor;outNormal = inNormal;gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);vec4 pos = ubo.model * vec4(inPos, 1.0);outNormal = mat3(ubo.model) * inNormal;outLightVec = normalize(ubo.lightPos.xyz - inPos);outViewVec = -pos.xyz;			outShadowCoord = ( biasMat * ubo.lightSpace * ubo.model ) * vec4(inPos, 1.0);	
}

frag

#version 450layout (binding = 1) uniform sampler2D shadowMap;layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec4 inShadowCoord;layout (constant_id = 0) const int enablePCF = 0;layout (location = 0) out vec4 outFragColor;#define ambient 0.1float textureProj(vec4 shadowCoord, vec2 off)
{float shadow = 1.0;if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 ) {float dist = texture( shadowMap, shadowCoord.st + off ).r;if ( shadowCoord.w > 0.0 && dist < shadowCoord.z ) {shadow = ambient;}}return shadow;
}float filterPCF(vec4 sc)
{ivec2 texDim = textureSize(shadowMap, 0);float scale = 1.5;float dx = scale * 1.0 / float(texDim.x);float dy = scale * 1.0 / float(texDim.y);float shadowFactor = 0.0;int count = 0;int range = 1;for (int x = -range; x <= range; x++){for (int y = -range; y <= range; y++){shadowFactor += textureProj(sc, vec2(dx*x, dy*y));count++;}}return shadowFactor / count;
}void main() 
{	float shadow = (enablePCF == 1) ? filterPCF(inShadowCoord / inShadowCoord.w) : textureProj(inShadowCoord / inShadowCoord.w, vec2(0.0));vec3 N = normalize(inNormal);vec3 L = normalize(inLightVec);vec3 V = normalize(inViewVec);vec3 R = normalize(-reflect(L, N));vec3 diffuse = max(dot(N, L), ambient) * inColor;outFragColor = vec4(diffuse * shadow, 1.0);}

3.quad 深度渲染

vert

#version 450layout (location = 0) out vec2 outUV;void main() 
{outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
}

frag

#version 450layout (binding = 1) uniform sampler2D samplerColor;layout (location = 0) in vec2 inUV;layout (location = 0) out vec4 outFragColor;layout (binding = 0) uniform UBO 
{mat4 projection;mat4 view;mat4 model;mat4 lightSpace;vec4 lightPos;float zNear;float zFar;
} ubo;float LinearizeDepth(float depth)
{float n = ubo.zNear;float f = ubo.zFar;float z = depth;return (2.0 * n) / (f + n - z * (f - n));	
}void main() 
{float depth = texture(samplerColor, inUV).r;outFragColor = vec4(vec3(1.0-LinearizeDepth(depth)), 1.0);
}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。