Skip to content

Commit

Permalink
finally images work
Browse files Browse the repository at this point in the history
finished desc buffers
base API is finished
  • Loading branch information
Agrael1 committed Apr 17, 2024
1 parent 785d923 commit 4dcbd7a
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 57 deletions.
1 change: 1 addition & 0 deletions examples/hello-triangle-win32-t/include/example/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class App

wis::Buffer vertex_buffer;
wis::Buffer ubuf_2;
wis::Buffer cbuf;
wis::VertexBufferBinding vertex_binding;
wis::ResourceAllocator allocator;

Expand Down
45 changes: 37 additions & 8 deletions examples/hello-triangle-win32-t/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void Test::App::CreateResources()
}
auto [resx, ubuf] = allocator.CreateUploadBuffer(sizeof(triangleVertices));
auto [resx2, ubuf2] = allocator.CreateUploadBuffer(2 * 2 * 4);
auto [resx3, ubuf3] = allocator.CreateUploadBuffer(sizeof(float) * 4);

{
auto [res, vbuf] = allocator.CreateCommitedBuffer(sizeof(triangleVertices), wis::BufferFlags::VertexBuffer);
Expand All @@ -166,18 +167,26 @@ void Test::App::CreateResources()

// Create descriptor buffer
{
auto [res, hdesc] = device.CreateDescriptorBuffer(wis::DescriptorHeapType::Descriptor, wis::DescriptorMemory::ShaderVisible, 1);
auto [res, hdesc] = device.CreateDescriptorBuffer(wis::DescriptorHeapType::Descriptor, wis::DescriptorMemory::ShaderVisible, 2);
desc_buffer = std::move(hdesc);

auto [res2, hdesc2] = device.CreateDescriptorBuffer(wis::DescriptorHeapType::Sampler, wis::DescriptorMemory::ShaderVisible, 1);
sampler_buffer = std::move(hdesc2);
}
{
auto [res,val] = allocator.CreateCommitedBuffer(sizeof(float) * 4, wis::BufferFlags::ConstantBuffer);
cbuf = std::move(val);
}

// Upload vertex data to a buffer
{
auto memory = ubuf.Map<Vertex>();
std::copy(std::begin(triangleVertices), std::end(triangleVertices), memory);
ubuf.Unmap();
ubuf.Unmap();

auto memoryx = ubuf3.Map<glm::vec4>();
*memoryx = { 1, 1, 0, 1 };
ubuf3.Unmap();

auto texture_mem = ubuf2.Map<uint32_t>();
texture_mem[0] = 0xFF0000FF;
Expand All @@ -187,6 +196,7 @@ void Test::App::CreateResources()
ubuf2.Unmap();

cmd_list.CopyBuffer(ubuf, vertex_buffer, { .size_bytes = sizeof(triangleVertices) });
cmd_list.CopyBuffer(ubuf3, cbuf, { .size_bytes = sizeof(float) * 4 });
cmd_list.BufferBarrier({
.sync_before = wis::BarrierSync::All,
.sync_after = wis::BarrierSync::Draw,
Expand All @@ -195,6 +205,14 @@ void Test::App::CreateResources()
},
vertex_buffer);

cmd_list.BufferBarrier({
.sync_before = wis::BarrierSync::All,
.sync_after = wis::BarrierSync::Draw,
.access_before = wis::ResourceAccess::Common,
.access_after = wis::ResourceAccess::ConstantBuffer,
},
cbuf);

cmd_list.TextureBarrier({
.sync_before = wis::BarrierSync::All,
.sync_after = wis::BarrierSync::All,
Expand Down Expand Up @@ -223,7 +241,7 @@ void Test::App::CreateResources()
cmd_list.TextureBarrier(
{
.sync_before = wis::BarrierSync::All,
.sync_after = wis::BarrierSync::Draw,
.sync_after = wis::BarrierSync::All,
.access_before = wis::ResourceAccess::CopyDest,
.access_after = wis::ResourceAccess::ShaderResource,
.state_before = wis::TextureState::CopyDest,
Expand All @@ -242,6 +260,9 @@ void Test::App::CreateResources()
wis::CommandListView cmd_lists[] = { cmd_list };
queue.ExecuteCommandLists(cmd_lists, 1);

WaitForGPU();


auto [res, hsrv] = device.CreateShaderResource(texture, { .format = wis::DataFormat::BGRA8Unorm, .view_type = wis::TextureViewType::Texture2D, .subresource_range = {
.base_mip_level = 0,
.level_count = 1,
Expand All @@ -250,6 +271,7 @@ void Test::App::CreateResources()
} });
srv = std::move(hsrv);
desc_buffer.WriteShaderResource(0, srv);
desc_buffer.WriteConstantBuffer(1, cbuf, sizeof(float) * 4);
}

{
Expand All @@ -276,6 +298,11 @@ void Test::App::CreateResources()
.bind_register = 0,
.count = 1,
},
{
.type = wis::DescriptorType::ConstantBuffer,
.bind_register = 1,
.count = 1,
},
{
.type = wis::DescriptorType::Sampler,
.bind_register = 0,
Expand All @@ -287,15 +314,17 @@ void Test::App::CreateResources()
{
.type = wis::DescriptorHeapType::Descriptor,
.entries = entries,
.entry_count = 1,
.entry_count = 2,
.stage = wis::ShaderStages::Pixel,
},
{
.type = wis::DescriptorHeapType::Sampler,
.entries = entries + 1,
.entries = entries + 2,
.entry_count = 1,
.stage = wis::ShaderStages::Pixel,
},


};
auto [result, hroot] = device.CreateRootSignature(root_constants, sizeof(root_constants) / sizeof(root_constants[0]), tables, sizeof(tables) / sizeof(tables[0]));
root = std::move(hroot);
Expand Down Expand Up @@ -388,9 +417,9 @@ void Test::App::OnResize(uint32_t width, uint32_t height)

void Test::App::Frame()
{
rotation += 0.01f;
if (rotation > 1)
rotation -= 1;
//rotation += 0.01f;
//if (rotation > 1)
// rotation -= 1;

auto res = cmd_list.Reset(pipeline);
cmd_list.TextureBarrier({
Expand Down
8 changes: 7 additions & 1 deletion examples/shaders/forward/example.ps.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ struct PSInput
float2 tc : TEXCOORD0;
};

[[vk::binding(0,0)]]Texture2D<float4> texture0 : register(t0);
[[vk::binding(0,0)]] Texture2D texture0 : register(t0);
[[vk::binding(1,0)]]
cbuffer cbuf : register(b1)
{
float4 exposure;
};

[[vk::binding(0,1)]] SamplerState samLinear : register(s0);

struct PSOutput
Expand Down
2 changes: 1 addition & 1 deletion wisdom/bindings/wisdom.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by wisdom generator on 2024-04-04 00:00:18.1158629 GMT+2
// Generated by wisdom generator on 2024-04-05 18:00:05.7197771 GMT+2
#include "wisdom.h"
#include <wisdom/wisdom.hpp>

Expand Down
2 changes: 1 addition & 1 deletion wisdom/bindings/wisdom.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by wisdom generator on 2024-04-04 00:00:18.1158629 GMT+2
// Generated by wisdom generator on 2024-04-05 18:00:05.7197771 GMT+2
#pragma once
#include <stdint.h>
#include <stdbool.h>
Expand Down
4 changes: 2 additions & 2 deletions wisdom/include/wisdom/dx12/dx12_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class DX12ResourceAllocator : public QueryInternal<DX12ResourceAllocator>

public:
[[nodiscard]] WIS_INLINE wis::ResultValue<wis::DX12Buffer>
CreateCommitedBuffer(size_t size, [[maybe_unused]] BufferFlags flags = BufferFlags::None) const noexcept;
CreateCommitedBuffer(uint64_t size, BufferFlags flags = BufferFlags::None) const noexcept;

[[nodiscard]] WIS_INLINE wis::ResultValue<wis::DX12UploadBuffer>
CreateUploadBuffer(size_t size) const noexcept;
CreateUploadBuffer(uint64_t size) const noexcept;

[[nodiscard]] WIS_INLINE wis::ResultValue<DX12Texture>
CreateTexture(wis::TextureDesc desc) const noexcept;
Expand Down
15 changes: 15 additions & 0 deletions wisdom/include/wisdom/dx12/dx12_descriptor_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <wisdom/global/internal.h>
#include <wisdom/util/com_ptr.h>
#include <d3dx12/d3dx12_root_signature.h>
#include <wisdom/util/misc.h>

namespace wis {

Expand Down Expand Up @@ -66,5 +67,19 @@ class DX12DescriptorBuffer : public QueryInternal<DX12DescriptorBuffer>
auto sampler_handle = std::get<0>(resource);
device->CopyDescriptorsSimple(1, handle, sampler_handle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}

void WriteConstantBuffer(uint32_t index, wis::DX12BufferView buffer, uint32_t size) noexcept
{
auto handle = heap->GetCPUDescriptorHandleForHeapStart();
handle.ptr += index * heap_increment;

D3D12_CONSTANT_BUFFER_VIEW_DESC desc
{
.BufferLocation = std::get<0>(buffer)->GetGPUVirtualAddress(),
.SizeInBytes = wis::detail::aligned_size(size, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT)
};

device->CreateConstantBufferView(&desc, handle);
}
};
} // namespace wis
8 changes: 6 additions & 2 deletions wisdom/include/wisdom/dx12/impl/dx12_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <d3dx12/d3dx12_core.h>
#include <d3dx12/d3dx12_resource_helpers.h>
#include <wisdom/generated/dx12/dx12_structs.hpp>
#include <wisdom/util/misc.h>

wis::ResultValue<wis::DX12Buffer>
wis::DX12ResourceAllocator::CreateBuffer(const D3D12MA::ALLOCATION_DESC& all_desc, const D3D12_RESOURCE_DESC1& res_desc, D3D12_RESOURCE_STATES state) const noexcept
Expand All @@ -25,13 +26,16 @@ wis::DX12ResourceAllocator::CreateBuffer(const D3D12MA::ALLOCATION_DESC& all_des


wis::ResultValue<wis::DX12Buffer>
wis::DX12ResourceAllocator::CreateCommitedBuffer(size_t size, BufferFlags) const noexcept
wis::DX12ResourceAllocator::CreateCommitedBuffer(uint64_t size, BufferFlags flags) const noexcept
{
uint32_t alignment = flags & BufferFlags::ConstantBuffer ? D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT : 1;
size = wis::detail::aligned_size(size, alignment);

return CreateBuffer({ .HeapType = D3D12_HEAP_TYPE_DEFAULT }, CD3DX12_RESOURCE_DESC1::Buffer(size), D3D12_RESOURCE_STATE_COMMON);
}

wis::ResultValue<wis::DX12UploadBuffer>
wis::DX12ResourceAllocator::CreateUploadBuffer(size_t size) const noexcept
wis::DX12ResourceAllocator::CreateUploadBuffer(uint64_t size) const noexcept
{
auto buffer = CreateBuffer({ .HeapType = D3D12_HEAP_TYPE_UPLOAD }, CD3DX12_RESOURCE_DESC1::Buffer(size), D3D12_RESOURCE_STATE_GENERIC_READ);
return {
Expand Down
2 changes: 1 addition & 1 deletion wisdom/include/wisdom/generated/vulkan/vk_structs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ inline constexpr VkImageUsageFlags convert_vk(TextureUsage value) noexcept{
if(value & TextureUsage::DepthStencil) output |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
if(value & TextureUsage::CopySrc) output |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
if(value & TextureUsage::CopyDst) output |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if(value & TextureUsage::ShaderResource) output |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
if(value & TextureUsage::ShaderResource) output |= VK_IMAGE_USAGE_SAMPLED_BIT;
if(value & TextureUsage::UnorderedAccess) output |= VK_IMAGE_USAGE_STORAGE_BIT;
return output;
}
Expand Down
5 changes: 5 additions & 0 deletions wisdom/include/wisdom/util/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ template<class Type, class... Types, std::enable_if_t<!std::is_array_v<Type>, in
{ // make a unique_ptr
return std::unique_ptr<Type>(new (std::nothrow) Type(std::forward<Types>(Args)...));
}

constexpr inline uint32_t aligned_size(uint32_t size, uint32_t alignment) noexcept
{
return (size + alignment - 1) & ~(alignment - 1);
}
} // namespace wis::detail
6 changes: 3 additions & 3 deletions wisdom/include/wisdom/vulkan/impl/vk_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ wis::VKResourceAllocator::CreateBuffer(const VkBufferCreateInfo& desc, const Vma
}

wis::ResultValue<wis::VKBuffer>
wis::VKResourceAllocator::CreateCommitedBuffer(size_t size, BufferFlags flags) const noexcept
wis::VKResourceAllocator::CreateCommitedBuffer(uint64_t size, BufferFlags flags) const noexcept
{
VkBufferCreateInfo desc{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = size,
.usage = VkBufferUsageFlags(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VkBufferUsageFlagBits(flags)),
.usage = VkBufferUsageFlags(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VkBufferUsageFlagBits(flags)),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo alloc{
Expand All @@ -42,7 +42,7 @@ wis::VKResourceAllocator::CreateCommitedBuffer(size_t size, BufferFlags flags) c
}

wis::ResultValue<wis::VKUploadBuffer>
wis::VKResourceAllocator::CreateUploadBuffer(size_t size) const noexcept
wis::VKResourceAllocator::CreateUploadBuffer(uint64_t size) const noexcept
{
VkBufferCreateInfo desc{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Expand Down
Loading

0 comments on commit 4dcbd7a

Please sign in to comment.