From 00b5ea453f32db7bfca1b09f9502429c18c5e79a Mon Sep 17 00:00:00 2001 From: saschawillems Date: Sun, 1 Jan 2017 15:41:23 +0100 Subject: [PATCH] Code cleanup, additional specialization constant as parameter for one lighting path --- .../shaders/specializationconstants/uber.frag | 4 + .../specializationconstants/uber.frag.spv | Bin 4656 -> 4988 bytes .../specializationconstants.cpp | 79 ++++++++---------- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/data/shaders/specializationconstants/uber.frag b/data/shaders/specializationconstants/uber.frag index a5c3b79f..946cbd40 100644 --- a/data/shaders/specializationconstants/uber.frag +++ b/data/shaders/specializationconstants/uber.frag @@ -17,6 +17,8 @@ layout (location = 0) out vec4 outFragColor; // We use this constant to control the flow of the shader depending on the // lighting model selected at pipeline creation time layout (constant_id = 0) const int LIGHTING_MODEL = 0; +// Parameter for the toon shading part of the shader +layout (constant_id = 1) const float PARAM_TOON_DESATURATION = 0.0f; void main() { @@ -50,6 +52,8 @@ void main() color = inColor * 0.4; else color = inColor * 0.2; + // Desaturate a bit + color = vec3(mix(color, vec3(dot(vec3(0.2126,0.7152,0.0722), color)), PARAM_TOON_DESATURATION)); outFragColor.rgb = color; break; } diff --git a/data/shaders/specializationconstants/uber.frag.spv b/data/shaders/specializationconstants/uber.frag.spv index 7ec71a38133cab2758c777997c296b58a0ac9575..c650df5006e704a7e8ea4867e6f595a05fd03d2f 100644 GIT binary patch delta 1910 zcmYk7+l!TD6vp4Zw|gdY&wx;DOi;^+WTXoTN(K!P@g^E4D4Dt%b%cT0IygxeR@>3+ zVBw){scGeyrDmR*YP6%~AuX7?^Qu21NKo+i+rG>8Cl6~q&pLeTeb=|gGmqYStfN$% z+n!Q^fA^)oc6Yp7*n4(xQ=z^yrL=ne(E1J7a%xKr>_~rYcxYh5Qsr9zF#43V7kzNu z3ol}t`~bGUwrX%--7D}?8iPOAjFr+6?6cWFinYj`udvTI{VC}*cFo}0S!*|r3=k}* z351d1fz=y_`ke)(EloB?i?8->g%vWEGm0ZV0(^o$jBk$IQD=c-=9Adg{K@f!rBboh zMQVl6YvAwkzQw^`hxb*VdM5ZA@O}R@7R_g%4*WW>#yn#Sav#U^nFHT}<+js8Fe6P( zyBfXax%J(!UBHYFus&+tnR_swD*QvN(|HK3vA^sF?bLD#Q2i-18sG0X)|~U>|v3f zV-$FEo56~FlUwlB1EIf(=4}Qle6sf31`6N_unAaiZf}0aTlnPon*YVO?~2XBNPH)2 zUT(bfU3?puZ+tOV=K<6gaX${Q9jM7&?R%gb^sr+2`%R52-`=>{F|$v4lMPP8{)fFE zYpFZm1KhXVo$dqfb0uf7)qY@(dbIyI>-!tE&gwjWxhgTtL438Id|RL3s~v2d?5x&5 z#Z!3zSmAL#1Jl7a-YGu>%v1OH{>Bf1$Ulr<1(m$N&+*;IGGNS_F|2yje1SjHokY=> z2vKCvq9a)KC_08e8$``<{HQTz%~7m+)SSS#Pdy**Ykal4alVO@5T~o~$$HvTz^2QA z$;NH$M{X<{pTV~-_*r~k6kql?_~xkFyMi4D_I?}~@8bQ<2Cnv7(7LGavZn51p3`O(Xl`asgM@b79#Mp6A^`oh#<0p>L5Cp%wRxEwCJGZ8{7A7vdtD- zENwU26YO8~XFBM*#_xss;|hpo<^o7tAjWtX*5nh@d?LOWO<7KV}X&)T)gX}l3&#u==i zT1V=;pkEXId2Bs!G4n5A9Yo!HeFsuohW`QE3jEqq|06sH(*9ZYc_9CHi^G+G0~%U{ z^_Z)DkGb0SsH^?^<@UOk#_=k(8$LNRRX3Jzch{#t2k2(^`opPq5?cdmK^{0?7yr#2 zE*BOzRZi&~o~O}Ct~~6} zZ^7xG0-J6H<{G#07}i*b-}rz#R3?kjyXg z4aY`1)2H?wt`-rCf1ugG_^x!IpKx`*sQrSgc?wbcoofC}c5-jt&Fm`v*}wu5HUoFn Lft@WEmaO~_+25|4 diff --git a/specializationconstants/specializationconstants.cpp b/specializationconstants/specializationconstants.cpp index a460b08d..edf6d8ad 100644 --- a/specializationconstants/specializationconstants.cpp +++ b/specializationconstants/specializationconstants.cpp @@ -1,6 +1,8 @@ /* * Vulkan Example - Shader specialization constants * +* For details see https://www.khronos.org/registry/vulkan/specs/misc/GL_KHR_vulkan_glsl.txt +* * Copyright (C) 2016 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) @@ -71,11 +73,8 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.1f; - rotation = glm::vec3(-41.25f, -90.0f, 0.0f); - enableTextOverlay = true; title = "Vulkan Example - Specialization constants"; - + enableTextOverlay = true; camera.type = Camera::CameraType::lookat; camera.setPerspective(60.0f, ((float)width / 3.0f) / (float)height, 0.1f, 512.0f); camera.setRotation(glm::vec3(-40.0f, -90.0f, 0.0f)); @@ -137,20 +136,20 @@ public: vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.cube.indices.buf, 0, VK_INDEX_TYPE_UINT32); // Left - viewport.width = (float)width / 3.0; + viewport.width = (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.phong); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.cube.indexCount, 1, 0, 0, 0); // Center - viewport.x = (float)width / 3.0; + viewport.x = (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.toon); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.cube.indexCount, 1, 0, 0, 0); // Right - viewport.x = (float)width / 3.0 + (float)width / 3.0; + viewport.x = (float)width / 3.0f + (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.textured); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.cube.indexCount, 1, 0, 0, 0); @@ -163,7 +162,7 @@ public: void loadAssets() { - loadMesh(getAssetPath() + "models/color_teapot_spheres.X", &meshes.cube, vertexLayout, 0.1f); + loadMesh(getAssetPath() + "models/color_teapot_spheres.dae", &meshes.cube, vertexLayout, 0.1f); textureLoader->loadTexture(getAssetPath() + "textures/metalplate_nomips_rgba.ktx", VK_FORMAT_R8G8B8A8_UNORM, &textures.colormap); } @@ -171,48 +170,42 @@ public: { // Binding description vertices.bindingDescriptions.resize(1); - vertices.bindingDescriptions[0] = - vkTools::initializers::vertexInputBindingDescription( - VERTEX_BUFFER_BIND_ID, - vkMeshLoader::vertexSize(vertexLayout), - VK_VERTEX_INPUT_RATE_VERTEX); + vertices.bindingDescriptions = { + vkTools::initializers::vertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, vkMeshLoader::vertexSize(vertexLayout), VK_VERTEX_INPUT_RATE_VERTEX), + }; // Attribute descriptions - // Describes memory layout and shader positions - vertices.attributeDescriptions.resize(4); - // Location 0 : Position - vertices.attributeDescriptions[0] = + vertices.attributeDescriptions = { + // Location 0 : Position vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 0, VK_FORMAT_R32G32B32_SFLOAT, - 0); - // Location 1 : Color - vertices.attributeDescriptions[1] = + 0), + // Location 1 : Color vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 1, VK_FORMAT_R32G32B32_SFLOAT, - sizeof(float) * 3); - // Location 3 : Texture coordinates - vertices.attributeDescriptions[2] = + sizeof(float) * 3), + // Location 3 : Texture coordinates vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 2, VK_FORMAT_R32G32_SFLOAT, - sizeof(float) * 6); - // Location 2 : Normal - vertices.attributeDescriptions[3] = + sizeof(float) * 6), + // Location 2 : Normal vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 3, VK_FORMAT_R32G32B32_SFLOAT, - sizeof(float) * 8); + sizeof(float) * 8), + }; vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo(); - vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size(); + vertices.inputState.vertexBindingDescriptionCount = static_cast(vertices.bindingDescriptions.size()); vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data(); - vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size(); + vertices.inputState.vertexAttributeDescriptionCount = static_cast(vertices.attributeDescriptions.size()); vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data(); } @@ -226,7 +219,7 @@ public: VkDescriptorPoolCreateInfo descriptorPoolInfo = vkTools::initializers::descriptorPoolCreateInfo( - poolSizes.size(), + static_cast(poolSizes.size()), poolSizes.data(), 1); @@ -243,7 +236,7 @@ public: VkDescriptorSetLayoutCreateInfo descriptorLayout = vkTools::initializers::descriptorSetLayoutCreateInfo( setLayoutBindings.data(), - setLayoutBindings.size()); + static_cast(setLayoutBindings.size())); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); @@ -270,7 +263,7 @@ public: vkTools::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textures.colormap.descriptor), }; - vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); + vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); } void preparePipelines() @@ -320,7 +313,7 @@ public: VkPipelineDynamicStateCreateInfo dynamicState = vkTools::initializers::pipelineDynamicStateCreateInfo( dynamicStateEnables.data(), - dynamicStateEnables.size(), + static_cast(dynamicStateEnables.size()), 0); std::array shaderStages; @@ -346,19 +339,28 @@ public: // Host data to take specialization constants from struct SpecializationData { + // Sets the lighting model used in the fragment "uber" shader uint32_t lightingModel; + // Parameter for the toon shading part of the fragment shader + float toonDesaturationFactor = 0.5f; } specializationData; // Each shader constant of a shader stage corresponds to one map entry - std::array specializationMapEntries; + std::array specializationMapEntries; // Shader bindings based on specialization constants are marked by the new "constant_id" layout qualifier: // layout (constant_id = 0) const int LIGHTING_MODEL = 0; + // layout (constant_id = 1) const float PARAM_TOON_DESATURATION = 0.0f; // Map entry for the lighting model to be used by the fragment shader specializationMapEntries[0].constantID = 0; specializationMapEntries[0].size = sizeof(specializationData.lightingModel); specializationMapEntries[0].offset = 0; + // Map entry for the toon shader parameter + specializationMapEntries[1].constantID = 1; + specializationMapEntries[1].size = sizeof(specializationData.toonDesaturationFactor); + specializationMapEntries[1].offset = offsetof(SpecializationData, toonDesaturationFactor); + // Prepare specialization info block for the shader stage VkSpecializationInfo specializationInfo{}; specializationInfo.dataSize = sizeof(specializationData); @@ -404,15 +406,6 @@ public: void updateUniformBuffers() { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)(width / 3.0f) / (float)height, 0.1f, 256.0f); - - glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.modelView = viewMatrix * glm::translate(glm::mat4(), cameraPos); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - uboVS.projection = camera.matrices.perspective; uboVS.modelView = camera.matrices.view;