Completed textured box.
This commit is contained in:
parent
e541be389d
commit
b48fb3168d
|
|
@ -0,0 +1,4 @@
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.png filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.glb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.jpg filter=lfs diff=lfs merge=lfs -text
|
||||||
|
|
@ -12,6 +12,7 @@ A Vulkan based renderer created with Vulkan 1.3 in C++.
|
||||||
- [ ] Load Camera
|
- [ ] Load Camera
|
||||||
- [ ] Load Lights
|
- [ ] Load Lights
|
||||||
- [ ] Support Specular Materials
|
- [ ] Support Specular Materials
|
||||||
|
- [ ] Bindless Descriptors
|
||||||
- [ ] PBR
|
- [ ] PBR
|
||||||
- [ ] IBL
|
- [ ] IBL
|
||||||
- [ ] Shadows v1
|
- [ ] Shadows v1
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,9 @@ set(SOURCE_FILES
|
||||||
device.cpp
|
device.cpp
|
||||||
swapchain.cpp
|
swapchain.cpp
|
||||||
pipeline.cpp
|
pipeline.cpp
|
||||||
buffer.cpp)
|
buffer.cpp
|
||||||
|
image.cpp
|
||||||
|
image.h)
|
||||||
|
|
||||||
add_library(aster_core STATIC ${SOURCE_FILES} ${HEADER_FILES})
|
add_library(aster_core STATIC ${SOURCE_FILES} ${HEADER_FILES})
|
||||||
set_property(TARGET aster_core PROPERTY CXX_STANDARD 20)
|
set_property(TARGET aster_core PROPERTY CXX_STANDARD 20)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
|
|
||||||
|
#include "device.h"
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "device.h"
|
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
|
||||||
struct Device;
|
struct Device;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
// =============================================
|
||||||
|
// Aster: image.cpp
|
||||||
|
// Copyright (c) 2020-2024 Anish Bhobe
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
#include "image.h"
|
||||||
|
|
||||||
|
#include "device.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
Image::Destroy(const Device *device)
|
||||||
|
{
|
||||||
|
if (!m_Image)
|
||||||
|
return;
|
||||||
|
|
||||||
|
vmaDestroyImage(device->m_Allocator, m_Image, m_Allocation);
|
||||||
|
m_Image = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Texture::Init(const Device *device, const vk::Extent2D extent, const bool isMipmapped, const cstr name)
|
||||||
|
{
|
||||||
|
const u32 mipLevels = isMipmapped ? 1 + Cast<u32>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
|
||||||
|
vk::ImageCreateInfo imageCreateInfo = {
|
||||||
|
.imageType = vk::ImageType::e2D,
|
||||||
|
.format = vk::Format::eR8G8B8A8Srgb,
|
||||||
|
.extent = {.width = extent.width, .height = extent.height, .depth = 1},
|
||||||
|
.mipLevels = mipLevels,
|
||||||
|
.arrayLayers = 1,
|
||||||
|
.samples = vk::SampleCountFlagBits::e1,
|
||||||
|
.tiling = vk::ImageTiling::eOptimal,
|
||||||
|
.usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst,
|
||||||
|
.sharingMode = vk::SharingMode::eExclusive,
|
||||||
|
.initialLayout = vk::ImageLayout::eUndefined,
|
||||||
|
};
|
||||||
|
constexpr VmaAllocationCreateInfo allocationCreateInfo = {
|
||||||
|
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||||
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkImage image;
|
||||||
|
VmaAllocation allocation;
|
||||||
|
auto result = Cast<vk::Result>(vmaCreateImage(device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
|
||||||
|
&allocationCreateInfo, &image, &allocation, nullptr));
|
||||||
|
ERROR_IF(Failed(result), "Could not allocate buffer. Cause: {}", result) THEN_ABORT(result);
|
||||||
|
|
||||||
|
m_Image = image;
|
||||||
|
m_Allocation = allocation;
|
||||||
|
m_Extent = {extent.width, extent.height, 1};
|
||||||
|
|
||||||
|
device->SetName(m_Image, name);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
// =============================================
|
||||||
|
// Aster: image.h
|
||||||
|
// Copyright (c) 2020-2024 Anish Bhobe
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "global.h"
|
||||||
|
|
||||||
|
struct Device;
|
||||||
|
|
||||||
|
struct Image
|
||||||
|
{
|
||||||
|
vk::Image m_Image = nullptr;
|
||||||
|
VmaAllocation m_Allocation = nullptr;
|
||||||
|
vk::Extent3D m_Extent;
|
||||||
|
usize m_Size = 0;
|
||||||
|
|
||||||
|
void Destroy(const Device *device);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Texture : Image
|
||||||
|
{
|
||||||
|
void Init(const Device *device, vk::Extent2D extent, bool isMipmapped, cstr name = nullptr);
|
||||||
|
};
|
||||||
|
|
@ -4,9 +4,13 @@ cmake_minimum_required(VERSION 3.13)
|
||||||
|
|
||||||
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fsanitize=address")
|
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fsanitize=address")
|
||||||
|
|
||||||
add_executable(box box.cpp)
|
add_executable(box box.cpp stb_image.h)
|
||||||
add_shader(box shader/box.vert.glsl)
|
add_shader(box shader/box.vert.glsl)
|
||||||
add_shader(box shader/box.frag.glsl)
|
add_shader(box shader/box.frag.glsl)
|
||||||
|
|
||||||
target_link_libraries(box PRIVATE aster_core)
|
target_link_libraries(box PRIVATE aster_core)
|
||||||
target_link_libraries(box PRIVATE util_helper)
|
target_link_libraries(box PRIVATE util_helper)
|
||||||
|
|
||||||
|
add_custom_target(copy-resource-files ALL
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/image/ ${CMAKE_CURRENT_BINARY_DIR}/
|
||||||
|
DEPENDS box)
|
||||||
|
|
@ -7,15 +7,18 @@
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "physical_device.h"
|
|
||||||
#include "window.h"
|
|
||||||
|
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#include "physical_device.h"
|
||||||
#include "pipeline.h"
|
#include "pipeline.h"
|
||||||
#include "swapchain.h"
|
#include "swapchain.h"
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "image.h"
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
#include <EASTL/array.h>
|
#include <EASTL/array.h>
|
||||||
|
|
||||||
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
||||||
|
|
@ -45,12 +48,56 @@ constexpr auto FRAGMENT_SHADER_FILE = "shader/box.frag.glsl.spv";
|
||||||
ERROR_IF(Failed(_checkResultValue_), MSG " Cause: {}", _checkResultValue_) THEN_ABORT(_checkResultValue_); \
|
ERROR_IF(Failed(_checkResultValue_), MSG " Cause: {}", _checkResultValue_) THEN_ABORT(_checkResultValue_); \
|
||||||
} while (false)
|
} while (false)
|
||||||
|
|
||||||
|
struct ImageFile
|
||||||
|
{
|
||||||
|
void *m_Data = nullptr;
|
||||||
|
u32 m_Width = 0;
|
||||||
|
u32 m_Height = 0;
|
||||||
|
u32 m_NumChannels = 0;
|
||||||
|
|
||||||
|
bool Load(cstr fileName);
|
||||||
|
usize GetSize() const;
|
||||||
|
~ImageFile();
|
||||||
|
};
|
||||||
|
|
||||||
|
bool
|
||||||
|
ImageFile::Load(cstr fileName)
|
||||||
|
{
|
||||||
|
int width, height, nrChannels;
|
||||||
|
m_Data = stbi_load(fileName, &width, &height, &nrChannels, 4);
|
||||||
|
ERROR_IF(!m_Data, "Could not load {}", fileName);
|
||||||
|
|
||||||
|
if (!m_Data)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Width = width;
|
||||||
|
m_Height = height;
|
||||||
|
m_NumChannels = 4;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
usize
|
||||||
|
ImageFile::GetSize() const
|
||||||
|
{
|
||||||
|
return m_Width * m_Height * m_NumChannels;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageFile::~ImageFile()
|
||||||
|
{
|
||||||
|
stbi_image_free(m_Data);
|
||||||
|
m_Data = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
vk::ShaderModule CreateShader(const Device *device, cstr shaderFile);
|
vk::ShaderModule CreateShader(const Device *device, cstr shaderFile);
|
||||||
Pipeline CreatePipeline(const Device *device, const Swapchain *swapchain);
|
Pipeline CreatePipeline(const Device *device, const Swapchain *swapchain);
|
||||||
|
|
||||||
struct Vertex
|
struct Vertex
|
||||||
{
|
{
|
||||||
vec3 m_Position;
|
vec3 m_Position;
|
||||||
|
vec2 m_UV0;
|
||||||
|
|
||||||
constexpr static vk::VertexInputBindingDescription
|
constexpr static vk::VertexInputBindingDescription
|
||||||
GetBinding(const u32 binding)
|
GetBinding(const u32 binding)
|
||||||
|
|
@ -58,7 +105,7 @@ struct Vertex
|
||||||
return {.binding = binding, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
|
return {.binding = binding, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr static eastl::array<vk::VertexInputAttributeDescription, 1>
|
constexpr static eastl::array<vk::VertexInputAttributeDescription, 2>
|
||||||
GetAttributes(const u32 binding)
|
GetAttributes(const u32 binding)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
|
|
@ -68,6 +115,12 @@ struct Vertex
|
||||||
.format = vk::Format::eR32G32B32Sfloat,
|
.format = vk::Format::eR32G32B32Sfloat,
|
||||||
.offset = offsetof(Vertex, m_Position),
|
.offset = offsetof(Vertex, m_Position),
|
||||||
},
|
},
|
||||||
|
vk::VertexInputAttributeDescription{
|
||||||
|
.location = 1,
|
||||||
|
.binding = binding,
|
||||||
|
.format = vk::Format::eR32G32Sfloat,
|
||||||
|
.offset = offsetof(Vertex, m_UV0),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -105,7 +158,10 @@ main(int, char **)
|
||||||
|
|
||||||
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
|
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
|
||||||
|
|
||||||
Features enabledDeviceFeatures = {.m_Vulkan13Features = {.dynamicRendering = true}};
|
Features enabledDeviceFeatures = {
|
||||||
|
.m_Vulkan10Features = {.samplerAnisotropy = true},
|
||||||
|
.m_Vulkan13Features = {.dynamicRendering = true},
|
||||||
|
};
|
||||||
QueueAllocation queueAllocation = FindAppropriateQueueAllocation(&deviceToUse);
|
QueueAllocation queueAllocation = FindAppropriateQueueAllocation(&deviceToUse);
|
||||||
Device device = {&context, &deviceToUse, &enabledDeviceFeatures, {queueAllocation}, "Primary Device"};
|
Device device = {&context, &deviceToUse, &enabledDeviceFeatures, {queueAllocation}, "Primary Device"};
|
||||||
vk::Queue commandQueue = device.GetQueue(queueAllocation.m_Family, 0);
|
vk::Queue commandQueue = device.GetQueue(queueAllocation.m_Family, 0);
|
||||||
|
|
@ -123,12 +179,18 @@ main(int, char **)
|
||||||
vk::DescriptorSet descriptorSet;
|
vk::DescriptorSet descriptorSet;
|
||||||
{
|
{
|
||||||
vk::DescriptorSetLayout descriptorSetLayout = pipeline.m_SetLayouts.front();
|
vk::DescriptorSetLayout descriptorSetLayout = pipeline.m_SetLayouts.front();
|
||||||
vk::DescriptorPoolSize poolSize = {
|
eastl::array poolSizes = {
|
||||||
|
vk::DescriptorPoolSize{
|
||||||
.type = vk::DescriptorType::eUniformBuffer,
|
.type = vk::DescriptorType::eUniformBuffer,
|
||||||
.descriptorCount = 1,
|
.descriptorCount = 1,
|
||||||
|
},
|
||||||
|
vk::DescriptorPoolSize{
|
||||||
|
.type = vk::DescriptorType::eCombinedImageSampler,
|
||||||
|
.descriptorCount = 1,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo = {
|
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo = {
|
||||||
.maxSets = 1, .poolSizeCount = 1, .pPoolSizes = &poolSize};
|
.maxSets = 1, .poolSizeCount = Cast<u32>(poolSizes.size()), .pPoolSizes = poolSizes.data()};
|
||||||
AbortIfFailed(device.m_Device.createDescriptorPool(&descriptorPoolCreateInfo, nullptr, &descriptorPool));
|
AbortIfFailed(device.m_Device.createDescriptorPool(&descriptorPoolCreateInfo, nullptr, &descriptorPool));
|
||||||
|
|
||||||
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo = {
|
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo = {
|
||||||
|
|
@ -157,25 +219,99 @@ main(int, char **)
|
||||||
"Copy command buffer allocation failed.");
|
"Copy command buffer allocation failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// eastl::array<Vertex, 3> vertices{};
|
|
||||||
eastl::array vertices = {
|
eastl::array vertices = {
|
||||||
vec3(-0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, -0.5f), vec3(0.5f, 0.5f, -0.5f), vec3(0.5f, 0.5f, -0.5f),
|
Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
vec3(-0.5f, 0.5f, -0.5f), vec3(-0.5f, -0.5f, -0.5f), vec3(-0.5f, -0.5f, 0.5f), vec3(0.5f, -0.5f, 0.5f),
|
Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
vec3(0.5f, 0.5f, 0.5f), vec3(0.5f, 0.5f, 0.5f), vec3(-0.5f, 0.5f, 0.5f), vec3(-0.5f, -0.5f, 0.5f),
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
vec3(-0.5f, 0.5f, 0.5f), vec3(-0.5f, 0.5f, -0.5f), vec3(-0.5f, -0.5f, -0.5f), vec3(-0.5f, -0.5f, -0.5f),
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
vec3(-0.5f, -0.5f, 0.5f), vec3(-0.5f, 0.5f, 0.5f), vec3(0.5f, 0.5f, 0.5f), vec3(0.5f, 0.5f, -0.5f),
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
vec3(0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, 0.5f), vec3(0.5f, 0.5f, 0.5f),
|
Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
vec3(-0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, 0.5f), vec3(0.5f, -0.5f, 0.5f),
|
|
||||||
vec3(-0.5f, -0.5f, 0.5f), vec3(-0.5f, -0.5f, -0.5f), vec3(-0.5f, 0.5f, -0.5f), vec3(0.5f, 0.5f, -0.5f),
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
vec3(0.5f, 0.5f, 0.5f), vec3(0.5f, 0.5f, 0.5f), vec3(-0.5f, 0.5f, 0.5f), vec3(-0.5f, 0.5f, -0.5f),
|
Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)},
|
||||||
|
Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)},
|
||||||
|
Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ImageFile imageFile;
|
||||||
|
bool loaded = imageFile.Load("image/container.jpg");
|
||||||
|
assert(loaded);
|
||||||
|
INFO("Image {}x{} : {} channels", imageFile.m_Width, imageFile.m_Height, imageFile.m_NumChannels);
|
||||||
|
|
||||||
VertexBuffer vbo;
|
VertexBuffer vbo;
|
||||||
|
Texture crate;
|
||||||
vbo.Init(&device, vertices.size() * sizeof vertices[0], "VBO");
|
vbo.Init(&device, vertices.size() * sizeof vertices[0], "VBO");
|
||||||
|
crate.Init(&device, {imageFile.m_Width, imageFile.m_Height}, false, "Crate Texture");
|
||||||
{
|
{
|
||||||
StagingBuffer staging;
|
StagingBuffer vertexStaging, imageStaging;
|
||||||
staging.Init(&device, vertices.size() * sizeof vertices[0], "Staging");
|
vertexStaging.Init(&device, vertices.size() * sizeof vertices[0], "Vertex Staging");
|
||||||
staging.Write(&device, 0, vertices.size() * sizeof vertices[0], vertices.data());
|
vertexStaging.Write(&device, 0, vertices.size() * sizeof vertices[0], vertices.data());
|
||||||
|
|
||||||
|
imageStaging.Init(&device, imageFile.GetSize(), "Image Staging");
|
||||||
|
INFO("fine {}", imageFile.GetSize());
|
||||||
|
imageStaging.Write(&device, 0, imageFile.GetSize(), imageFile.m_Data);
|
||||||
|
|
||||||
|
vk::ImageMemoryBarrier imageReadyToWrite = {
|
||||||
|
.oldLayout = vk::ImageLayout::eUndefined,
|
||||||
|
.newLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||||
|
.srcQueueFamilyIndex = queueAllocation.m_Family,
|
||||||
|
.dstQueueFamilyIndex = queueAllocation.m_Family,
|
||||||
|
.image = crate.m_Image,
|
||||||
|
.subresourceRange =
|
||||||
|
{
|
||||||
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
vk::ImageMemoryBarrier imageReadyToRead = {
|
||||||
|
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||||
|
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||||
|
.srcQueueFamilyIndex = queueAllocation.m_Family,
|
||||||
|
.dstQueueFamilyIndex = queueAllocation.m_Family,
|
||||||
|
.image = crate.m_Image,
|
||||||
|
.subresourceRange =
|
||||||
|
{
|
||||||
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
vk::Fence fence;
|
vk::Fence fence;
|
||||||
vk::FenceCreateInfo fenceCreateInfo = {};
|
vk::FenceCreateInfo fenceCreateInfo = {};
|
||||||
|
|
@ -183,9 +319,31 @@ main(int, char **)
|
||||||
|
|
||||||
vk::CommandBufferBeginInfo beginInfo = {.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit};
|
vk::CommandBufferBeginInfo beginInfo = {.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit};
|
||||||
AbortIfFailed(copyBuffer.begin(&beginInfo));
|
AbortIfFailed(copyBuffer.begin(&beginInfo));
|
||||||
|
copyBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eTransfer, {}, 0,
|
||||||
|
nullptr, 0, nullptr, 1, &imageReadyToWrite);
|
||||||
|
|
||||||
vk::BufferCopy bufferCopy = {.srcOffset = 0, .dstOffset = 0, .size = staging.GetSize()};
|
vk::BufferCopy bufferCopy = {.srcOffset = 0, .dstOffset = 0, .size = vertexStaging.GetSize()};
|
||||||
copyBuffer.copyBuffer(staging.m_Buffer, vbo.m_Buffer, 1, &bufferCopy);
|
copyBuffer.copyBuffer(vertexStaging.m_Buffer, vbo.m_Buffer, 1, &bufferCopy);
|
||||||
|
|
||||||
|
vk::BufferImageCopy imageCopy = {
|
||||||
|
.bufferOffset = 0,
|
||||||
|
.bufferRowLength = imageFile.m_Width,
|
||||||
|
.bufferImageHeight = imageFile.m_Height,
|
||||||
|
.imageSubresource =
|
||||||
|
{
|
||||||
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||||
|
.mipLevel = 0,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
},
|
||||||
|
.imageOffset = {},
|
||||||
|
.imageExtent = {imageFile.m_Width, imageFile.m_Height, 1},
|
||||||
|
};
|
||||||
|
copyBuffer.copyBufferToImage(imageStaging.m_Buffer, crate.m_Image, vk::ImageLayout::eTransferDstOptimal, 1,
|
||||||
|
&imageCopy);
|
||||||
|
|
||||||
|
copyBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, {},
|
||||||
|
0, nullptr, 0, nullptr, 1, &imageReadyToRead);
|
||||||
|
|
||||||
AbortIfFailed(copyBuffer.end());
|
AbortIfFailed(copyBuffer.end());
|
||||||
|
|
||||||
|
|
@ -203,7 +361,50 @@ main(int, char **)
|
||||||
AbortIfFailedM(device.m_Device.resetCommandPool(copyPool, {}), "Couldn't reset command pool.");
|
AbortIfFailedM(device.m_Device.resetCommandPool(copyPool, {}), "Couldn't reset command pool.");
|
||||||
|
|
||||||
device.m_Device.destroy(fence, nullptr);
|
device.m_Device.destroy(fence, nullptr);
|
||||||
staging.Destroy(&device);
|
vertexStaging.Destroy(&device);
|
||||||
|
imageStaging.Destroy(&device);
|
||||||
|
}
|
||||||
|
|
||||||
|
vk::ImageView imageView;
|
||||||
|
vk::Sampler sampler;
|
||||||
|
{
|
||||||
|
vk::ImageViewCreateInfo imageViewCreateInfo = {
|
||||||
|
.image = crate.m_Image,
|
||||||
|
.viewType = vk::ImageViewType::e2D,
|
||||||
|
.format = vk::Format::eR8G8B8A8Srgb,
|
||||||
|
.components =
|
||||||
|
vk::ComponentMapping{
|
||||||
|
.r = vk::ComponentSwizzle::eIdentity,
|
||||||
|
.g = vk::ComponentSwizzle::eIdentity,
|
||||||
|
.b = vk::ComponentSwizzle::eIdentity,
|
||||||
|
.a = vk::ComponentSwizzle::eIdentity,
|
||||||
|
},
|
||||||
|
.subresourceRange =
|
||||||
|
{
|
||||||
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
AbortIfFailed(device.m_Device.createImageView(&imageViewCreateInfo, nullptr, &imageView));
|
||||||
|
vk::SamplerCreateInfo samplerCreateInfo = {
|
||||||
|
.magFilter = vk::Filter::eLinear,
|
||||||
|
.minFilter = vk::Filter::eLinear,
|
||||||
|
.mipmapMode = vk::SamplerMipmapMode::eLinear,
|
||||||
|
.addressModeU = vk::SamplerAddressMode::eRepeat,
|
||||||
|
.addressModeV = vk::SamplerAddressMode::eRepeat,
|
||||||
|
.addressModeW = vk::SamplerAddressMode::eRepeat,
|
||||||
|
.mipLodBias = 0.2,
|
||||||
|
.anisotropyEnable = true,
|
||||||
|
.maxAnisotropy = 1.0f,
|
||||||
|
.compareEnable = false,
|
||||||
|
.minLod = 0,
|
||||||
|
.maxLod = 4,
|
||||||
|
.unnormalizedCoordinates = false,
|
||||||
|
};
|
||||||
|
AbortIfFailed(device.m_Device.createSampler(&samplerCreateInfo, nullptr, &sampler));
|
||||||
}
|
}
|
||||||
|
|
||||||
UniformBuffer ubo;
|
UniformBuffer ubo;
|
||||||
|
|
@ -214,15 +415,30 @@ main(int, char **)
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.range = ubo.GetSize(),
|
.range = ubo.GetSize(),
|
||||||
};
|
};
|
||||||
vk::WriteDescriptorSet writeDescriptors = {
|
vk::DescriptorImageInfo descriptorImageInfo = {
|
||||||
|
.sampler = sampler,
|
||||||
|
.imageView = imageView,
|
||||||
|
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||||
|
};
|
||||||
|
eastl::array writeDescriptors = {
|
||||||
|
vk::WriteDescriptorSet{
|
||||||
.dstSet = descriptorSet,
|
.dstSet = descriptorSet,
|
||||||
.dstBinding = 0,
|
.dstBinding = 0,
|
||||||
.dstArrayElement = 0,
|
.dstArrayElement = 0,
|
||||||
.descriptorCount = 1,
|
.descriptorCount = 1,
|
||||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||||
.pBufferInfo = &descriptorBufferInfo,
|
.pBufferInfo = &descriptorBufferInfo,
|
||||||
|
},
|
||||||
|
vk::WriteDescriptorSet{
|
||||||
|
.dstSet = descriptorSet,
|
||||||
|
.dstBinding = 1,
|
||||||
|
.dstArrayElement = 0,
|
||||||
|
.descriptorCount = 1,
|
||||||
|
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||||
|
.pImageInfo = &descriptorImageInfo,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
device.m_Device.updateDescriptorSets(1, &writeDescriptors, 0, nullptr);
|
device.m_Device.updateDescriptorSets(Cast<u32>(writeDescriptors.size()), writeDescriptors.data(), 0, nullptr);
|
||||||
|
|
||||||
// Persistent variables
|
// Persistent variables
|
||||||
vk::Viewport viewport = {
|
vk::Viewport viewport = {
|
||||||
|
|
@ -400,9 +616,12 @@ main(int, char **)
|
||||||
|
|
||||||
AbortIfFailed(device.m_Device.waitIdle());
|
AbortIfFailed(device.m_Device.waitIdle());
|
||||||
|
|
||||||
|
device.m_Device.destroy(sampler, nullptr);
|
||||||
|
device.m_Device.destroy(imageView, nullptr);
|
||||||
ubo.Destroy(&device);
|
ubo.Destroy(&device);
|
||||||
device.m_Device.destroy(descriptorPool, nullptr);
|
device.m_Device.destroy(descriptorPool, nullptr);
|
||||||
device.m_Device.destroy(copyPool, nullptr);
|
device.m_Device.destroy(copyPool, nullptr);
|
||||||
|
crate.Destroy(&device);
|
||||||
vbo.Destroy(&device);
|
vbo.Destroy(&device);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -460,15 +679,23 @@ CreatePipeline(const Device *device, const Swapchain *swapchain)
|
||||||
},
|
},
|
||||||
}};
|
}};
|
||||||
|
|
||||||
vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding = {
|
eastl::array descriptorSetLayoutBinding = {
|
||||||
|
vk::DescriptorSetLayoutBinding{
|
||||||
.binding = 0,
|
.binding = 0,
|
||||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||||
.descriptorCount = 1,
|
.descriptorCount = 1,
|
||||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
|
.stageFlags = vk::ShaderStageFlagBits::eVertex,
|
||||||
|
},
|
||||||
|
vk::DescriptorSetLayoutBinding{
|
||||||
|
.binding = 1,
|
||||||
|
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||||
|
.descriptorCount = 1,
|
||||||
|
.stageFlags = vk::ShaderStageFlagBits::eFragment,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {
|
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {
|
||||||
.bindingCount = 1,
|
.bindingCount = Cast<u32>(descriptorSetLayoutBinding.size()),
|
||||||
.pBindings = &descriptorSetLayoutBinding,
|
.pBindings = descriptorSetLayoutBinding.data(),
|
||||||
};
|
};
|
||||||
vk::DescriptorSetLayout descriptorSetLayout;
|
vk::DescriptorSetLayout descriptorSetLayout;
|
||||||
AbortIfFailed(
|
AbortIfFailed(
|
||||||
|
|
@ -507,7 +734,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain)
|
||||||
.depthClampEnable = false,
|
.depthClampEnable = false,
|
||||||
.rasterizerDiscardEnable = false,
|
.rasterizerDiscardEnable = false,
|
||||||
.polygonMode = vk::PolygonMode::eFill,
|
.polygonMode = vk::PolygonMode::eFill,
|
||||||
.cullMode = vk::CullModeFlagBits::eNone,
|
.cullMode = vk::CullModeFlagBits::eBack,
|
||||||
.frontFace = vk::FrontFace::eCounterClockwise,
|
.frontFace = vk::FrontFace::eCounterClockwise,
|
||||||
.depthBiasEnable = false,
|
.depthBiasEnable = false,
|
||||||
.lineWidth = 1.0,
|
.lineWidth = 1.0,
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,9 +1,11 @@
|
||||||
#version 450
|
#version 450
|
||||||
#pragma shader_stage(fragment)
|
#pragma shader_stage(fragment)
|
||||||
|
|
||||||
layout (location = 0) in vec3 inColor;
|
layout (location = 0) in vec2 inUV;
|
||||||
layout (location = 0) out vec4 outColor;
|
layout (location = 0) out vec4 outColor;
|
||||||
|
|
||||||
|
layout(binding = 1) uniform sampler2D tex;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
outColor = vec4(inColor, 1.0);
|
outColor = vec4(texture(tex, inUV).rgb, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
@ -2,8 +2,9 @@
|
||||||
#pragma shader_stage(vertex)
|
#pragma shader_stage(vertex)
|
||||||
|
|
||||||
layout(location=0) in vec4 position;
|
layout(location=0) in vec4 position;
|
||||||
|
layout(location=1) in vec2 uv0;
|
||||||
|
|
||||||
layout(location=0) out vec3 outColor;
|
layout(location=0) out vec2 outUV;
|
||||||
|
|
||||||
layout(binding=0) uniform Camera {
|
layout(binding=0) uniform Camera {
|
||||||
mat4 model;
|
mat4 model;
|
||||||
|
|
@ -12,6 +13,7 @@ layout(binding=0) uniform Camera {
|
||||||
} ubo;
|
} ubo;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
outUV = uv0;
|
||||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(position.xyz, 1.0f);
|
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(position.xyz, 1.0f);
|
||||||
outColor = vec3(0.5f, 0.3f, 0.1f);
|
// outColor = vec3(0.5f, 0.3f, 0.1f);
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue