diff --git a/data/shaders/instancing/instancing.frag b/data/shaders/instancing/instancing.frag index 32047558..a0f78d72 100644 --- a/data/shaders/instancing/instancing.frag +++ b/data/shaders/instancing/instancing.frag @@ -20,7 +20,7 @@ void main() vec3 L = normalize(inLightVec); vec3 V = normalize(inViewVec); vec3 R = reflect(-L, N); - vec3 diffuse = max(dot(N, L), 0.0) * inColor; + vec3 diffuse = max(dot(N, L), 0.1) * inColor; vec3 specular = (dot(N,L) > 0.0) ? pow(max(dot(R, V), 0.0), 16.0) * vec3(0.75) * color.r : vec3(0.0); outFragColor = vec4(diffuse * color.rgb + specular, 1.0); } \ No newline at end of file diff --git a/data/shaders/instancing/instancing.frag.spv b/data/shaders/instancing/instancing.frag.spv index e56f071a..7857daf8 100644 Binary files a/data/shaders/instancing/instancing.frag.spv and b/data/shaders/instancing/instancing.frag.spv differ diff --git a/data/shaders/instancing/starsphere.frag b/data/shaders/instancing/starsphere.frag new file mode 100644 index 00000000..87c53d82 --- /dev/null +++ b/data/shaders/instancing/starsphere.frag @@ -0,0 +1,34 @@ +#version 450 + +layout (location = 0) in vec3 inUVW; + +layout (location = 0) out vec4 outFragColor; + +#define HASHSCALE3 vec3(443.897, 441.423, 437.195) +#define STARFREQUENCY 0.01 + +// Hash function by Dave Hoskins (https://www.shadertoy.com/view/4djSRW) +float hash33(vec3 p3) +{ + p3 = fract(p3 * HASHSCALE3); + p3 += dot(p3, p3.yxz+vec3(19.19)); + return fract((p3.x + p3.y)*p3.z + (p3.x+p3.z)*p3.y + (p3.y+p3.z)*p3.x); +} + +vec3 starField(vec3 pos) +{ + vec3 color = vec3(0.0); + float threshhold = (1.0 - STARFREQUENCY); + float rnd = hash33(pos); + if (rnd >= threshhold) + { + float starCol = pow((rnd - threshhold) / (1.0 - threshhold), 16.0); + color += vec3(starCol); + } + return color; +} + +void main() +{ + outFragColor = vec4(starField(inUVW), 1.0); +} \ No newline at end of file diff --git a/data/shaders/instancing/starsphere.frag.spv b/data/shaders/instancing/starsphere.frag.spv new file mode 100644 index 00000000..b03c95c8 Binary files /dev/null and b/data/shaders/instancing/starsphere.frag.spv differ diff --git a/data/shaders/instancing/starsphere.vert b/data/shaders/instancing/starsphere.vert new file mode 100644 index 00000000..381e249f --- /dev/null +++ b/data/shaders/instancing/starsphere.vert @@ -0,0 +1,9 @@ +#version 450 + +layout (location = 0) out vec3 outUVW; + +void main() +{ + outUVW = vec3((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2, gl_VertexIndex & 2); + gl_Position = vec4(outUVW.st * 2.0f - 1.0f, 0.0f, 1.0f); +} \ No newline at end of file diff --git a/data/shaders/instancing/starsphere.vert.spv b/data/shaders/instancing/starsphere.vert.spv new file mode 100644 index 00000000..256b58ca Binary files /dev/null and b/data/shaders/instancing/starsphere.vert.spv differ diff --git a/instancing/instancing.cpp b/instancing/instancing.cpp index d752f801..e24a6178 100644 --- a/instancing/instancing.cpp +++ b/instancing/instancing.cpp @@ -81,6 +81,7 @@ public: struct { VkPipeline instancedRocks; VkPipeline planet; + VkPipeline starfield; } pipelines; VkDescriptorSetLayout descriptorSetLayout; @@ -89,7 +90,6 @@ public: VkDescriptorSet planet; } descriptorSets; - VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { title = "Vulkan Example - Instanced mesh rendering"; @@ -105,6 +105,7 @@ public: { vkDestroyPipeline(device, pipelines.instancedRocks, nullptr); vkDestroyPipeline(device, pipelines.planet, nullptr); + vkDestroyPipeline(device, pipelines.starfield, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyBuffer(device, instanceBuffer.buffer, nullptr); @@ -148,6 +149,11 @@ public: VkDeviceSize offsets[1] = { 0 }; + // Star field + vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.planet, 0, NULL); + vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.starfield); + vkCmdDraw(drawCmdBuffers[i], 4, 1, 0, 0); + // Planet vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.planet, 0, NULL); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.planet); @@ -378,6 +384,16 @@ public: inputState.vertexBindingDescriptionCount = 1; inputState.vertexAttributeDescriptionCount = 4; VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.planet)); + + // Star field pipeline + rasterizationState.cullMode = VK_CULL_MODE_NONE; + depthStencilState.depthWriteEnable = VK_FALSE; + shaderStages[0] = loadShader(getAssetPath() + "shaders/instancing/starfield.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/instancing/starfield.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + // Vertices are generated in the vertex shader + inputState.vertexBindingDescriptionCount = 0; + inputState.vertexAttributeDescriptionCount = 0; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.starfield)); } float rnd(float range) diff --git a/instancing/instancing.vcxproj b/instancing/instancing.vcxproj index 4a8d04ea..8a1d5475 100644 --- a/instancing/instancing.vcxproj +++ b/instancing/instancing.vcxproj @@ -26,6 +26,8 @@ + + {CF2E27EC-5A81-412A-A3E2-E6EB5C8B66E3} diff --git a/instancing/instancing.vcxproj.filters b/instancing/instancing.vcxproj.filters index 152fd322..7f8c733c 100644 --- a/instancing/instancing.vcxproj.filters +++ b/instancing/instancing.vcxproj.filters @@ -55,5 +55,11 @@ Shaders + + Shaders + + + Shaders + \ No newline at end of file diff --git a/vulkanExamples.sln b/vulkanExamples.sln index b7c1ec70..feaa06b2 100644 --- a/vulkanExamples.sln +++ b/vulkanExamples.sln @@ -127,6 +127,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9E815C67-731B-4559-938E-23CD771F0860}" ProjectSection(SolutionItems) = preProject CMakeLists.txt = CMakeLists.txt + README.md = README.md EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "screenshot", "screenshot\screenshot.vcxproj", "{AD1DAD4D-A753-4A78-88E2-B4DCE15D482F}"