diff --git a/resource/content/shaders/Tris.vs.spv b/resource/content/shaders/Tris.vs.spv index ba8ebf7..c49d17f 100644 --- a/resource/content/shaders/Tris.vs.spv +++ b/resource/content/shaders/Tris.vs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2ec9a7ac1fde5472e4d1e1136e2adcd8b19ce9b51c3224f38a6458e4ec5dad09 -size 1872 +oid sha256:e3ebf7b30e7fcda926bddd63a4f3e3c7d78b8b2945b8d75f77621a8c58ed84d0 +size 1876 diff --git a/resource/shaders-src/Tris.vert b/resource/shaders-src/Tris.vert index 5aa7b41..ff0f539 100644 --- a/resource/shaders-src/Tris.vert +++ b/resource/shaders-src/Tris.vert @@ -7,7 +7,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 proj; } ubo; -layout(location = 0) in vec2 inPosition; +layout(location = 0) in vec3 inPosition; layout(location = 1) in vec3 inColor; layout(location = 2) in vec2 inTexCoord; @@ -15,7 +15,7 @@ layout(location = 0) out vec3 fragColor; layout(location = 1) out vec2 fragTexCoord; void main() { - gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); + gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); fragColor = inColor; fragTexCoord = inTexCoord; } \ No newline at end of file diff --git a/resource/shaders-src/compile.sh b/resource/shaders-src/compile.sh index 8310056..3fa18a6 100755 --- a/resource/shaders-src/compile.sh +++ b/resource/shaders-src/compile.sh @@ -2,7 +2,7 @@ BASEDIR=$(dirname "$0") TARGETDIR="$BASEDIR/../content/shaders" -GLSL="glslc" +GLSL=${GLSL:="glslc"} rm $TARGETDIR/*.spv diff --git a/src/client/render/api/Images.hpp b/src/client/render/api/Images.hpp index fdb5a8d..8c3dc4d 100644 --- a/src/client/render/api/Images.hpp +++ b/src/client/render/api/Images.hpp @@ -23,7 +23,7 @@ public: BC2 = 136, /// DXT1 RGBA SRGB BC3 = 138, - // MAYBE: BMP RGBA SRGB ?? + // MAYBE: R8G8B8A8 // MAYBE: For HDR BC6H_SFLOAT = 144, }; // NOTE: matches VkImageLayout @@ -58,6 +58,13 @@ public: friend inline Usage operator|(Usage a, Usage b) { return static_cast(static_cast(a) | static_cast(b)); } + //NOTE: matches VkImageAspectFlags + enum class Aspect { + COLOR = 0x00000001, + DEPTH = 0x00000002, + STENCIL = 0x00000004, + METADATA = 0x00000008 + }; struct properties { frame size; @@ -69,6 +76,7 @@ public: Layout layout; Usage usage; bool linear = false; + Aspect aspect = Aspect::COLOR; }; static std::optional Read(const std::string&, std::vector& data); diff --git a/src/client/render/vk/Allocator.cpp b/src/client/render/vk/Allocator.cpp index 6f8a5f1..9a87c22 100644 --- a/src/client/render/vk/Allocator.cpp +++ b/src/client/render/vk/Allocator.cpp @@ -206,12 +206,21 @@ void Allocator::transitionImageLayout(VkImage image, VkFormat format, VkImageLay barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.image = image; - barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; barrier.subresourceRange.baseMipLevel = 0; barrier.subresourceRange.levelCount = 1; barrier.subresourceRange.baseArrayLayer = 0; barrier.subresourceRange.layerCount = 1; + if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { + barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; + + if (PhysicalDeviceInfo::HasStencilComponent(format)) { + barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; + } + } else { + barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + } + VkPipelineStageFlags sourceStage; VkPipelineStageFlags destinationStage; @@ -227,6 +236,12 @@ void Allocator::transitionImageLayout(VkImage image, VkFormat format, VkImageLay sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT; destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { + barrier.srcAccessMask = 0; + barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + + sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; + destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; } else { FATAL("Unsupported layout transition!"); } diff --git a/src/client/render/vk/CommandCenter.cpp b/src/client/render/vk/CommandCenter.cpp index 456a342..491e30f 100644 --- a/src/client/render/vk/CommandCenter.cpp +++ b/src/client/render/vk/CommandCenter.cpp @@ -34,6 +34,7 @@ CommandCenter::CommandCenter(VkDevice device, const PhysicalDeviceInfo &info, co FATAL("Failed to create texture sampler!"); } } + depthFormat = info.findDepthFormat(); } CommandCenter::~CommandCenter() { if(!freed) @@ -45,16 +46,19 @@ CommandCenter::~CommandCenter() { void CommandCenter::allocate(const std::vector& views, const Pipeline& pipe, VkExtent2D extent, const renderOptions& opt) { assert(freed); + depthbuffer = Image::Create({{{extent.height, extent.width}, 1, (Image::Format)depthFormat}, + Image::Layout::DEPTH_STENCIL_ATTACHMENT, Image::Usage::DEPTH_STENCIL_ATTACHMENT, false, Image::Aspect::DEPTH}); + framebuffers.resize(views.size()); for (size_t i = 0; i < views.size(); i++) { - VkImageView attachments[] = { views[i] }; + std::array attachments = { views[i], depthbuffer->getView() }; VkFramebufferCreateInfo framebufferInfo{}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = pipe.getRenderPass(); - framebufferInfo.attachmentCount = 1; - framebufferInfo.pAttachments = attachments; + framebufferInfo.attachmentCount = attachments.size(); + framebufferInfo.pAttachments = attachments.data(); framebufferInfo.width = extent.width; framebufferInfo.height = extent.height; framebufferInfo.layers = 1; @@ -162,9 +166,11 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli renderPassInfo.renderArea.offset = {0, 0}; renderPassInfo.renderArea.extent = extent; - VkClearValue clearColor = {opt.clear_color.x, opt.clear_color.y, opt.clear_color.z, opt.clear_color.a}; - renderPassInfo.clearValueCount = 1; //TODO: clear depth - renderPassInfo.pClearValues = &clearColor; + std::array clearValues{}; + clearValues[0].color = {opt.clear_color.x, opt.clear_color.y, opt.clear_color.z, opt.clear_color.a}; + clearValues[1].depthStencil = {1.0f, 0}; + renderPassInfo.clearValueCount = clearValues.size(); + renderPassInfo.pClearValues = clearValues.data(); vkCmdBeginRenderPass(graphicsBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBindPipeline(graphicsBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.getPipeline()); @@ -181,7 +187,7 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli } } - proj = glm::perspective(glm::radians(45.0f), extent.width / (float) extent.height, 0.1f, 10.0f); + proj = glm::perspectiveZO(glm::radians(45.0f), extent.width / (float) extent.height, 0.1f, 10.0f); proj[1][1] *= -1; freed = false; @@ -192,6 +198,8 @@ void CommandCenter::free() { vkDestroyDescriptorPool(device, descriptorPool, nullptr); + depthbuffer.reset(); + for (size_t i = 0; i < framebuffers.size(); i++) { vkDestroyFramebuffer(device, framebuffers[i], nullptr); } diff --git a/src/client/render/vk/CommandCenter.hpp b/src/client/render/vk/CommandCenter.hpp index 684a169..e292497 100644 --- a/src/client/render/vk/CommandCenter.hpp +++ b/src/client/render/vk/CommandCenter.hpp @@ -25,6 +25,8 @@ private: VkDevice device; std::vector framebuffers; + VkFormat depthFormat; + std::unique_ptr depthbuffer; VkQueue graphicsQueue; VkCommandPool graphicsPool; diff --git a/src/client/render/vk/PhysicalDeviceInfo.cpp b/src/client/render/vk/PhysicalDeviceInfo.cpp index 5ccb4f7..ad46911 100644 --- a/src/client/render/vk/PhysicalDeviceInfo.cpp +++ b/src/client/render/vk/PhysicalDeviceInfo.cpp @@ -56,7 +56,7 @@ QueueFamilyIndices QueueFamilyIndices::Query(VkPhysicalDevice device, VkSurfaceK return queueIndices; } -VkSurfaceFormatKHR PhysicalDeviceInfo::getFormat() const { +VkSurfaceFormatKHR PhysicalDeviceInfo::getSurfaceFormat() const { for(const auto& format: swapDetails.formats) { if (format.format == VK_FORMAT_B8G8R8A8_SRGB && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { return format; @@ -66,6 +66,27 @@ VkSurfaceFormatKHR PhysicalDeviceInfo::getFormat() const { LOG_W("Using suboptimal surface format"); return swapDetails.formats[0]; } +std::optional PhysicalDeviceInfo::findSupportedFormat(const std::vector& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) const { + for (VkFormat format : candidates) { + VkFormatProperties props; + vkGetPhysicalDeviceFormatProperties(device, format, &props); + + if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) { + return format; + } else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) { + return format; + } + } + return {}; +} +VkFormat PhysicalDeviceInfo::findDepthFormat() const { + if (auto res = findSupportedFormat( + {VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}, + VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) + return res.value(); + + FATAL("Any compatible depth format"); +} #include bool PhysicalDeviceInfo::hasMemoryBudget() const { for (auto extension: optionalExtensions) { diff --git a/src/client/render/vk/PhysicalDeviceInfo.hpp b/src/client/render/vk/PhysicalDeviceInfo.hpp index f678ac0..7a6bb04 100644 --- a/src/client/render/vk/PhysicalDeviceInfo.hpp +++ b/src/client/render/vk/PhysicalDeviceInfo.hpp @@ -32,9 +32,15 @@ struct PhysicalDeviceInfo { vkGetPhysicalDeviceFeatures(device, &features); } - VkSurfaceFormatKHR getFormat() const; + VkSurfaceFormatKHR getSurfaceFormat() const; + std::optional findSupportedFormat(const std::vector &candidates, VkImageTiling tiling, VkFormatFeatureFlags features) const; + VkFormat findDepthFormat() const; bool hasMemoryBudget() const; + static bool HasStencilComponent(VkFormat format) { + return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; + } + GLFWwindow *window; VkPhysicalDevice device = VK_NULL_HANDLE; VkSurfaceKHR surface; diff --git a/src/client/render/vk/Pipeline.cpp b/src/client/render/vk/Pipeline.cpp index a5521d0..ed36dc1 100644 --- a/src/client/render/vk/Pipeline.cpp +++ b/src/client/render/vk/Pipeline.cpp @@ -15,7 +15,7 @@ using namespace render::vk; Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const renderOptions &options): device(device) { { // Render pass VkAttachmentDescription colorAttachment{}; - colorAttachment.format = info.getFormat().format; + colorAttachment.format = info.getSurfaceFormat().format; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; @@ -24,16 +24,31 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; - //TODO: subpasses (world, entities, colors, sky, ui) VkAttachmentReference colorAttachmentRef{}; colorAttachmentRef.attachment = 0; //TODO: layouts bind colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + VkAttachmentDescription depthAttachment{}; + depthAttachment.format = info.findDepthFormat(); + depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; + depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + + VkAttachmentReference depthAttachmentRef{}; + depthAttachmentRef.attachment = 1; + depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + + //TODO: subpasses (world, entities, colors, sky, ui) + VkSubpassDescription subpass{}; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &colorAttachmentRef; + subpass.pDepthStencilAttachment = &depthAttachmentRef; VkSubpassDependency dependency{}; dependency.srcSubpass = VK_SUBPASS_EXTERNAL; @@ -45,8 +60,9 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render VkRenderPassCreateInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - renderPassInfo.attachmentCount = 1; - renderPassInfo.pAttachments = &colorAttachment; + std::array attachments = {colorAttachment, depthAttachment}; + renderPassInfo.attachmentCount = attachments.size(); + renderPassInfo.pAttachments = attachments.data(); renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpass; renderPassInfo.dependencyCount = 1; @@ -185,8 +201,15 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render VkPipelineDepthStencilStateCreateInfo depthStencil{}; depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + depthStencil.depthTestEnable = VK_TRUE; + depthStencil.depthWriteEnable = VK_TRUE; + depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; //FIXME: LEQUAL for skybox + depthStencil.depthBoundsTestEnable = VK_FALSE; // Restrict sub depth area + depthStencil.minDepthBounds = 0.0f; + depthStencil.maxDepthBounds = 1.0f; + depthStencil.stencilTestEnable = VK_FALSE; - depthStencil.depthTestEnable = VK_FALSE; // TODO: depth filter NOTE: set as null if create info + // Stencil options front/back VkPipelineColorBlendAttachmentState colorBlendAttachment{}; colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; @@ -233,7 +256,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render pipelineInfo.pViewportState = &viewportState; pipelineInfo.pRasterizationState = &rasterizer; pipelineInfo.pMultisampleState = &multisampling; - pipelineInfo.pDepthStencilState = nullptr; //FIXME: depthTest + pipelineInfo.pDepthStencilState = &depthStencil; pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = nullptr; diff --git a/src/client/render/vk/SwapChain.cpp b/src/client/render/vk/SwapChain.cpp index 47a37c3..0748295 100644 --- a/src/client/render/vk/SwapChain.cpp +++ b/src/client/render/vk/SwapChain.cpp @@ -27,8 +27,8 @@ SwapChain::SwapChain(VkDevice device, const PhysicalDeviceInfo& info): device(de createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.surface = info.surface; createInfo.minImageCount = imageCount; - createInfo.imageFormat = info.getFormat().format; - createInfo.imageColorSpace = info.getFormat().colorSpace; + createInfo.imageFormat = info.getSurfaceFormat().format; + createInfo.imageColorSpace = info.getSurfaceFormat().colorSpace; createInfo.imageExtent = info.swapDetails.capabilities.currentExtent; createInfo.imageArrayLayers = 1; createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; //VK_IMAGE_USAGE_TRANSFER_DST_BIT @@ -67,7 +67,7 @@ SwapChain::SwapChain(VkDevice device, const PhysicalDeviceInfo& info): device(de createInfo.image = images[i]; createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - createInfo.format = info.getFormat().format; + createInfo.format = info.getSurfaceFormat().format; createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; diff --git a/src/client/render/vk/api/Images.cpp b/src/client/render/vk/api/Images.cpp index 0bdb0ec..e22354c 100644 --- a/src/client/render/vk/api/Images.cpp +++ b/src/client/render/vk/api/Images.cpp @@ -7,13 +7,13 @@ using namespace render::vk; Image::~Image() { vkDestroyImage(Allocator::GetDefault()->getDevice(), ref, ALLOC); + vkDestroyImageView(Allocator::GetDefault()->getDevice(), view, ALLOC); } Texture::~Texture() { vkDestroySampler(Allocator::GetDefault()->getDevice(), sampler, ALLOC); - vkDestroyImageView(Allocator::GetDefault()->getDevice(), view, ALLOC); } -memory::ptr createImage(const Image::requirement& req, VkMemoryPropertyFlags properties, const render::data_view view, Image::info& out) { +memory::ptr createImage(const Image::requirement& req, VkMemoryPropertyFlags properties, const render::data_view data, Image::info& out) { auto alloc = Allocator::GetDefault(); auto device = alloc->getDevice(); @@ -27,13 +27,12 @@ memory::ptr createImage(const Image::requirement& req, VkMemoryPropertyFlags pro info.arrayLayers = 1; info.format = static_cast(req.props.format); info.tiling = req.linear ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; - info.initialLayout = static_cast(req.layout); + info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; info.usage = static_cast(req.usage); info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; info.samples = VK_SAMPLE_COUNT_1_BIT; info.flags = 0; - if (view) { - info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + if (data) { info.usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; } @@ -52,21 +51,48 @@ memory::ptr createImage(const Image::requirement& req, VkMemoryPropertyFlags pro return memory::GetNull(); } - if (view) { - if(auto staging = WritableBuffer::Create(view.size)) { - staging->write(view, 0); + if (data) { + if(auto staging = WritableBuffer::Create(data.size)) { + staging->write(data, 0); alloc->transitionImageLayout(out.ref, info.format, info.initialLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); alloc->copyBufferToImage(staging->getRef(), out.ref, info.extent.width, info.extent.height); alloc->transitionImageLayout(out.ref, info.format, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast(req.layout)); } else { - FATAL("Cannot allocate staging memory"); + LOG_E("Cannot allocate staging memory"); return memory::GetNull(); } + } else { + alloc->transitionImageLayout(out.ref, info.format, info.initialLayout, static_cast(req.layout)); + } + + VkImageViewCreateInfo viewInfo{}; + viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; + viewInfo.image = out.ref; + viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; + viewInfo.format = static_cast(req.props.format); + viewInfo.subresourceRange.aspectMask = static_cast(req.aspect); + viewInfo.subresourceRange.baseMipLevel = 0; + viewInfo.subresourceRange.levelCount = 1; //TODO: mipmap + viewInfo.subresourceRange.baseArrayLayer = 0; + viewInfo.subresourceRange.layerCount = 1; + + if (vkCreateImageView(device, &viewInfo, ALLOC, &out.view) != VK_SUCCESS) { + LOG_E("Failed to create texture image view!"); + return memory::GetNull(); } return memory; } //TODO: createImages +std::unique_ptr Image::Create(const requirement & req) { + vk::Image::info img; + auto mem = createImage(req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, data_view(), img); + if(!mem) { + FATAL("Cannot create texture image"); + } + return std::unique_ptr(new Image(img.ref, img.view, std::move(mem))); +} + std::unique_ptr Texture::LoadFromFile(const std::string& path, const sampling& props) { auto device = Allocator::GetDefault()->getDevice(); @@ -85,22 +111,6 @@ std::unique_ptr Texture::LoadFromFile(const std::string& path, const sa FATAL("Cannot create texture image"); } - VkImageViewCreateInfo viewInfo{}; - viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - viewInfo.image = img.ref; - viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - viewInfo.format = static_cast(header.format); - viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - viewInfo.subresourceRange.baseMipLevel = 0; - viewInfo.subresourceRange.levelCount = 1; - viewInfo.subresourceRange.baseArrayLayer = 0; - viewInfo.subresourceRange.layerCount = 1; - - VkImageView view; - if (vkCreateImageView(device, &viewInfo, ALLOC, &view) != VK_SUCCESS) { - FATAL("Failed to create texture image view!"); - } - VkSamplerCreateInfo samplerInfo{}; samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerInfo.magFilter = props.magLinear ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; @@ -135,5 +145,5 @@ std::unique_ptr Texture::LoadFromFile(const std::string& path, const sa FATAL("Failed to create texture sampler!"); } - return std::unique_ptr(new Texture(sampler, view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, img.ref, std::move(mem))); + return std::unique_ptr(new Texture(sampler, img.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, img.ref, std::move(mem))); } \ No newline at end of file diff --git a/src/client/render/vk/api/Images.hpp b/src/client/render/vk/api/Images.hpp index a07dd87..13a1990 100644 --- a/src/client/render/vk/api/Images.hpp +++ b/src/client/render/vk/api/Images.hpp @@ -9,17 +9,23 @@ class Image: public render::Image { public: virtual ~Image(); + const VkImageView &getView() const { return view; } + static void MakeDefault(); struct info { VkImage ref = nullptr; + VkImageView view = nullptr; VkDeviceSize offset = 0; }; + static std::unique_ptr Create(const requirement &); + protected: - Image(VkImage ref, memory::ptr mem): - ref(ref), memory(std::move(mem)) { } + Image(VkImage ref, VkImageView view, memory::ptr mem): + ref(ref), view(view), memory(std::move(mem)) { } VkImage ref; + VkImageView view; memory::ptr memory; }; @@ -33,9 +39,8 @@ public: protected: Texture(VkSampler sampler, VkImageView view, VkImageLayout layout, VkImage ref, memory::ptr memory): - vk::Image(ref, std::move(memory)), sampler(sampler), view(view), descriptor({sampler, view, layout}) { } + vk::Image(ref, view, std::move(memory)), sampler(sampler), descriptor({sampler, view, layout}) { } VkSampler sampler; - VkImageView view; const VkDescriptorImageInfo descriptor; }; diff --git a/src/client/render/vk/buffer/VertexData.hpp b/src/client/render/vk/buffer/VertexData.hpp index fa2d305..19b9353 100644 --- a/src/client/render/vk/buffer/VertexData.hpp +++ b/src/client/render/vk/buffer/VertexData.hpp @@ -8,7 +8,7 @@ namespace buffer::vk { struct VertexData { - glm::vec2 pos; + glm::vec3 pos; glm::vec3 color; glm::vec2 uv; @@ -23,7 +23,7 @@ struct VertexData { std::array attributeDescriptions{}; attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; - attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; attributeDescriptions[0].offset = offsetof(VertexData, pos); attributeDescriptions[1].binding = 0; attributeDescriptions[1].location = 1; @@ -38,15 +38,20 @@ struct VertexData { }; const std::vector vertices = { - {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, - {{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, - {{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}}, - {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}} -}; -const std::vector indices = { - 0, 1, 2, 2, 3, 0 -}; + {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, + {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, + {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}}, + {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, + {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, + {{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, + {{0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}}, + {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; + +const std::vector indices = { + 0, 1, 2, 2, 3, 0, + 4, 5, 6, 6, 7, 4 +}; struct UniformBufferObject { alignas(16) glm::mat4 model;