From 0fb88d546799c3b512dabfc454814644b42a9bc8 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Thu, 19 Jul 2018 11:01:15 +0200 Subject: [PATCH] Different scene, toon shading and separate parameters for color input attachment --- android/build.gradle | 2 +- .../inputattachments/attachmentread.frag | 20 ++++++- .../inputattachments/attachmentread.frag.spv | Bin 1632 -> 2500 bytes .../inputattachments/attachmentwrite.frag | 14 ++++- .../inputattachments/attachmentwrite.frag.spv | Bin 496 -> 1364 bytes .../inputattachments/attachmentwrite.vert | 13 ++-- .../inputattachments/attachmentwrite.vert.spv | Bin 1396 -> 1772 bytes .../inputattachments/inputattachments.cpp | 56 +++++++++++------- 8 files changed, 74 insertions(+), 31 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 76d0d98b..45ae3812 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.1' + classpath 'com.android.tools.build:gradle:3.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/data/shaders/inputattachments/attachmentread.frag b/data/shaders/inputattachments/attachmentread.frag index 65415604..685a619a 100644 --- a/data/shaders/inputattachments/attachmentread.frag +++ b/data/shaders/inputattachments/attachmentread.frag @@ -4,16 +4,30 @@ layout (input_attachment_index = 0, binding = 0) uniform subpassInput inputColor layout (input_attachment_index = 1, binding = 1) uniform subpassInput inputDepth; layout (binding = 2) uniform UBO { + vec2 brightnessContrast; vec2 range; int attachmentIndex; } ubo; layout (location = 0) out vec4 outColor; +vec3 brightnessContrast(vec3 color, float brightness, float contrast) { + return (color - 0.5) * contrast + 0.5 + brightness; +} + void main() { - // Read values from previous sub pass - vec3 col = ubo.attachmentIndex == 0 ? subpassLoad(inputColor).rgb : subpassLoad(inputDepth).rrr; + // Apply brightness and contrast filer to color input + if (ubo.attachmentIndex == 0) { + // Read color from previous color input attachment + vec3 color = subpassLoad(inputColor).rgb; + outColor.rgb = brightnessContrast(color, ubo.brightnessContrast[0], ubo.brightnessContrast[1]); + } - outColor.rgb = ((col - ubo.range[0]) * 1.0 / (ubo.range[1] - ubo.range[0])); + // Visualize depth input range + if (ubo.attachmentIndex == 1) { + // Read depth from previous depth input attachment + float depth = subpassLoad(inputDepth).r; + outColor.rgb = vec3((depth - ubo.range[0]) * 1.0 / (ubo.range[1] - ubo.range[0])); + } } \ No newline at end of file diff --git a/data/shaders/inputattachments/attachmentread.frag.spv b/data/shaders/inputattachments/attachmentread.frag.spv index 3585b2ac72a6111d835add0263db3f3aa883d648..5c4575f417d68106955807a72170e429d169c0fc 100644 GIT binary patch literal 2500 zcmZ{jYj0Im5QbN2Pbn>zR&GKfo>Nq+U=_K8(3THHI1bEsX7yB3rI@xDfn0#F7<`Q zTZ_}1#me-_Q}61zGY!g0+b(>DkfrtZT37ruG>(rLMUV!`n9^?~Z-CZ)8)=k$DJ}Qc z9<3JL&gSNPuUquno5j?VhbJyPJno0xxH0&H-gmuzsW;O0|HSFjNIOcODgz?OV0smP z`|=WnP{&=~EJd}F6OJI?a$ft{za;LodMfu_uK1m^PGE5pLtGSL|^G_6sx6wBYg}n>s+b8 zYhc*?@rmA&Y<%_C%*oQm})`a%s?|W1Cg*MT>rXH<$3EPRkoqhuD`puKui1%Cucj56q{>aub<{15}ABU_b>GK}k8vd{5JOy{I z;^tI$pJLABJiG~c>|fn~*JJ$qaO;^f?6b%NS+}2SS&yFGzej)LZkO8kGpFg;7kT-TzE|j;!wHHK+wmLKmrFzpq2$zv6QQK9w*rWR~#@SvfUFcN6h8Yr;2fY>gVXd9_FE)koC={ z-8k=e1@i4I@t)N`$e1zi)IHjhd-V?b>hmWzj`_)BH&>DEufCfFbn)2Db!0K$n7jE1 z?r}HTK7_>Fjk3MEn@=F$VeDopWA5fAo!aLONZez;z8$fc(@kWtnCGX+_G>To9o$0K z_G8Zb8M3kBKV|H5WO4t6$oB?ZVYjAB*_Zwtm#edFq?;u-S z+YN5*j_*R^L9yR&!Rq_Hhc2f7|Ls>F_ZvHqKjW33h5X;G=X>&uLT8xs2z)Hd+u+-4 z@(yHoxeIPz#*ah#NB=J&Pt?f!>$e-yS9{EI53)Ib$v%6L^%IYL`;hDTe#pN2!S&Z3 b`Cdl0--tbcEEW{82f>Wj{x|ngcP9S;NqwmS literal 1632 zcmYk6*-leY6oxmH78GTWNgVJLoQrh=L=gyTQcXx|VtjxCjgUY~uq0lZc;l51;$!$y z4vC51ch1@7tmMxc_xjgf`?RJ0g~1T|!eAH*8zGbN&`%@;^=l``vc^V{fm% zwEV!zkx){lxlw8cu*Fg9pkw?F7^B7*Bv>UnhVX;tWunE}GPyJ4cDsky;Rj*Vqtf9w^bTV#mDm6!ZG#73=kD zsh9ck6zlb^cLwYAt&hIbooXgf-XDa`B2q@n?OXYq@vWjio%>rs5dtQ=7HSW_VGQg0Qb8?ANP>}WA3!cbk0=; zNe)zcFYmMgkF%QpOWcHM5;rHW^F0NgH6CYtmdDMBulYP{*v@2LUf=XN@cVAE4gK{zX06|) S-=aKzo9@wCYyR@mtKc6k5>vqd diff --git a/data/shaders/inputattachments/attachmentwrite.frag b/data/shaders/inputattachments/attachmentwrite.frag index 2f7003aa..69e1b6b7 100644 --- a/data/shaders/inputattachments/attachmentwrite.frag +++ b/data/shaders/inputattachments/attachmentwrite.frag @@ -1,11 +1,23 @@ #version 450 layout (location = 0) in vec3 inColor; +layout (location = 1) in vec3 inNormal; +layout (location = 2) in vec3 inViewVec; +layout (location = 3) in vec3 inLightVec; layout (location = 0) out vec4 outColor; void main() { - outColor = vec4(inColor, 0.0); + // Toon shading color attachment output + float intensity = dot(normalize(inNormal), normalize(inLightVec)); + float shade = 1.0; + shade = intensity < 0.5 ? 0.75 : shade; + shade = intensity < 0.35 ? 0.6 : shade; + shade = intensity < 0.25 ? 0.5 : shade; + shade = intensity < 0.1 ? 0.25 : shade; + + outColor.rgb = inColor * 3.0 * shade; + // Depth attachment does not need to be explicitly written } \ No newline at end of file diff --git a/data/shaders/inputattachments/attachmentwrite.frag.spv b/data/shaders/inputattachments/attachmentwrite.frag.spv index 8adb5c84d249286cc651e3f1c3b340e1d5612bd3..cfb4a18e9f98e7efb14ef8643a09d266e8cad412 100644 GIT binary patch literal 1364 zcmYk4Pft@p6veN!1q9@eAc!Iq@jqJB0-6w@2_bA6U6`)MBHKETKcYFd@ZuA?t zcH`PLd>=O^p5OG{I!$ihJ?EY|b7$t323Cg5uGo+b+biq+rffir*{BW1??Lljv)0Xz zYU>-%QH)zD3eim1h?VsvPFo?PFsc|=JX6#Zn~F)TL8Hx-m9&G|Zy8j?rfVx=hE)q$ z-p;xq|EwGi&yc<8oSn8#ltzv{Le>l)j`PFz`<~ZzaoywAQM=I4L+4|@*E#8&Db>1i z>?veD3UB%g*Y6FPI;gbEX(iFz1}sKi}ne@*X#QiL*dJdi*5Q|RnFeLDSbcHmovcACz!i~^S-&ewC^}% z)@XR0yQIFUx8ff0=7+~0F%^B$a(~PYU&qV?cw2SuZcM?wjj2oUH3i=Lk^8Tk5avEz zH>uop)V^w0&N-#PgZhp-Vt+Vt_T~-P8|{pO`Y)~J{^tsgcX7?Ua@PIO3fA0GpyQ3G zxi7r0xvf0)y(0|AJGy2`dFopgM#tMybKk0RU$d+{^<5E$P_r-^2GSrS5Suh`;z02Uf+j+Fq3i{HDkni}9k1bmefHUF zfBWZTmAiVxS=6HugE*XCR3H(ps3kv}ujZrg?fd9){Gg&8SrV%0P+7%mpKt4piF51% z>!Oo0fLBJ>h#f^a^i-citSNrBuf<34#WQK_vECfej0!b3COO9DkMV%+_PP;*Q{Tj^H&)-mvwq_0jom?ZPpr0$d7k)z`#8&Ek3A&E vPoeg{#H?RJt?iTy_s_Z|bH-8EE1CTlZ14Vk%zE|a-Q$I+{>R)eTRvld4b&Gn diff --git a/data/shaders/inputattachments/attachmentwrite.vert b/data/shaders/inputattachments/attachmentwrite.vert index d70423a6..c5b82566 100644 --- a/data/shaders/inputattachments/attachmentwrite.vert +++ b/data/shaders/inputattachments/attachmentwrite.vert @@ -1,9 +1,6 @@ #version 450 -#extension GL_ARB_separate_shader_objects : enable -#extension GL_ARB_shading_language_420pack : enable - -layout (location = 0) in vec4 inPos; +layout (location = 0) in vec3 inPos; layout (location = 1) in vec3 inColor; layout (location = 2) in vec3 inNormal; @@ -14,6 +11,9 @@ layout (binding = 0) uniform UBO { } ubo; layout (location = 0) out vec3 outColor; +layout (location = 1) out vec3 outNormal; +layout (location = 2) out vec3 outViewVec; +layout (location = 3) out vec3 outLightVec; out gl_PerVertex { vec4 gl_Position; @@ -21,6 +21,9 @@ out gl_PerVertex { void main() { - gl_Position = ubo.projection * ubo.view * ubo.model * inPos; + gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos, 1.0); outColor = inColor; + outNormal = inNormal; + outLightVec = vec3(0.0f, 5.0f, 15.0f) - inPos; + outViewVec = -inPos.xyz; } diff --git a/data/shaders/inputattachments/attachmentwrite.vert.spv b/data/shaders/inputattachments/attachmentwrite.vert.spv index 05849022ad811c774b84e6884386e5c1e42d6c2e..6a52830575268bc5fed78590cc6e6713504d935a 100644 GIT binary patch literal 1772 zcmZ9MT~8BH5QdN4E}$rg$cJCpQon(U3W_l*=ta3`6hiE+LD=A$c9$%RdMAm$K!1V1 z#b4!(iSKiE2OCZ^J@dXZb7s!WX@g2*(797?&<(k#u2^HPBF4F4H{kbHd#l|%$akAd z%Zqr7xWGH&GwRNHy{hltcANmtNM%QNJvQCuc(N{sS1&@v`J&5x-O%xyaeDLTa+TXo;>;2JL`&s%a+VSqvI~ zYB!FWKF84)ZT_tD+7CaZ(yRVHaiX-|7W%uThxzNYn`VW7)!#Kv3My0-_m>jm_fcZ9I8 z^QQVS?+Z+A*y(vcrr%)8N3X$w1b_N$_HlXPSC;nb&jGO@GHF@a8ii9UuB_K9kbnf9f1!rX<9{Tihk-#4#6(yDS~v;;u+1 z79Jn&bzQ=2Hgz_0sh6C#V{W$O=(qVbN{%S7~E|dtHpTrVAW{-sW&SM1yN`{^dcg@z_PX>M$_Grr01S|fRINJdJxU2p65| z`Qy`o!yDigU#WPT*(aH+^#*<_+)A&O`D(Pf(rfX@)Ux&L3z z7UZ}>>30h0e9nDjwkj20Mx0ih-Iz(=M|1%CtKi#+3zbH{U7e}3o}ANJLw^^My?%Xi zZUb$lETF&}$wIm>V~*jXTp`C@$v1&DfO;RAkGKW!XDtKg7)H*#vueeCoH>Rb*CGGH H&>rv&Z)rS; diff --git a/examples/inputattachments/inputattachments.cpp b/examples/inputattachments/inputattachments.cpp index ab39b93f..0b198ece 100644 --- a/examples/inputattachments/inputattachments.cpp +++ b/examples/inputattachments/inputattachments.cpp @@ -36,7 +36,6 @@ public: vks::VERTEX_COMPONENT_POSITION, vks::VERTEX_COMPONENT_COLOR, vks::VERTEX_COMPONENT_NORMAL, - vks::VERTEX_COMPONENT_UV, }); vks::Model scene; @@ -48,6 +47,7 @@ public: } uboMatrices; struct UBOParams { + glm::vec2 brightnessContrast = glm::vec2(0.5f, 1.8f); glm::vec2 range = glm::vec2(0.6f, 1.0f); int32_t attachmentIndex = 1; } uboParams; @@ -68,7 +68,6 @@ public: } pipelineLayouts; struct { - VkDescriptorSet attachmentWrite; VkDescriptorSet attachmentRead; } descriptorSets; @@ -93,12 +92,9 @@ public: { title = "Input attachments"; camera.type = Camera::CameraType::firstperson; - camera.movementSpeed = 5.0f; -#ifndef __ANDROID__ - camera.rotationSpeed = 0.25f; -#endif - camera.setPosition(glm::vec3(-3.2f, 1.0f, 5.9f)); - camera.setRotation(glm::vec3(0.5f, 210.05f, 0.0f)); + camera.movementSpeed = 2.5f; + camera.setPosition(glm::vec3(1.65f, 1.75f, -6.15f)); + camera.setRotation(glm::vec3(-12.75f, 380.0f, 0.0f)); camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -219,6 +215,7 @@ public: std::array attachments{}; // Swap chain image color attachment + // Will be transitioned to present layout attachments[0].format = swapChain.colorFormat; attachments[0].samples = VK_SAMPLE_COUNT_1_BIT; attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; @@ -229,6 +226,9 @@ public: attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; // Input attachments + // These will be written in the first subpass, transitioned to input attachments + // and then read in the secod subpass + // Color attachments[1].format = this->attachments.color.format; attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; @@ -380,7 +380,7 @@ public: /* Second sub pass - Reads from the attachments via input attachments + Render a full screen quad, reading from the previously written attachments via input attachments */ { vks::debugmarker::beginRegion(drawCmdBuffers[i], "Subpass 1: Reading attachments", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); @@ -402,7 +402,7 @@ public: void loadAssets() { - scene.loadFromFile(getAssetPath() + "models/samplebuilding.dae", vertexLayout, 1.0f, vulkanDevice, queue); + scene.loadFromFile(getAssetPath() + "models/treasure_smooth.dae", vertexLayout, 1.0f, vulkanDevice, queue); } void setupDescriptors() @@ -518,10 +518,9 @@ public: // Attribute descriptions std::vector vertexInputAttributes = { - vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, 0), // Location 0: Position - vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 3), // Location 1: Color - vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 6), // Location 2: Normal - vks::initializers::vertexInputAttributeDescription(0, 3, VK_FORMAT_R32G32_SFLOAT, sizeof(float) * 9), // Location 3: UV + vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, 0), // Location 0: Position + vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 3), // Location 1: Color + vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 6), // Location 2: Normal }; VkPipelineVertexInputStateCreateInfo vertexInputStateCI = vks::initializers::pipelineVertexInputStateCreateInfo(); @@ -624,15 +623,30 @@ public: virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) { if (overlay->header("Settings")) { - if (overlay->comboBox("Attachment", &uboParams.attachmentIndex, { "color", "depth" })) { + overlay->text("Input attachment"); + if (overlay->comboBox("##attachment", &uboParams.attachmentIndex, { "color", "depth" })) { updateUniformBuffers(); } - overlay->text("Visible range"); - if (overlay->sliderFloat("min", &uboParams.range[0], 0.0f, uboParams.range[1])) { - updateUniformBuffers(); - } - if (overlay->sliderFloat("max", &uboParams.range[1], uboParams.range[0], 1.0f)) { - updateUniformBuffers(); + switch (uboParams.attachmentIndex) { + case 0: + overlay->text("Brightness"); + if (overlay->sliderFloat("##b", &uboParams.brightnessContrast[0], 0.0f, 2.0f)) { + updateUniformBuffers(); + } + overlay->text("Contrast"); + if (overlay->sliderFloat("##c", &uboParams.brightnessContrast[1], 0.0f, 4.0f)) { + updateUniformBuffers(); + } + break; + case 1: + overlay->text("Visible range"); + if (overlay->sliderFloat("min", &uboParams.range[0], 0.0f, uboParams.range[1])) { + updateUniformBuffers(); + } + if (overlay->sliderFloat("max", &uboParams.range[1], uboParams.range[0], 1.0f)) { + updateUniformBuffers(); + } + break; } } }