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 3585b2ac..5c4575f4 100644 Binary files a/data/shaders/inputattachments/attachmentread.frag.spv and b/data/shaders/inputattachments/attachmentread.frag.spv differ 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 8adb5c84..cfb4a18e 100644 Binary files a/data/shaders/inputattachments/attachmentwrite.frag.spv and b/data/shaders/inputattachments/attachmentwrite.frag.spv differ 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 05849022..6a528305 100644 Binary files a/data/shaders/inputattachments/attachmentwrite.vert.spv and b/data/shaders/inputattachments/attachmentwrite.vert.spv differ 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; } } }