From 98c5f28146b4658bd2950ca2b918d936d69f2614 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Sat, 14 Jun 2025 17:16:37 +0200 Subject: [PATCH] Load and add texture to the Rect. --- .gitattributes | 5 + AppState.cpp | 2 +- Blaze.cpp | 2 +- Blaze.vcxproj | 5 + Blaze.vcxproj.filters | 11 ++ Mesh.slang | 34 ++-- MiscData.cpp | 408 +++++++++++++++++++++++++++++++++++++----- MiscData.h | 13 +- StbImpl.cpp | 6 + container2.png | 3 + vcpkg.json | 1 + wall.jpg | 3 + 12 files changed, 430 insertions(+), 63 deletions(-) create mode 100644 .gitattributes create mode 100644 StbImpl.cpp create mode 100644 container2.png create mode 100644 wall.jpg diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6c8519f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +*.jpg filter=lfs diff=lfs merge=lfs -text +*.png filter=lfs diff=lfs merge=lfs -text +*.glb filter=lfs diff=lfs merge=lfs -text +*.hdr filter=lfs diff=lfs merge=lfs -text +*.exr filter=lfs diff=lfs merge=lfs -text diff --git a/AppState.cpp b/AppState.cpp index f0a07a2..522c362 100644 --- a/AppState.cpp +++ b/AppState.cpp @@ -48,7 +48,7 @@ AppState* AppState_Create( GlobalMemory* memory, uint32_t const width, uint32_t auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) ); MiscData* miscData = new ( miscDataAllocation ) MiscData{}; - miscData->init( *renderDevice ); + if ( !miscData->init( *renderDevice ) ) return nullptr; auto* allocation = memory->allocate( sizeof( AppState ) ); AppState* appState = new ( allocation ) AppState{ window, renderDevice, miscData }; diff --git a/Blaze.cpp b/Blaze.cpp index d5dce29..e6dfd1d 100644 --- a/Blaze.cpp +++ b/Blaze.cpp @@ -208,7 +208,7 @@ void SDL_AppQuit( void* appstate, SDL_AppResult ) { AppState* appState = static_cast( appstate ); - appState->destroy(); + if ( appState ) appState->destroy(); Blaze::Global::g_Memory.destroy(); } diff --git a/Blaze.vcxproj b/Blaze.vcxproj index 92324bf..ba9d26d 100644 --- a/Blaze.vcxproj +++ b/Blaze.vcxproj @@ -154,6 +154,7 @@ + @@ -189,6 +190,10 @@ + + + + diff --git a/Blaze.vcxproj.filters b/Blaze.vcxproj.filters index ce082be..5043e5f 100644 --- a/Blaze.vcxproj.filters +++ b/Blaze.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -88,4 +91,12 @@ Header Files + + + Resource Files + + + Resource Files + + \ No newline at end of file diff --git a/Mesh.slang b/Mesh.slang index 79493ef..df4dc74 100644 --- a/Mesh.slang +++ b/Mesh.slang @@ -1,18 +1,8 @@ -const static float4 vertexPos[] = { - float4(0.0f, 0.5f, 0.5f, 1.0f), - float4(-0.5f, -0.5f, 0.5f, 1.0f), - float4(0.5f, -0.5f, 0.5f, 1.0f), -}; -const static float4 vertexColors[] = { - float4(1.0f, 0.0f, 0.0f, 1.0f), - float4(0.0f, 1.0f, 0.0f, 1.0f), - float4(0.0f, 0.0f, 1.0f, 1.0f), -}; - struct VertexOut { float4 outPosition : SV_Position; float4 vertexColor : CoarseColor; + float2 texCoord0 : TexCoord0; }; struct CameraData { @@ -21,24 +11,32 @@ struct CameraData { float4x4 proj; }; -ParameterBlock camera; +struct PerFrameData { + CameraData camera; + Sampler2D texture; +} + +ParameterBlock perFrameData; [shader("vertex")] VertexOut VertexMain( uint vertexId: SV_VertexID, - float4 position, - float4 color, + float3 position, + float3 color, + float2 texCoord0, ) { VertexOut output; - output.outPosition = mul(camera.proj, mul(camera.view, mul(camera.model, position))); - output.vertexColor = color; + output.outPosition = mul(perFrameData.camera.proj, mul(perFrameData.camera.view, mul(perFrameData.camera.model, float4(position, 1.0f)))); + output.vertexColor = float4(color, 1.0f); + output.texCoord0 = texCoord0 * 2.0f; return output; } [shader("fragment")] float4 FragmentMain( - float4 interpolatedColors: CoarseColor, + float4 interpolatedColors : CoarseColor, + float2 uv0 : TexCoord0, ) : SV_Target0 { - return float4(interpolatedColors.rgb, 1.0f); + return float4(perFrameData.texture.Sample(uv0).rgb, 1.0f) * interpolatedColors; } diff --git a/MiscData.cpp b/MiscData.cpp index 9acdf63..9f247b0 100644 --- a/MiscData.cpp +++ b/MiscData.cpp @@ -4,10 +4,13 @@ #include +#include + +#include "Frame.h" #include "MacroUtils.h" #include "RenderDevice.h" -void MiscData::init( RenderDevice const& renderDevice ) +bool MiscData::init( RenderDevice const& renderDevice ) { VkDevice const device = renderDevice.device; @@ -22,7 +25,7 @@ void MiscData::init( RenderDevice const& renderDevice ) if ( !rawData ) { SDL_LogError( SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError() ); - abort(); + return false; } uint32_t const* data = static_cast( rawData ); @@ -38,20 +41,29 @@ void MiscData::init( RenderDevice const& renderDevice ) VkShaderModule shaderModule; VK_CHECK( vkCreateShaderModule( device, &shaderModuleCreateInfo, nullptr, &shaderModule ) ); - VkDescriptorSetLayoutBinding constexpr descriptorSetLayoutBinding = { - .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, - .pImmutableSamplers = nullptr, + std::array descriptorSetLayoutBindings{ + VkDescriptorSetLayoutBinding{ + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + .pImmutableSamplers = nullptr, + }, + VkDescriptorSetLayoutBinding{ + .binding = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, + .pImmutableSamplers = nullptr, + }, }; VkDescriptorSetLayoutCreateInfo const descriptorSetLayoutCreateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .pNext = nullptr, .flags = 0, - .bindingCount = 1, - .pBindings = &descriptorSetLayoutBinding, + .bindingCount = static_cast( descriptorSetLayoutBindings.size() ), + .pBindings = descriptorSetLayoutBindings.data(), }; VK_CHECK( vkCreateDescriptorSetLayout( device, &descriptorSetLayoutCreateInfo, nullptr, &descriptorSetLayout ) ); @@ -98,14 +110,20 @@ void MiscData::init( RenderDevice const& renderDevice ) VkVertexInputAttributeDescription{ .location = 0, .binding = 0, - .format = VK_FORMAT_R32G32B32A32_SFLOAT, - .offset = offsetof( Vertex, position ), + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof( Vertex, position ), }, VkVertexInputAttributeDescription{ .location = 1, .binding = 0, - .format = VK_FORMAT_R32G32B32A32_SFLOAT, - .offset = offsetof( Vertex, color ), + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof( Vertex, color ), + }, + VkVertexInputAttributeDescription{ + .location = 2, + .binding = 0, + .format = VK_FORMAT_R32G32_SFLOAT, + .offset = offsetof( Vertex, texCoord0 ), }, }; @@ -210,7 +228,7 @@ void MiscData::init( RenderDevice const& renderDevice ) .blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f }, }; - std::array constexpr dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; + std::array constexpr dynamicStates{ VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, @@ -271,23 +289,27 @@ void MiscData::init( RenderDevice const& renderDevice ) vertices = std::array{ // Bottom Left Vertex{ - .position = { -1.0f, -1.0f, 0.0f, 1.0f }, - .color = { 0.0f, 0.0f, 1.0f, 1.0f }, + .position = { -1.0f, -1.0f, 0.0f }, + .color = { 0.0f, 0.0f, 1.0f }, + .texCoord0 = { 0.0f, 0.0f }, }, // Bottom Right Vertex{ - .position = { 1.0f, -1.0f, 0.0f, 1.0f }, - .color = { 1.0f, 0.0f, 0.0f, 1.0f }, + .position = { 1.0f, -1.0f, 0.0f }, + .color = { 1.0f, 0.0f, 0.0f }, + .texCoord0 = { 1.0f, 0.0f }, }, // Top Left Vertex{ - .position = { -1.0f, 1.0f, 0.0f, 1.0f }, - .color = { 0.0f, 1.0f, 0.0f, 1.0f }, + .position = { -1.0f, 1.0f, 0.0f }, + .color = { 0.0f, 1.0f, 0.0f }, + .texCoord0 = { 0.0f, 1.0f }, }, // Top Right Vertex{ - .position = { 1.0f, 1.0f, 0.0f, 1.0f }, - .color = { 1.0f, 1.0f, 0.0f, 1.0f }, + .position = { 1.0f, 1.0f, 0.0f }, + .color = { 1.0f, 1.0f, 0.0f }, + .texCoord0 = { 1.0f, 1.0f }, } }; @@ -329,6 +351,277 @@ void MiscData::init( RenderDevice const& renderDevice ) } } + // Texture + { + uint32_t width; + uint32_t height; + uint32_t numChannels = 4; + stbi_uc* textureData; + { + int w; + int h; + int nc; + int requestedChannels = static_cast( numChannels ); + + textureData = stbi_load( "wall.jpg", &w, &h, &nc, requestedChannels ); + ASSERT( nc <= requestedChannels ); + + if ( !textureData ) + { + vkDestroyPipeline( device, Take( meshPipeline ), nullptr ); + vmaDestroyBuffer( renderDevice.gpuAllocator, Take( vertexBuffer ), Take( vertexBufferAllocation ) ); + SDL_LogError( SDL_LOG_CATEGORY_ERROR, "%s", stbi_failure_reason() ); + return false; + } + + width = static_cast( w ); + height = static_cast( h ); + } + + VkImageCreateInfo const imageCreateInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R8G8B8A8_SRGB, + .extent = { .width = width, .height = height, .depth = 1 }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .queueFamilyIndexCount = 0, + .pQueueFamilyIndices = nullptr, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }; + + VmaAllocationCreateInfo constexpr allocationCreateInfo = { + .flags = 0, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = 0, + .preferredFlags = 0, + .memoryTypeBits = 0, + .pool = nullptr, + .pUserData = nullptr, + .priority = 1.0f, + }; + + VK_CHECK( vmaCreateImage( + renderDevice.gpuAllocator, &imageCreateInfo, &allocationCreateInfo, &texture, &textureAllocation, nullptr ) ); + + VkImageSubresourceRange constexpr subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }; + + VkComponentMapping constexpr componentMapping = { + .r = VK_COMPONENT_SWIZZLE_IDENTITY, + .g = VK_COMPONENT_SWIZZLE_IDENTITY, + .b = VK_COMPONENT_SWIZZLE_IDENTITY, + .a = VK_COMPONENT_SWIZZLE_IDENTITY, + }; + + VkImageViewCreateInfo const imageViewCreateInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .image = texture, + .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = imageCreateInfo.format, + .components = componentMapping, + .subresourceRange = subresourceRange, + }; + + VK_CHECK( vkCreateImageView( device, &imageViewCreateInfo, nullptr, &textureView ) ); + + VkSamplerCreateInfo constexpr samplerCreateInfo = { + .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .magFilter = VK_FILTER_LINEAR, + .minFilter = VK_FILTER_LINEAR, + .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .mipLodBias = 0.0, + .anisotropyEnable = true, + .maxAnisotropy = 1.0f, + .compareEnable = false, + .compareOp = VK_COMPARE_OP_NEVER, + .minLod = 0.0f, + .maxLod = 0.0f, + .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK, + .unnormalizedCoordinates = false, + }; + + VK_CHECK( vkCreateSampler( device, &samplerCreateInfo, nullptr, &sampler ) ); + + // Staging Buffer Create + VkBuffer stagingBuffer; + VmaAllocation stagingAllocation; + { + VkBufferCreateInfo const stagingBufferCreateInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = static_cast( width ) * height * numChannels * sizeof( textureData[0] ), + .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .queueFamilyIndexCount = 0, + .pQueueFamilyIndices = nullptr, + }; + + VmaAllocationCreateInfo constexpr stagingAllocationCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + .preferredFlags = 0, + .memoryTypeBits = 0, + .pool = nullptr, + .pUserData = nullptr, + .priority = 1.0f, + }; + + VmaAllocationInfo allocationInfo; + + VK_CHECK( vmaCreateBuffer( + renderDevice.gpuAllocator, + &stagingBufferCreateInfo, + &stagingAllocationCreateInfo, + &stagingBuffer, + &stagingAllocation, + &allocationInfo ) ); + + if ( allocationInfo.pMappedData ) + { + memcpy( allocationInfo.pMappedData, textureData, stagingBufferCreateInfo.size ); + } + } + + // All data is copied to stagingBuffer, don't need this. + stbi_image_free( textureData ); + + { + Frame& frameInUse = renderDevice.frames[0]; + + // This should just pass. + VK_CHECK( vkWaitForFences( device, 1, &frameInUse.frameReadyToReuse, VK_TRUE, INT64_MAX ) ); + VK_CHECK( vkResetFences( device, 1, &frameInUse.frameReadyToReuse ) ); + + VkCommandBufferBeginInfo constexpr beginInfo = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .pNext = nullptr, + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, + .pInheritanceInfo = nullptr, + }; + + VkImageMemoryBarrier2 const creationToTransferImageBarrier = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .pNext = nullptr, + .srcStageMask = VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT, + .srcAccessMask = VK_ACCESS_2_NONE, + .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, + .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = texture, + .subresourceRange = subresourceRange, + }; + + VkDependencyInfo const creationToTransferDependency = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .pNext = nullptr, + .dependencyFlags = 0, + .memoryBarrierCount = 0, + .pMemoryBarriers = nullptr, + .bufferMemoryBarrierCount = 0, + .pBufferMemoryBarriers = nullptr, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &creationToTransferImageBarrier, + }; + + VkImageMemoryBarrier2 const transferToReadyImageBarrier = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .pNext = nullptr, + .srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, + .dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT, + .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = texture, + .subresourceRange = subresourceRange, + }; + + VkDependencyInfo const transferToReadyDependency = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .pNext = nullptr, + .dependencyFlags = 0, + .memoryBarrierCount = 0, + .pMemoryBarriers = nullptr, + .bufferMemoryBarrierCount = 0, + .pBufferMemoryBarriers = nullptr, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &transferToReadyImageBarrier, + }; + + vkBeginCommandBuffer( frameInUse.commandBuffer, &beginInfo ); + { + VkImageSubresourceLayers imageSubresourceLayers = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .mipLevel = 0, + .baseArrayLayer = 0, + .layerCount = 1, + }; + + // TODO: Ensure `bufferRowLength` and `bufferImageHeight` are not required. + VkBufferImageCopy copyRegion = { + .bufferOffset = 0, + .bufferRowLength = 0, + .bufferImageHeight = 0, + .imageSubresource = imageSubresourceLayers, + .imageOffset = { 0, 0, 0 }, + .imageExtent = imageCreateInfo.extent + }; + + vkCmdPipelineBarrier2( frameInUse.commandBuffer, &creationToTransferDependency ); + + vkCmdCopyBufferToImage( + frameInUse.commandBuffer, stagingBuffer, texture, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region ); + + vkCmdPipelineBarrier2( frameInUse.commandBuffer, &transferToReadyDependency ); + } + vkEndCommandBuffer( frameInUse.commandBuffer ); + + VkSubmitInfo submitInfo = { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .pNext = nullptr, + .waitSemaphoreCount = 0, + .pWaitSemaphores = nullptr, + .pWaitDstStageMask = nullptr, + .commandBufferCount = 1, + .pCommandBuffers = &frameInUse.commandBuffer, + .signalSemaphoreCount = 0, + .pSignalSemaphores = nullptr, + }; + VK_CHECK( vkQueueSubmit( renderDevice.directQueue, 1, &submitInfo, frameInUse.frameReadyToReuse ) ); + + // Do not reset this. Else, the frame will never be available to the main loop. + VK_CHECK( vkWaitForFences( device, 1, &frameInUse.frameReadyToReuse, VK_TRUE, INT64_MAX ) ); + } + + vmaDestroyBuffer( renderDevice.gpuAllocator, stagingBuffer, stagingAllocation ); + } + // Camera { cameraPosition = DirectX::XMVectorSet( 0.0f, 0.0f, -5.0f, 1.0f ); @@ -382,17 +675,23 @@ void MiscData::init( RenderDevice const& renderDevice ) // Descriptors { - VkDescriptorPoolSize const poolSize = { - .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = renderDevice.getNumFrames(), + std::array poolSizes = { + VkDescriptorPoolSize{ + .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = renderDevice.getNumFrames(), + }, + VkDescriptorPoolSize{ + .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = renderDevice.getNumFrames(), + }, }; VkDescriptorPoolCreateInfo const descriptorPoolCreateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .pNext = nullptr, .flags = 0, .maxSets = renderDevice.getNumFrames(), - .poolSizeCount = 1, - .pPoolSizes = &poolSize, + .poolSizeCount = static_cast( poolSizes.size() ), + .pPoolSizes = poolSizes.data(), }; VK_CHECK( vkCreateDescriptorPool( device, &descriptorPoolCreateInfo, nullptr, &descriptorPool ) ); @@ -413,20 +712,42 @@ void MiscData::init( RenderDevice const& renderDevice ) .range = sizeof CameraData, }; - VkWriteDescriptorSet writeDescriptorSet = { - .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .pNext = nullptr, - .dstSet = descriptorSet, - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .pImageInfo = nullptr, - .pBufferInfo = &descriptorBufferInfo, - .pTexelBufferView = nullptr, + VkDescriptorImageInfo const descriptorImageInfo = { + .sampler = sampler, + .imageView = textureView, + .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, }; - vkUpdateDescriptorSets( device, 1, &writeDescriptorSet, 0, nullptr ); + std::array writeDescriptorSets = { + VkWriteDescriptorSet{ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = nullptr, + .dstSet = descriptorSet, + .dstBinding = 0, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .pImageInfo = nullptr, + .pBufferInfo = &descriptorBufferInfo, + .pTexelBufferView = nullptr, + }, + + VkWriteDescriptorSet{ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = nullptr, + .dstSet = descriptorSet, + .dstBinding = 1, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .pImageInfo = &descriptorImageInfo, + .pBufferInfo = nullptr, + .pTexelBufferView = nullptr, + } + }; + + vkUpdateDescriptorSets( + device, static_cast( writeDescriptorSets.size() ), writeDescriptorSets.data(), 0, nullptr ); } // Barrier Creation @@ -491,6 +812,8 @@ void MiscData::init( RenderDevice const& renderDevice ) .pImageMemoryBarriers = &renderToPresentBarrier, }; } + + return true; } void MiscData::destroy( RenderDevice const& renderDevice ) @@ -499,6 +822,11 @@ void MiscData::destroy( RenderDevice const& renderDevice ) vkDestroyDescriptorPool( device, Take( descriptorPool ), nullptr ); vmaDestroyBuffer( renderDevice.gpuAllocator, Take( cameraUniformBuffer ), Take( cameraUniformBufferAllocation ) ); + + vkDestroySampler( device, Take( sampler ), nullptr ); + vkDestroyImageView( device, Take( textureView ), nullptr ); + + vmaDestroyImage( renderDevice.gpuAllocator, Take( texture ), Take( textureAllocation ) ); vmaDestroyBuffer( renderDevice.gpuAllocator, Take( vertexBuffer ), Take( vertexBufferAllocation ) ); vkDestroyPipeline( device, Take( meshPipeline ), nullptr ); diff --git a/MiscData.h b/MiscData.h index 00222ce..5ae7ed7 100644 --- a/MiscData.h +++ b/MiscData.h @@ -7,12 +7,14 @@ #include +struct GlobalMemory; struct RenderDevice; struct Vertex { - float position[4]; - float color[4]; + DirectX::XMFLOAT3 position; + DirectX::XMFLOAT3 color; + DirectX::XMFLOAT2 texCoord0; }; struct MiscData @@ -35,6 +37,11 @@ struct MiscData size_t vertexBufferSize; std::array vertices; + VkImage texture; + VmaAllocation textureAllocation; + VkImageView textureView; + VkSampler sampler; + uint64_t _padding; // TODO: Optimize out? DirectX::XMVECTOR cameraPosition; @@ -53,6 +60,6 @@ struct MiscData VkImageMemoryBarrier2 renderToPresentBarrier; VkDependencyInfo renderToPresentDependency; - void init( RenderDevice const& renderDevice ); + bool init( RenderDevice const& renderDevice ); void destroy( RenderDevice const& renderDevice ); }; diff --git a/StbImpl.cpp b/StbImpl.cpp new file mode 100644 index 0000000..f960110 --- /dev/null +++ b/StbImpl.cpp @@ -0,0 +1,6 @@ + +#pragma warning( push, 1 ) +#pragma warning( disable : 5045 ) +#define STB_IMAGE_IMPLEMENTATION +#include +#pragma warning( pop ) diff --git a/container2.png b/container2.png new file mode 100644 index 0000000..a38b62e --- /dev/null +++ b/container2.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd9c9421005719d75aab1e4494314436d9c197bd3ce2b823e75cccc4d02f44a1 +size 467893 diff --git a/vcpkg.json b/vcpkg.json index 2d030a5..89d99fc 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -4,6 +4,7 @@ "shader-slang", "vulkan-memory-allocator", "directxmath", + "stb", { "name": "sdl3", "features": [ "vulkan" ] diff --git a/wall.jpg b/wall.jpg new file mode 100644 index 0000000..2f81ab7 --- /dev/null +++ b/wall.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d1dfe4ff8ad7bd8a5102da36d1227e07b9c62ba26098adee1999c4da230eefe3 +size 256989