From bb9310b94aa019e768ace5149edb1f3feca6030d Mon Sep 17 00:00:00 2001 From: saschawillems Date: Sun, 8 Jan 2017 12:27:03 +0100 Subject: [PATCH] Added starfield backdrop to instancing example --- data/shaders/instancing/instancing.frag | 2 +- data/shaders/instancing/instancing.frag.spv | Bin 2380 -> 2396 bytes data/shaders/instancing/starsphere.frag | 34 ++++++++++++++++++++ data/shaders/instancing/starsphere.frag.spv | Bin 0 -> 2412 bytes data/shaders/instancing/starsphere.vert | 9 ++++++ data/shaders/instancing/starsphere.vert.spv | Bin 0 -> 1260 bytes instancing/instancing.cpp | 18 ++++++++++- instancing/instancing.vcxproj | 2 ++ instancing/instancing.vcxproj.filters | 6 ++++ vulkanExamples.sln | 1 + 10 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 data/shaders/instancing/starsphere.frag create mode 100644 data/shaders/instancing/starsphere.frag.spv create mode 100644 data/shaders/instancing/starsphere.vert create mode 100644 data/shaders/instancing/starsphere.vert.spv 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 e56f071a6141623ae53b9e2d0ac2def99e5681c0..7857daf840031ef673ed52770fe7d1c4dc1f0acb 100644 GIT binary patch delta 757 zcmYk4NlF7j7=^34qee6caaag~Ac8@0nTVR9o##$$=cs$}5YdIW6C_?lvhoh`2JStA z0|#UsQ15ru1-7qkb z^u$ZAz$-xdAS3A^R)GaGKz?;`zIC{K<3MqGoF{6j_7>1%C?-H-$%fg(=m*+fL#V+% zknR!>;7XQWVx0oFv>(2NR{Q1-yL@`FZl7Y@f;G7ERVPn>Bf&Y3;PZ*i^P6L!;X?i* z+TbM7r0Zx?4?H9rp2Af_ajxbWJP+=Zm~*(|q}Of{webazL#LK#b+|M&aoS~~scDtZ Wp&_q;@)a-t7|?bRw|sw8SOEWdv^~oJ delta 735 zcmYk4$w~u35Qe*FqER#miF(kBAcDc=;K7KRW!#NRl5sbB@R&#N6g`MHLE?)@PQHVW z;7fRN0m1K|OhY>SRMr1ibyfA`BmJIE`QBvG7|XBETtBDkHhgVVtPNXl_R)szz{}Z& z;S3wktYBwM!t`R-MVh<7#hX!FD$b$~;co$P4kOg<5KjOd)hXVMBJl}RZhiWhYz1ba z3sP#tK44U)=)_@rE-~OYHPk9t1}gg^v4GY2B~@A;9D;9_ke`RwkVTN~ic9!Y;KlSr zO9uGLDS<(nYwyU@k*!ox= 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 0000000000000000000000000000000000000000..b03c95c839d3eea8f6e1c62221184df6bc252a77 GIT binary patch literal 2412 zcmZ9M$!-)^6oxOf2ND}I&m^=lQ)1#InAw1_Ol)u*Y~lc!p@nVQ(73H`!wxACLgE2f zggwuYB@5nwEebEeB7{hZ_`Y%-RC2X`-T(Y&xaZzJRi%|Z?J2F`w<;~9rDt7QiJekM zYRlHq@u~5?=HhhU;Uixd(UnSBpfPJwdum}7cPex9I%Yu?tR+sJ4PYgzru3YoR@7pA zD@q5xYNc82?>}&FwtoPnls-gT=r_KNeNt*JRvII7GqvfZkSwOln0gc5lFq?z)NAzy zt2IYi7psk#X0=+cO{QebQV7bU#K)Hce41FG{x#3!}S{ew(K1ByNe@@%1xxK zY?kNQ=H@S7xtizAu|}Oy`tbZ810QYU!`{Fxcn{s#jiiikhPR=W!4~4ZFKutRjJ=9g zO>|>_z^Y$edo`;*?Otqs>%oiHuZF|kfW7z6zuwy$vH$+#@x5%PrE0xmipfVcMtmt|9&{5@ZRo)doQu?S8$)$cLc5$h`94; zPZ#bFZX|mG9=iWcO1}+;{nL^?ncdb5<+q1c*|0djCJ%fJjYOWn+ zBJA5*KGyjMrIpoK{}boK+|$^uw}AO)h}Nflf((7PfwQiD3C%mJ0d=3yoLM#d3eA^K z&3-1S=6UMCeBjUPhN8aaf^NKzBp;nKWFY}mU?L%nl(G#EAWS)BD9fn)Gc$OpB z>e16txVh?4>loZ#^+m1Y@a45mps7d9$vmdm=Kz{|)IJ3_Pd)NZ! zdek}#w^x16t#>>GRsidr1^+W~YVn>pmurB}Y7p;97u@$`Eo;2{=itU`J3ph#>6m2UTu}gv!)*AgmYcOJF|J`l4n3I&D9~+oZ~Xy zzRqRd_$t7jCw{%A-<~Dz`)Tgi#oOCi9`dI4@l2ZfACwmU395H>5_nI)P~~$`^R9kJ zZ9eMW*D%g>npi#cSs=DgT62b2ZF0>bTs^H>BA#5c3|E_6a~7`N=O+9X&H?wfm$|2$ z%I`wmzSgV(wKp*D^GejzwJGp}vzfaF%zMZEsNEp;j>Y$=-6Xcw+*>r?WgVz%NxMy~ j<{o|a_Z{v4Yt8vq_I*JQ-|8;NKnHjhx%GeP@Cf_^24YJm literal 0 HcmV?d00001 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}"