From a0d92be759450f09343bb04f4da913ccef505bbd Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 6 Mar 2022 22:45:08 -0500 Subject: [PATCH] Start to separate combined image samplers This is to be in line with requirements from HLSL/DX12 and WebGPU, both of which do not support this. It's probably better to get started removing our usage of them now :-) --- engine/gfx/public/gfx.hpp | 7 --- engine/gfx/vulkan/src/gfx_vulkan.cpp | 35 +----------- engine/gfx/vulkan/src/gfx_vulkan_texture.hpp | 1 - engine/renderer/include/renderer.hpp | 3 + engine/renderer/include/shadowpass.hpp | 2 + engine/renderer/src/dofpass.cpp | 7 +-- engine/renderer/src/imguipass.cpp | 2 +- engine/renderer/src/materialcompiler.cpp | 31 ++++++----- engine/renderer/src/renderer.cpp | 58 ++++++++------------ engine/renderer/src/scenecapture.cpp | 19 ++----- engine/renderer/src/shadowpass.cpp | 16 +++--- engine/renderer/src/smaapass.cpp | 12 ++-- engine/shaders/billboard.frag.glsl | 5 +- engine/shaders/imgui.frag.glsl | 5 +- engine/shaders/post.frag.glsl | 55 ++++++++++--------- engine/shaders/rendering.nocompile.glsl | 18 +++--- 16 files changed, 110 insertions(+), 166 deletions(-) diff --git a/engine/gfx/public/gfx.hpp b/engine/gfx/public/gfx.hpp index 49186f9..180d972 100755 --- a/engine/gfx/public/gfx.hpp +++ b/engine/gfx/public/gfx.hpp @@ -125,7 +125,6 @@ enum class GFXBindingType { StorageBuffer, StorageImage, PushConstant, - Texture, Sampler, SampledImage }; @@ -263,12 +262,6 @@ struct GFXTextureCreateInfo { GFXTextureUsage usage; int array_length = 1; int mip_count = 1; - - // sampler - SamplingMode samplingMode = SamplingMode::Repeat; - GFXBorderColor border_color = GFXBorderColor::OpaqueWhite; - bool compare_enabled = false; - GFXCompareFunction compare_function = GFXCompareFunction::Never; }; struct GFXSamplerCreateInfo { diff --git a/engine/gfx/vulkan/src/gfx_vulkan.cpp b/engine/gfx/vulkan/src/gfx_vulkan.cpp index fdce4bc..8d1dfb0 100755 --- a/engine/gfx/vulkan/src/gfx_vulkan.cpp +++ b/engine/gfx/vulkan/src/gfx_vulkan.cpp @@ -496,26 +496,6 @@ GFXTexture* GFXVulkan::create_texture(const GFXTextureCreateInfo& info) { vkCreateImageView(device, &viewInfo, nullptr, &texture->view); - const VkSamplerAddressMode samplerMode = toSamplerMode(info.samplingMode); - - // create sampler - VkSamplerCreateInfo samplerInfo = {}; - samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; - samplerInfo.magFilter = VK_FILTER_LINEAR; - samplerInfo.minFilter = VK_FILTER_LINEAR; - samplerInfo.addressModeU = samplerMode; - samplerInfo.addressModeV = samplerMode; - samplerInfo.addressModeW = samplerMode; - samplerInfo.anisotropyEnable = VK_TRUE; - samplerInfo.maxAnisotropy = 16; - samplerInfo.compareEnable = info.compare_enabled; - samplerInfo.borderColor = toBorderColor(info.border_color); - samplerInfo.compareOp = toCompareFunc(info.compare_function); - samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; - samplerInfo.maxLod = static_cast(info.mip_count); - - vkCreateSampler(device, &samplerInfo, nullptr, &texture->sampler); - return texture; } @@ -1028,11 +1008,6 @@ GFXPipeline* GFXVulkan::create_graphics_pipeline(const GFXGraphicsPipelineCreate case GFXBindingType::StorageBuffer: descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; break; - case GFXBindingType::Texture: { - descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - pipeline->bindings_marked_as_normal_images.push_back(binding.binding); - } - break; case GFXBindingType::StorageImage: { descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; @@ -1165,11 +1140,6 @@ GFXPipeline* GFXVulkan::create_compute_pipeline(const GFXComputePipelineCreateIn case GFXBindingType::StorageBuffer: descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; break; - case GFXBindingType::Texture: { - descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - pipeline->bindings_marked_as_normal_images.push_back(binding.binding); - } - break; case GFXBindingType::StorageImage: { descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; @@ -2144,7 +2114,7 @@ void GFXVulkan::createSwapchain(NativeSurface* native_surface, VkSwapchainKHR ol void GFXVulkan::createDescriptorPool() { const std::array poolSizes = { { {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 5000}, - {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 5000} + {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 5000} }}; VkDescriptorPoolCreateInfo poolInfo = {}; @@ -2234,7 +2204,6 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe VkDescriptorImageInfo imageInfo = {}; imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageInfo.imageView = vulkanTexture->view; - imageInfo.sampler = vulkanTexture->sampler; if((vulkanTexture->usage & GFXTextureUsage::Attachment) == GFXTextureUsage::Attachment) { if(vulkanTexture->format == VK_FORMAT_D32_SFLOAT) { @@ -2253,8 +2222,6 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe imageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL; } else if (utility::contains(pipeline->bindings_marked_as_sampled_images, i)) { descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; - } else { - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; } descriptorWrite.pImageInfo = &imageInfo; diff --git a/engine/gfx/vulkan/src/gfx_vulkan_texture.hpp b/engine/gfx/vulkan/src/gfx_vulkan_texture.hpp index c1f1acc..c43ec24 100755 --- a/engine/gfx/vulkan/src/gfx_vulkan_texture.hpp +++ b/engine/gfx/vulkan/src/gfx_vulkan_texture.hpp @@ -9,7 +9,6 @@ public: VkImage handle = VK_NULL_HANDLE; VkDeviceMemory memory = VK_NULL_HANDLE; VkImageView view = VK_NULL_HANDLE; - VkSampler sampler = VK_NULL_HANDLE; int width = 0, height = 0; diff --git a/engine/renderer/include/renderer.hpp b/engine/renderer/include/renderer.hpp index e83f51f..b03f3cc 100755 --- a/engine/renderer/include/renderer.hpp +++ b/engine/renderer/include/renderer.hpp @@ -14,6 +14,7 @@ #include "shadercompiler.hpp" #include "rendertarget.hpp" #include "platform.hpp" +#include "gfx_sampler.hpp" class GFX; class GFXBuffer; @@ -145,6 +146,8 @@ namespace prism { GFXBuffer* histogram_buffer = nullptr; GFXTexture* average_luminance_texture = nullptr; + GFXSampler* global_sampler = nullptr; + std::unique_ptr smaa_pass; std::unique_ptr dof_pass; diff --git a/engine/renderer/include/shadowpass.hpp b/engine/renderer/include/shadowpass.hpp index c72bda0..02f498d 100755 --- a/engine/renderer/include/shadowpass.hpp +++ b/engine/renderer/include/shadowpass.hpp @@ -58,4 +58,6 @@ private: // point GFXPipeline* static_point_pipeline = nullptr; GFXPipeline* skinned_point_pipeline = nullptr; + + GFXSampler* shadow_sampler = nullptr; }; diff --git a/engine/renderer/src/dofpass.cpp b/engine/renderer/src/dofpass.cpp index a4027b4..95432c2 100755 --- a/engine/renderer/src/dofpass.cpp +++ b/engine/renderer/src/dofpass.cpp @@ -34,8 +34,8 @@ DoFPass::DoFPass(GFX* gfx) { create_info.shader_input.bindings = { {0, GFXBindingType::StorageImage}, - {1, GFXBindingType::Texture}, - {3, GFXBindingType::Texture}, + {1, GFXBindingType::SampledImage}, + {3, GFXBindingType::SampledImage}, {2, GFXBindingType::PushConstant} }; @@ -59,8 +59,7 @@ DoFPass::DoFPass(GFX* gfx) { textureInfo.height = extent.height; textureInfo.format = GFXPixelFormat::RGBA_32F; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::Sampled; - textureInfo.samplingMode = SamplingMode::ClampToEdge; - + normal_field = gfx->create_texture(textureInfo); textureInfo.label = "Far Field"; diff --git a/engine/renderer/src/imguipass.cpp b/engine/renderer/src/imguipass.cpp index 86bbfb2..8eba875 100755 --- a/engine/renderer/src/imguipass.cpp +++ b/engine/renderer/src/imguipass.cpp @@ -63,7 +63,7 @@ void ImGuiPass::create_render_target_resources(RenderTarget& target) { createInfo.shader_input.bindings = { {1, GFXBindingType::PushConstant}, - {2, GFXBindingType::Texture} + {2, GFXBindingType::SampledImage} }; pipeline = engine->get_gfx()->create_graphics_pipeline(createInfo); diff --git a/engine/renderer/src/materialcompiler.cpp b/engine/renderer/src/materialcompiler.cpp index 0993ae7..f358c7a 100755 --- a/engine/renderer/src/materialcompiler.cpp +++ b/engine/renderer/src/materialcompiler.cpp @@ -151,8 +151,9 @@ layout(std430, binding = 1) buffer readonly SceneInformation {{ Probe probes[max_probes]; int numLights; }} scene; -layout (binding = 2) uniform sampler2D sun_shadow; -layout (binding = 6) uniform sampler2DArray spot_shadow; +layout (binding = 2) uniform texture2D sun_shadow; +layout (binding = 6) uniform texture2DArray spot_shadow; +layout (binding = 10) uniform sampler shadow_sampler; layout(push_constant) uniform PushConstant {{ mat4 model; }}; @@ -186,7 +187,7 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo if(render_options.enable_point_shadows) { format_to(std::back_inserter(src), R"(#define POINT_SHADOWS_SUPPORTED - layout (binding = 3) uniform samplerCubeArray point_shadow; + layout (binding = 3) uniform textureCubeArray point_shadow; )"); } @@ -194,9 +195,9 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo if(use_ibl) { format_to(std::back_inserter(src), - R"(layout (binding = 7) uniform samplerCubeArray irrandianceSampler; - layout (binding = 8) uniform samplerCubeArray prefilterSampler; - layout (binding = 9) uniform sampler2D brdfSampler; + R"(layout (binding = 7) uniform textureCubeArray irrandianceSampler; + layout (binding = 8) uniform textureCubeArray prefilterSampler; + layout (binding = 9) uniform texture2D brdfSampler; )"); } @@ -212,17 +213,17 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo material.bound_textures.clear(); // insert samplers as needed - int sampler_index = 10; + int sampler_index = 11; if(material.colorProperty.type == DataType::AssetTexture) { material.bound_textures[sampler_index] = material.colorProperty.value_tex; - format_to(std::back_inserter(src), "layout(binding = {}) uniform sampler2D colorTexture;\n", sampler_index++); + format_to(std::back_inserter(src), "layout(binding = {}) uniform texture2D colorTexture;\n", sampler_index++); } if(material.normalProperty.type == DataType::AssetTexture) { material.bound_textures[sampler_index] = material.normalProperty.value_tex; - format_to(std::back_inserter(src), "layout(binding = {}) uniform sampler2D normalTexture;\n", sampler_index++); + format_to(std::back_inserter(src), "layout(binding = {}) uniform texture2D normalTexture;\n", sampler_index++); } if(use_ibl) { @@ -262,7 +263,7 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo pos = step - step * noise.x; float shadow = 0.0; while(pos <= 1.0) {{ - vec3 tmp_normal = texture(normal_map, in_uv + dir * pos).rgb; + vec3 tmp_normal = texture(sampler2D(normal_map, shadow_sampler), in_uv + dir * pos).rgb; tmp_normal = in_tbn * (tmp_normal * 2.0 - 1.0); float tmp_lighting = dot(light_dir, tmp_normal); float shadowed = -tmp_lighting; @@ -283,9 +284,9 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo R"(vec3 ibl(const int probe, const ComputedSurfaceInfo surface_info, const float intensity) {{ const vec3 F = fresnel_schlick_roughness(surface_info.NdotV, surface_info.F0, surface_info.roughness); const vec3 R = get_reflect(probe, surface_info.N); - const vec2 brdf = texture(brdfSampler, vec2(surface_info.NdotV, surface_info.roughness)).rg; - const vec3 sampledIrradiance = texture(irrandianceSampler, vec4(surface_info.N, probe)).xyz; - const vec3 prefilteredColor = textureLod(prefilterSampler, vec4(R, probe), surface_info.roughness * 4).xyz; + const vec2 brdf = texture(sampler2D(brdfSampler, shadow_sampler), vec2(surface_info.NdotV, surface_info.roughness)).rg; + const vec3 sampledIrradiance = texture(samplerCubeArray(irrandianceSampler, shadow_sampler), vec4(surface_info.N, probe)).xyz; + const vec3 prefilteredColor = textureLod(samplerCubeArray(prefilterSampler, shadow_sampler), vec4(R, probe), surface_info.roughness * 4).xyz; const vec3 diffuse = sampledIrradiance * surface_info.diffuse_color; const vec3 specular = prefilteredColor * (F * brdf.x + brdf.y); return (diffuse + specular) * intensity; @@ -298,7 +299,7 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo if(material.colorProperty.type == DataType::Vector3) { format_to(std::back_inserter(src), "vec3 Color = vec3({}, {}, {});\n", material.colorProperty.value.x, material.colorProperty.value.y, material.colorProperty.value.z); } else if(material.colorProperty.type == DataType::AssetTexture) { - format_to(std::back_inserter(src), "vec3 Color = texture(colorTexture, in_uv).rgb;\n"); + format_to(std::back_inserter(src), "vec3 Color = texture(sampler2D(colorTexture, shadow_sampler), in_uv).rgb;\n"); } else if(material.colorProperty.type == DataType::Float) { format_to(std::back_inserter(src),"vec3 Color = vec3({}});\n", material.colorProperty.float_value); } @@ -313,7 +314,7 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo prism::log("enabling normal mapping on material.."); format_to(std::back_inserter(src), - R"(vec3 final_normal = texture(normalTexture, in_uv).rgb; + R"(vec3 final_normal = texture(sampler2D(normalTexture, shadow_sampler), in_uv).rgb; final_normal = final_normal * 2.0 - 1.0; final_normal = in_tbn * final_normal; )"); diff --git a/engine/renderer/src/renderer.cpp b/engine/renderer/src/renderer.cpp index 35da550..e451ac0 100755 --- a/engine/renderer/src/renderer.cpp +++ b/engine/renderer/src/renderer.cpp @@ -88,6 +88,10 @@ renderer::renderer(GFX* gfx, const bool enable_imgui) : gfx(gfx) { unorm_render_pass = gfx->create_render_pass(renderPassInfo); create_sky_pipeline(); + + GFXSamplerCreateInfo sampler_create_info = {}; + + global_sampler = gfx->create_sampler(sampler_create_info); } renderer::~renderer() = default; @@ -277,6 +281,8 @@ void renderer::render(GFXCommandBuffer* commandbuffer, Scene* scene, RenderTarge else commandbuffer->bind_texture(dummy_texture, 7); + commandbuffer->bind_sampler(global_sampler, 8); + PostPushConstants pc; pc.options.x = render_options.enable_aa; pc.options.z = render_options.exposure; @@ -412,6 +418,7 @@ void renderer::render_camera(GFXCommandBuffer* command_buffer, Scene& scene, Obj command_buffer->bind_texture(scene.irradianceCubeArray, 7); command_buffer->bind_texture(scene.prefilteredCubeArray, 8); command_buffer->bind_texture(brdf_texture, 9); + command_buffer->bind_sampler(global_sampler, 10); if(!mesh.mesh->bones.empty()) command_buffer->bind_shader_buffer(part.bone_batrix_buffer, 0, 14, sizeof(Matrix4x4) * 128); @@ -488,12 +495,13 @@ void renderer::create_mesh_pipeline(Material& material) const { pipelineInfo.shader_input.bindings = { {1, GFXBindingType::StorageBuffer}, {0, GFXBindingType::PushConstant}, - {2, GFXBindingType::Texture}, - {3, GFXBindingType::Texture}, - {6, GFXBindingType::Texture}, - {7, GFXBindingType::Texture}, - {8, GFXBindingType::Texture}, - {9, GFXBindingType::Texture} + {2, GFXBindingType::SampledImage}, + {3, GFXBindingType::SampledImage}, + {6, GFXBindingType::SampledImage}, + {7, GFXBindingType::SampledImage}, + {8, GFXBindingType::SampledImage}, + {9, GFXBindingType::SampledImage}, + {10, GFXBindingType::Sampler} }; pipelineInfo.render_pass = offscreen_render_pass; @@ -507,7 +515,7 @@ void renderer::create_mesh_pipeline(Material& material) const { for (auto [index, texture] : material.bound_textures) { GFXShaderBinding binding; binding.binding = index; - binding.type = GFXBindingType::Texture; + binding.type = GFXBindingType::SampledImage; pipelineInfo.shader_input.bindings.push_back(binding); } @@ -550,7 +558,6 @@ void renderer::create_render_target_resources(RenderTarget& target) { textureInfo.height = extent.height; textureInfo.format = GFXPixelFormat::RGBA_32F; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::Sampled | GFXTextureUsage::Storage; - textureInfo.samplingMode = SamplingMode::ClampToEdge; target.offscreenColorTexture = gfx->create_texture(textureInfo); @@ -568,27 +575,7 @@ void renderer::create_render_target_resources(RenderTarget& target) { target.offscreenFramebuffer = gfx->create_framebuffer(framebufferInfo); if(post_pipeline == nullptr) { - GFXGraphicsPipelineCreateInfo pipelineInfo = {}; - pipelineInfo.label = "Post"; - - pipelineInfo.shaders.vertex_src = ShaderSource(prism::path("post.vert")); - pipelineInfo.shaders.fragment_src = ShaderSource(prism::path("post.frag")); - - pipelineInfo.shader_input.bindings = { - {4, GFXBindingType::PushConstant}, - {1, GFXBindingType::Texture}, - {2, GFXBindingType::Texture}, - {3, GFXBindingType::Texture}, - {5, GFXBindingType::Texture}, - {6, GFXBindingType::Texture}, - {7, GFXBindingType::Texture} - }; - - pipelineInfo.shader_input.push_constants = { - {sizeof(PostPushConstants), 0} - }; - - post_pipeline = gfx->create_graphics_pipeline(pipelineInfo); + create_post_pipelines(); } target.sceneBuffer = gfx->create_buffer(nullptr, sizeof(SceneInformation), true, GFXBufferUsage::Storage); @@ -603,12 +590,13 @@ void renderer::create_post_pipelines() { pipelineInfo.shader_input.bindings = { {4, GFXBindingType::PushConstant}, - {1, GFXBindingType::Texture}, - {2, GFXBindingType::Texture}, - {3, GFXBindingType::Texture}, - {5, GFXBindingType::Texture}, - {6, GFXBindingType::Texture}, - {7, GFXBindingType::Texture} + {1, GFXBindingType::SampledImage}, + {2, GFXBindingType::SampledImage}, + {3, GFXBindingType::SampledImage}, + {5, GFXBindingType::SampledImage}, + {6, GFXBindingType::SampledImage}, + {7, GFXBindingType::SampledImage}, + {8, GFXBindingType::Sampler} }; pipelineInfo.shader_input.push_constants = { diff --git a/engine/renderer/src/scenecapture.cpp b/engine/renderer/src/scenecapture.cpp index a54d586..f145753 100755 --- a/engine/renderer/src/scenecapture.cpp +++ b/engine/renderer/src/scenecapture.cpp @@ -84,8 +84,7 @@ SceneCapture::SceneCapture(GFX* gfx) { textureInfo.height = scene_cubemap_resolution; textureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::TransferSrc | GFXTextureUsage::Sampled; - textureInfo.samplingMode = SamplingMode::ClampToEdge; - + offscreenTexture = gfx->create_texture(textureInfo); GFXTextureCreateInfo depthTextureInfo = {}; @@ -94,8 +93,7 @@ SceneCapture::SceneCapture(GFX* gfx) { depthTextureInfo.height = scene_cubemap_resolution; depthTextureInfo.format = GFXPixelFormat::DEPTH_32F; depthTextureInfo.usage = GFXTextureUsage::Attachment; - depthTextureInfo.samplingMode = SamplingMode::ClampToEdge; - + offscreenDepth = gfx->create_texture(depthTextureInfo); GFXFramebufferCreateInfo info; @@ -111,7 +109,6 @@ SceneCapture::SceneCapture(GFX* gfx) { cubeTextureInfo.height = scene_cubemap_resolution; cubeTextureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; cubeTextureInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst | GFXTextureUsage::TransferSrc; // dst used for cubemap copy, src used for mipmap gen - cubeTextureInfo.samplingMode = SamplingMode::ClampToEdge; cubeTextureInfo.mip_count = mipLevels; environmentCube = gfx->create_texture(cubeTextureInfo); @@ -134,7 +131,6 @@ void SceneCapture::create_scene_resources(Scene& scene) { cubeTextureInfo.height = irradiance_cubemap_resolution; cubeTextureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; cubeTextureInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst; - cubeTextureInfo.samplingMode = SamplingMode::ClampToEdge; cubeTextureInfo.array_length = max_environment_probes; scene.irradianceCubeArray = gfx->create_texture(cubeTextureInfo); @@ -146,7 +142,6 @@ void SceneCapture::create_scene_resources(Scene& scene) { cubeTextureInfo.height = scene_cubemap_resolution; cubeTextureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; cubeTextureInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst; - cubeTextureInfo.samplingMode = SamplingMode::ClampToEdge; cubeTextureInfo.array_length = max_environment_probes; cubeTextureInfo.mip_count = mipLevels; @@ -436,8 +431,7 @@ void SceneCapture::createIrradianceResources() { textureInfo.height = irradiance_cubemap_resolution; textureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::TransferSrc; - textureInfo.samplingMode = SamplingMode::ClampToEdge; - + irradianceOffscreenTexture = gfx->create_texture(textureInfo); GFXRenderPassCreateInfo renderPassInfo = {}; @@ -474,7 +468,7 @@ void SceneCapture::createIrradianceResources() { pipelineInfo.shader_input.bindings = { {1, GFXBindingType::PushConstant}, - {2, GFXBindingType::Texture} + {2, GFXBindingType::SampledImage} }; pipelineInfo.render_pass = irradianceRenderPass; @@ -491,8 +485,7 @@ void SceneCapture::createPrefilterResources() { textureInfo.height = scene_cubemap_resolution; textureInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::TransferSrc; - textureInfo.samplingMode = SamplingMode::ClampToEdge; - + prefilteredOffscreenTexture = gfx->create_texture(textureInfo); GFXFramebufferCreateInfo info; @@ -528,7 +521,7 @@ void SceneCapture::createPrefilterResources() { pipelineInfo.shader_input.bindings = { {1, GFXBindingType::PushConstant}, - {2, GFXBindingType::Texture} + {2, GFXBindingType::SampledImage} }; pipelineInfo.render_pass = irradianceRenderPass; diff --git a/engine/renderer/src/shadowpass.cpp b/engine/renderer/src/shadowpass.cpp index be6606a..ee9f6da 100755 --- a/engine/renderer/src/shadowpass.cpp +++ b/engine/renderer/src/shadowpass.cpp @@ -42,8 +42,6 @@ void ShadowPass::create_scene_resources(Scene& scene) { textureInfo.height = render_options.shadow_resolution; textureInfo.format = GFXPixelFormat::DEPTH_32F; textureInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::Attachment; - textureInfo.samplingMode = SamplingMode::ClampToBorder; - textureInfo.border_color = GFXBorderColor::OpaqueWhite; scene.depthTexture = gfx->create_texture(textureInfo); @@ -64,8 +62,6 @@ void ShadowPass::create_scene_resources(Scene& scene) { cubeTextureInfo.format = GFXPixelFormat::R_32F; cubeTextureInfo.usage = GFXTextureUsage::Sampled; cubeTextureInfo.array_length = max_point_shadows; - cubeTextureInfo.samplingMode = SamplingMode::ClampToBorder; - cubeTextureInfo.border_color = GFXBorderColor::OpaqueWhite; scene.pointLightArray = gfx->create_texture(cubeTextureInfo); } @@ -80,11 +76,15 @@ void ShadowPass::create_scene_resources(Scene& scene) { spotTextureInfo.format = GFXPixelFormat::DEPTH_32F; spotTextureInfo.usage = GFXTextureUsage::Sampled; spotTextureInfo.array_length = max_spot_shadows; - spotTextureInfo.samplingMode = SamplingMode::ClampToBorder; - spotTextureInfo.border_color = GFXBorderColor::OpaqueWhite; scene.spotLightArray = gfx->create_texture(spotTextureInfo); } + + GFXSamplerCreateInfo sampler_create_info = {}; + sampler_create_info.samplingMode = SamplingMode::ClampToBorder; + sampler_create_info.border_color = GFXBorderColor::OpaqueWhite; + + shadow_sampler = gfx->create_sampler(sampler_create_info); } void ShadowPass::render(GFXCommandBuffer* command_buffer, Scene& scene) { @@ -391,7 +391,6 @@ void ShadowPass::create_offscreen_resources() { textureInfo.height = render_options.shadow_resolution; textureInfo.format = GFXPixelFormat::R_32F; textureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::Sampled; - textureInfo.samplingMode = SamplingMode::ClampToEdge; offscreen_color_texture = gfx->create_texture(textureInfo); @@ -401,8 +400,7 @@ void ShadowPass::create_offscreen_resources() { depthTextureInfo.height = render_options.shadow_resolution; depthTextureInfo.format = GFXPixelFormat::DEPTH_32F; depthTextureInfo.usage = GFXTextureUsage::Attachment | GFXTextureUsage::Sampled; - depthTextureInfo.samplingMode = SamplingMode::ClampToEdge; - + offscreen_depth = gfx->create_texture(depthTextureInfo); GFXFramebufferCreateInfo info; diff --git a/engine/renderer/src/smaapass.cpp b/engine/renderer/src/smaapass.cpp index 8b9e0f6..ef8bcd7 100755 --- a/engine/renderer/src/smaapass.cpp +++ b/engine/renderer/src/smaapass.cpp @@ -101,8 +101,7 @@ void SMAAPass::create_textures() { areaInfo.height = AREATEX_HEIGHT; areaInfo.format = GFXPixelFormat::R8G8_UNORM; areaInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst; - areaInfo.samplingMode = SamplingMode::ClampToEdge; - + area_image = gfx->create_texture(areaInfo); gfx->copy_texture(area_image, (void*)areaTexBytes, AREATEX_SIZE); @@ -114,8 +113,7 @@ void SMAAPass::create_textures() { searchInfo.height = SEARCHTEX_HEIGHT; searchInfo.format = GFXPixelFormat::R8_UNORM; searchInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst; - searchInfo.samplingMode = SamplingMode::ClampToEdge; - + search_image = gfx->create_texture(searchInfo); gfx->copy_texture(search_image, (void*)searchTexBytes, SEARCHTEX_SIZE); @@ -145,8 +143,8 @@ void SMAAPass::create_pipelines() { }; createInfo.shader_input.bindings = { - {0, GFXBindingType::Texture}, - {1, GFXBindingType::Texture}, + {0, GFXBindingType::SampledImage}, + {1, GFXBindingType::SampledImage}, {2, GFXBindingType::PushConstant} }; @@ -155,7 +153,7 @@ void SMAAPass::create_pipelines() { createInfo.label = "SMAA Blend"; createInfo.shaders.vertex_src = ShaderSource(prism::path("blend.vert")); createInfo.shaders.fragment_src = ShaderSource(prism::path("blend.frag")); - createInfo.shader_input.bindings.push_back({3, GFXBindingType::Texture}); + createInfo.shader_input.bindings.push_back({3, GFXBindingType::SampledImage}); blend_pipeline = gfx->create_graphics_pipeline(createInfo); } diff --git a/engine/shaders/billboard.frag.glsl b/engine/shaders/billboard.frag.glsl index f195420..2a42a17 100644 --- a/engine/shaders/billboard.frag.glsl +++ b/engine/shaders/billboard.frag.glsl @@ -7,11 +7,12 @@ layout(push_constant) uniform readonly PushConstant{ vec4 color; }; -layout (binding = 2) uniform sampler2D colorSampler; +layout (binding = 2) uniform texture2D colorSampler; +layout (binding = 3) uniform sampler testSampler; void main() { if(inUV.y < 0.0 || inUV.x > 1.0) discard; - outColor = texture(colorSampler, inUV) * color; + outColor = texture(sampler2D(colorSampler, testSampler), inUV) * color; } diff --git a/engine/shaders/imgui.frag.glsl b/engine/shaders/imgui.frag.glsl index 3db777c..d2f07b0 100644 --- a/engine/shaders/imgui.frag.glsl +++ b/engine/shaders/imgui.frag.glsl @@ -3,8 +3,9 @@ layout(location = 1) in vec2 in_uv; layout(location = 0) out vec4 out_color; -layout(binding = 2) uniform sampler2D bound_texture; +layout(binding = 2) uniform texture2D bound_texture; +layout(binding = 3) uniform sampler testSampler; void main() { - out_color = in_color * texture(bound_texture, in_uv); + out_color = in_color * texture(sampler2D(bound_texture, testSampler), in_uv); } diff --git a/engine/shaders/post.frag.glsl b/engine/shaders/post.frag.glsl index 36e1d4a..b79b801 100644 --- a/engine/shaders/post.frag.glsl +++ b/engine/shaders/post.frag.glsl @@ -19,30 +19,31 @@ layout(location = 1) in vec4 inOffset; layout (location = 0) out vec4 outColor; -layout (binding = 1) uniform sampler2D colorSampler; -layout (binding = 2) uniform sampler2D backSampler; -layout (binding = 3) uniform sampler2D blendSampler; -layout (binding = 5) uniform sampler2D sobelSampler; -layout (binding = 6) uniform sampler2D averageLuminanceSampler; -layout (binding = 7) uniform sampler2D farFieldSampler; +layout (binding = 1) uniform texture2D colorSampler; +layout (binding = 2) uniform texture2D backSampler; +layout (binding = 3) uniform texture2D blendSampler; +layout (binding = 5) uniform texture2D sobelSampler; +layout (binding = 6) uniform texture2D averageLuminanceSampler; +layout (binding = 7) uniform texture2D farFieldSampler; +layout (binding = 8) uniform sampler global_sampler; float calculate_sobel() { float x = 1.0 / viewport.z; float y = 1.0 / viewport.w; vec4 horizEdge = vec4( 0.0 ); - horizEdge -= texture( sobelSampler, vec2( inUV.x - x, inUV.y - y ) ) * 1.0; - horizEdge -= texture( sobelSampler, vec2( inUV.x - x, inUV.y ) ) * 2.0; - horizEdge -= texture( sobelSampler, vec2( inUV.x - x, inUV.y + y ) ) * 1.0; - horizEdge += texture( sobelSampler, vec2( inUV.x + x, inUV.y - y ) ) * 1.0; - horizEdge += texture( sobelSampler, vec2( inUV.x + x, inUV.y ) ) * 2.0; - horizEdge += texture( sobelSampler, vec2( inUV.x + x, inUV.y + y ) ) * 1.0; + horizEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x - x, inUV.y - y ) ) * 1.0; + horizEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x - x, inUV.y ) ) * 2.0; + horizEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x - x, inUV.y + y ) ) * 1.0; + horizEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x + x, inUV.y - y ) ) * 1.0; + horizEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x + x, inUV.y ) ) * 2.0; + horizEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x + x, inUV.y + y ) ) * 1.0; vec4 vertEdge = vec4( 0.0 ); - vertEdge -= texture( sobelSampler, vec2( inUV.x - x, inUV.y - y ) ) * 1.0; - vertEdge -= texture( sobelSampler, vec2( inUV.x , inUV.y - y ) ) * 2.0; - vertEdge -= texture( sobelSampler, vec2( inUV.x + x, inUV.y - y ) ) * 1.0; - vertEdge += texture( sobelSampler, vec2( inUV.x - x, inUV.y + y ) ) * 1.0; - vertEdge += texture( sobelSampler, vec2( inUV.x , inUV.y + y ) ) * 2.0; - vertEdge += texture( sobelSampler, vec2( inUV.x + x, inUV.y + y ) ) * 1.0; + vertEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x - x, inUV.y - y ) ) * 1.0; + vertEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x , inUV.y - y ) ) * 2.0; + vertEdge -= texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x + x, inUV.y - y ) ) * 1.0; + vertEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x - x, inUV.y + y ) ) * 1.0; + vertEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x , inUV.y + y ) ) * 2.0; + vertEdge += texture(sampler2D(sobelSampler, global_sampler), vec2( inUV.x + x, inUV.y + y ) ) * 1.0; return sqrt((horizEdge.rgb * horizEdge.rgb) + (vertEdge.rgb * vertEdge.rgb)).r; } @@ -106,31 +107,31 @@ float reinhard2(float _x, float _whiteSqr) void main() { // passthrough if(options.w == 1) { - outColor = texture(colorSampler, inUV); + outColor = texture(sampler2D(colorSampler, global_sampler), inUV); return; } bool enable_dof = options.w == 2; vec3 sceneColor = vec3(0); if(enable_dof) { - sceneColor = texture(farFieldSampler, inUV).rgb; - sceneColor += texture(colorSampler, inUV).rgb; + sceneColor = texture(sampler2D(farFieldSampler, global_sampler), inUV).rgb; + sceneColor += texture(sampler2D(colorSampler, global_sampler), inUV).rgb; } else { - if(options.x == 1) // enable AA - sceneColor = SMAANeighborhoodBlendingPS(inUV, inOffset, colorSampler, blendSampler).rgb; - else - sceneColor = texture(colorSampler, inUV).rgb; + //if(options.x == 1) // enable AA + // sceneColor = SMAANeighborhoodBlendingPS(inUV, inOffset, colorSampler, blendSampler).rgb; + //else + sceneColor = texture(sampler2D(colorSampler, global_sampler), inUV).rgb; } float sobel = 0.0; - if(textureSize(sobelSampler, 0).x > 1) + if(textureSize(sampler2D(sobelSampler, global_sampler), 0).x > 1) sobel = calculate_sobel(); vec3 sobelColor = vec3(0, 1, 1); vec3 hdrColor = sceneColor; // fading removed - float avg_lum = texture(averageLuminanceSampler, inUV).r; + float avg_lum = texture(sampler2D(averageLuminanceSampler, global_sampler), inUV).r; vec3 transformed_color = hdrColor; diff --git a/engine/shaders/rendering.nocompile.glsl b/engine/shaders/rendering.nocompile.glsl index d579267..9d7a40a 100644 --- a/engine/shaders/rendering.nocompile.glsl +++ b/engine/shaders/rendering.nocompile.glsl @@ -66,7 +66,7 @@ struct ComputedLightInformation { float pcf_sun(const vec4 shadowCoords, const float uvRadius) { float sum = 0; for(int i = 0; i < 16; i++) { - const float z = texture(sun_shadow, shadowCoords.xy + PoissionPCFDisk[i] * uvRadius).r; + const float z = texture(sampler2D(sun_shadow, shadow_sampler), shadowCoords.xy + PoissionPCFDisk[i] * uvRadius).r; sum += (z < (shadowCoords.z - 0.005)) ? 0.0 : 1.0; } @@ -91,7 +91,7 @@ void blocker_distance_sun(const vec3 shadowCoords, const float uvLightSize, inou blockers = 0; for(int i = 0; i < numBlockerSearchSamples; i++) { - const float z = texture(sun_shadow, shadowCoords.xy + PoissionPCFDisk[i] * searchWidth).r; + const float z = texture(sampler2D(sun_shadow, shadow_sampler), shadowCoords.xy + PoissionPCFDisk[i] * searchWidth).r; if(z < shadowCoords.z) { blockerSum += z; blockers++; @@ -126,7 +126,7 @@ ComputedLightInformation calculate_sun(Light light) { if(shadowCoords.z > -1.0 && shadowCoords.z < 1.0) { #ifdef SHADOW_FILTER_NONE - shadow = (texture(sun_shadow, shadowCoords.xy).r < shadowCoords.z - 0.005) ? 0.0 : 1.0; + shadow = (texture(sampler2D(sun_shadow, shadow_sampler), shadowCoords.xy).r < shadowCoords.z - 0.005) ? 0.0 : 1.0; #endif #ifdef SHADOW_FILTER_PCF shadow = pcf_sun(shadowCoords, 0.1); @@ -145,7 +145,7 @@ ComputedLightInformation calculate_sun(Light light) { float pcf_spot(const vec4 shadowCoords, const int index, const float uvRadius) { float sum = 0; for(int i = 0; i < 16; i++) { - const float z = texture(spot_shadow, vec3(shadowCoords.xy + PoissionPCFDisk[i] * uvRadius, index)).r; + const float z = texture(sampler2DArray(spot_shadow, shadow_sampler), vec3(shadowCoords.xy + PoissionPCFDisk[i] * uvRadius, index)).r; sum += (z < (shadowCoords.z - 0.001)) ? 0.0 : 1.0; } @@ -160,7 +160,7 @@ void blocker_distance_spot(const vec3 shadowCoords, const int index, const float blockers = 0; for(int i = 0; i < numBlockerSearchSamples; i++) { - const float z = texture(spot_shadow, vec3(shadowCoords.xy + PoissionPCFDisk[i] * searchWidth, index)).r; + const float z = texture(sampler2DArray(spot_shadow, shadow_sampler), vec3(shadowCoords.xy + PoissionPCFDisk[i] * searchWidth, index)).r; if(z < shadowCoords.z) { blockerSum += z; blockers++; @@ -197,7 +197,7 @@ ComputedLightInformation calculate_spot(Light light) { const vec4 shadowCoord = fragPostSpotLightSpace[last_spot_light] / fragPostSpotLightSpace[last_spot_light].w; #ifdef SHADOW_FILTER_NONE - shadow = (texture(spot_shadow, vec3(shadowCoord.xy, last_spot_light)).r < shadowCoord.z - 0.005) ? 0.0 : 1.0; + shadow = (texture(sampler2D(spot_shadow, shadow_sampler), vec3(shadowCoord.xy, last_spot_light)).r < shadowCoord.z - 0.005) ? 0.0 : 1.0; #endif #ifdef SHADOW_FILTER_PCF shadow = pcf_spot(shadowCoord, last_spot_light, 0.01); @@ -235,7 +235,7 @@ vec3 sampleOffsetDirections[20] = vec3[] float pcf_point(const vec3 shadowCoords, const int index, const float uvRadius) { float sum = 0; for(int i = 0; i < 20; i++) { - const float z = texture(point_shadow, vec4(shadowCoords.xyz + sampleOffsetDirections[i] * uvRadius, index)).r; + const float z = texture(samplerCubeArray(point_shadow, shadow_sampler), vec4(shadowCoords.xyz + sampleOffsetDirections[i] * uvRadius, index)).r; sum += (z < length(shadowCoords) - 0.05) ? 0.0 : 1.0; } @@ -250,7 +250,7 @@ void blocker_distance_point(const vec3 shadowCoords, const int index, const floa blockers = 0; for(int i = 0; i < 20; i++) { - const float z = texture(point_shadow, vec4(shadowCoords + sampleOffsetDirections[i] * 0.05, index)).r; + const float z = texture(samplerCubeArray(point_shadow, shadow_sampler), vec4(shadowCoords + sampleOffsetDirections[i] * 0.05, index)).r; if(z < length(shadowCoords)) { blockerSum += z; blockers++; @@ -289,7 +289,7 @@ ComputedLightInformation calculate_point(Light light) { #ifdef POINT_SHADOWS_SUPPORTED if(light.shadowsEnable.x == 1.0) { #ifdef SHADOW_FILTER_NONE - const float sampledDist = texture(point_shadow, vec4(lightVec, int(light.shadowsEnable.z))).r; + const float sampledDist = texture(sampler2D(point_shadow, shadow_sampler), vec4(lightVec, int(light.shadowsEnable.z))).r; const float dist = length(lightVec); shadow = (dist <= sampledDist + 0.05) ? 1.0 : 0.0;