WE GOT TEXTURES BABY

This commit is contained in:
2026-01-08 02:43:34 +01:00
parent e306c5e23f
commit 10b00b0525
24 changed files with 828 additions and 272 deletions

View File

@@ -7,7 +7,7 @@
#include "glm/gtx/transform.hpp"
#include "spdlog/spdlog.h"
App::App(): renderer{meshCache} {
App::App(): renderer{meshCache, materialCache} {
}
void App::init(const AppParams& params) {
@@ -34,6 +34,7 @@ void App::init(const AppParams& params) {
}
gfxDevice.init(window, params.appName, false);
materialCache.init(gfxDevice);
renderer.init(gfxDevice, params.renderSize);
//Read whole file
@@ -47,14 +48,22 @@ void App::init(const AppParams& params) {
testMeshID = meshCache.addMesh(gfxDevice, testMesh);
spdlog::info("TestMesh uploaded with id: {}", testMeshID);
const auto testimgpath = AssetFS::GetInstance().GetFullPath("engine://textures/kobe.png");
auto testimgID = gfxDevice.loadImageFromFile(testimgpath);
spdlog::info("Test image loaded with id: {}", testimgID);
testMaterialID = materialCache.addMaterial(gfxDevice, {
.baseColor = glm::vec3(1.f),
.diffuseTexture = testimgID,
});
spdlog::info("Test material created with id: {}", testMaterialID);
float aspectRatio = static_cast<float>(params.renderSize.x) / static_cast<float>(params.renderSize.y);
camera.setAspectRatio(aspectRatio);
//Look 90 deg to the right
camera.SetRotation(glm::radians(glm::vec2(90.f, 0.f)));
inputManager.Init();
InputManager::GetInstance().Init();
}
void App::run() {
@@ -90,7 +99,7 @@ void App::run() {
}
while (accumulator >= dt) {
inputManager.BeginFrame();
InputManager::GetInstance().BeginFrame();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
@@ -106,39 +115,12 @@ void App::run() {
break;
}
}
inputManager.ProcessEvent(event);
InputManager::GetInstance().ProcessEvent(event);
if (inputManager.IsKeyDown(SDL_SCANCODE_W)) {
camera.m_position += camera.GetForward() * dt * 5.f;
}
if (inputManager.IsKeyDown(SDL_SCANCODE_S)) {
camera.m_position -= camera.GetForward() * dt * 5.f;
}
if (inputManager.IsKeyDown(SDL_SCANCODE_A)) {
camera.m_position -= camera.GetRight() * dt * 5.f;
}
if (inputManager.IsKeyDown(SDL_SCANCODE_D)) {
camera.m_position += camera.GetRight() * dt * 5.f;
}
// rotation
if (inputManager.IsKeyDown(SDL_SCANCODE_LEFT)) {
camera.SetRotation(camera.GetYaw() - glm::radians(90.f) * dt, camera.GetPitch());
}
if (inputManager.IsKeyDown(SDL_SCANCODE_RIGHT)) {
camera.SetRotation(camera.GetYaw() + glm::radians(90.f) * dt, camera.GetPitch());
}
if (inputManager.IsKeyDown(SDL_SCANCODE_UP)) {
camera.SetRotation(camera.GetYaw(), camera.GetPitch() + glm::radians(90.f) * dt);
}
if (inputManager.IsKeyDown(SDL_SCANCODE_DOWN)) {
camera.SetRotation(camera.GetYaw(), camera.GetPitch() - glm::radians(90.f) * dt);
}
camera.Update(dt);
}
camera.Update(dt);
if (gfxDevice.needsSwapchainRecreate()) {
spdlog::info("Recreating swapchain to size: {}x{}", m_params.windowSize.x, m_params.windowSize.y);
gfxDevice.recreateSwapchain(m_params.windowSize.x, m_params.windowSize.y);
@@ -154,7 +136,7 @@ void App::run() {
glm::mat4 objMatrix = glm::mat4(1.f);
objMatrix = glm::translate(objMatrix, glm::vec3(0.f, -3.0f, 0.f));
renderer.beginDrawing(gfxDevice);
renderer.drawMesh(testMeshID, glm::mat4(1.f), 0);
renderer.drawMesh(testMeshID, glm::mat4(1.f), testMaterialID);
renderer.drawMesh(testMeshID, objMatrix, 0);
renderer.endDrawing();

View File

@@ -0,0 +1,184 @@
#include <destrum/Graphics/BindlessSetManager.h>
#include <array>
#include <volk.h>
#include <destrum/Graphics/Util.h>
namespace
{
static const std::uint32_t maxBindlessResources = 16536;
static const std::uint32_t maxSamplers = 32;
static const std::uint32_t texturesBinding = 0;
static const std::uint32_t samplersBinding = 1;
}
void BindlessSetManager::init(VkDevice device, float maxAnisotropy)
{
{ // create pool
const auto poolSizesBindless = std::array<VkDescriptorPoolSize, 2>{{
{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, maxBindlessResources},
{VK_DESCRIPTOR_TYPE_SAMPLER, maxSamplers},
}};
const auto poolInfo = VkDescriptorPoolCreateInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT,
.maxSets = 10,
.poolSizeCount = (std::uint32_t)poolSizesBindless.size(),
.pPoolSizes = poolSizesBindless.data(),
};
VK_CHECK(vkCreateDescriptorPool(device, &poolInfo, nullptr, &descPool));
}
{ // build desc set layout
const auto bindings = std::array<VkDescriptorSetLayoutBinding, 2>{{
{
.binding = texturesBinding,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.descriptorCount = maxBindlessResources,
.stageFlags = VK_SHADER_STAGE_ALL,
},
{
.binding = samplersBinding,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.descriptorCount = maxSamplers,
.stageFlags = VK_SHADER_STAGE_ALL,
},
}};
const VkDescriptorBindingFlags bindlessFlags =
VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT;
const auto bindingFlags = std::array{bindlessFlags, bindlessFlags};
const auto flagInfo = VkDescriptorSetLayoutBindingFlagsCreateInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.bindingCount = (std::uint32_t)bindingFlags.size(),
.pBindingFlags = bindingFlags.data(),
};
const auto info = VkDescriptorSetLayoutCreateInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &flagInfo,
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT,
.bindingCount = (std::uint32_t)bindings.size(),
.pBindings = bindings.data(),
};
VK_CHECK(vkCreateDescriptorSetLayout(device, &info, nullptr, &descSetLayout));
}
{ // alloc desc set
const auto allocInfo = VkDescriptorSetAllocateInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = descPool,
.descriptorSetCount = 1,
.pSetLayouts = &descSetLayout,
};
std::uint32_t maxBinding = maxBindlessResources - 1;
const auto countInfo = VkDescriptorSetVariableDescriptorCountAllocateInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO,
.descriptorSetCount = 1,
.pDescriptorCounts = &maxBinding,
};
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &descSet));
}
initDefaultSamplers(device, maxAnisotropy);
}
void BindlessSetManager::initDefaultSamplers(VkDevice device, float maxAnisotropy)
{
// Keep in sync with bindless.glsl
static const std::uint32_t nearestSamplerId = 0;
static const std::uint32_t linearSamplerId = 1;
static const std::uint32_t shadowSamplerId = 2;
{ // init nearest sampler
const auto samplerCreateInfo = VkSamplerCreateInfo{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_NEAREST,
.minFilter = VK_FILTER_NEAREST,
};
VK_CHECK(vkCreateSampler(device, &samplerCreateInfo, nullptr, &nearestSampler));
vkutil::addDebugLabel(device, nearestSampler, "nearest");
addSampler(device, nearestSamplerId, nearestSampler);
}
{ // init linear sampler
const auto samplerCreateInfo = VkSamplerCreateInfo{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
// TODO: make possible to disable anisotropy or set other values?
.anisotropyEnable = VK_TRUE,
.maxAnisotropy = maxAnisotropy,
};
VK_CHECK(vkCreateSampler(device, &samplerCreateInfo, nullptr, &linearSampler));
vkutil::addDebugLabel(device, linearSampler, "linear");
addSampler(device, linearSamplerId, linearSampler);
}
{ // init shadow map sampler
const auto samplerCreateInfo = VkSamplerCreateInfo{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.compareEnable = VK_TRUE,
.compareOp = VK_COMPARE_OP_GREATER_OR_EQUAL,
};
VK_CHECK(vkCreateSampler(device, &samplerCreateInfo, nullptr, &shadowMapSampler));
vkutil::addDebugLabel(device, shadowMapSampler, "shadow");
addSampler(device, shadowSamplerId, shadowMapSampler);
}
}
void BindlessSetManager::cleanup(VkDevice device)
{
vkDestroySampler(device, nearestSampler, nullptr);
vkDestroySampler(device, linearSampler, nullptr);
vkDestroySampler(device, shadowMapSampler, nullptr);
vkDestroyDescriptorSetLayout(device, descSetLayout, nullptr);
vkDestroyDescriptorPool(device, descPool, nullptr);
}
void BindlessSetManager::addImage(
const VkDevice device,
std::uint32_t id,
const VkImageView imageView)
{
const auto imageInfo = VkDescriptorImageInfo{
.imageView = imageView, .imageLayout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL};
const auto writeSet = VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descSet,
.dstBinding = texturesBinding,
.dstArrayElement = id,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.pImageInfo = &imageInfo,
};
vkUpdateDescriptorSets(device, 1, &writeSet, 0, nullptr);
}
void BindlessSetManager::addSampler(const VkDevice device, std::uint32_t id, VkSampler sampler)
{
const auto imageInfo =
VkDescriptorImageInfo{.sampler = sampler, .imageLayout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL};
const auto writeSet = VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descSet,
.dstBinding = samplersBinding,
.dstArrayElement = id,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &imageInfo,
};
vkUpdateDescriptorSets(device, 1, &writeSet, 0, nullptr);
}

View File

@@ -1,4 +1,5 @@
#include <destrum/Graphics/Camera.h>
#include <destrum/Input/InputManager.h>
#include <iostream>
#include <glm/gtc/matrix_inverse.hpp>
@@ -6,146 +7,112 @@
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include "glm/gtx/norm.hpp"
Camera::Camera(const glm::vec3& position, const glm::vec3& up): m_position{position}, m_up{up} {
}
void Camera::Update(float deltaTime) {
// const auto MyWindow = static_cast<Window*>(glfwGetWindowUserPointer(Window::gWindow));
//
// #pragma region Keyboard Movement
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
//
// float MovementSpeed = m_movementSpeed;
// if (glfwGetKey(Window::gWindow, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
// MovementSpeed *= 2.0f;
// }
//
// if (glfwGetKey(Window::gWindow, GLFW_KEY_W) == GLFW_PRESS) {
// m_position += m_forward * deltaTime * MovementSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_A) == GLFW_PRESS) {
// m_position -= m_right * deltaTime * MovementSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_S) == GLFW_PRESS) {
// m_position -= m_forward * deltaTime * MovementSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_D) == GLFW_PRESS) {
// m_position += m_right * deltaTime * MovementSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_E) == GLFW_PRESS) {
// m_position += m_up * deltaTime * MovementSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_Q) == GLFW_PRESS) {
// m_position -= m_up * deltaTime * MovementSpeed;
// }
//
// // Looking around with arrow keys
// constexpr float lookSpeed = 1.0f * glm::radians(1.f);
// if (glfwGetKey(Window::gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
// totalPitch += lookSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
// totalPitch -= lookSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
// totalYaw -= lookSpeed;
// }
// if (glfwGetKey(Window::gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
// totalYaw += lookSpeed;
// }
//
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>(), glm::half_pi<float>());
//
// const glm::mat4 yawMatrix = glm::rotate(glm::mat4(1.0f), -totalYaw, glm::vec3(0, 1, 0));
// const glm::mat4 pitchMatrix = glm::rotate(glm::mat4(1.0f), totalPitch, glm::vec3(0, 0, 1));
//
// const glm::mat4 rotationMatrix = yawMatrix * pitchMatrix;
//
// m_forward = glm::vec3(rotationMatrix * glm::vec4(1, 0, 0, 0));
// m_right = glm::normalize(glm::cross(m_forward, glm::vec3(0, 1, 0)));
// m_up = glm::normalize(glm::cross(m_right, m_forward));
// #pragma endregion
//
// #pragma region Mouse Looking
// static bool wasCursorLockedLastFrame = false;
// static double lastMouseX = 0.0;
// static double lastMouseY = 0.0;
// static bool firstMouse = true;
//
// bool isLocked = MyWindow->isCursorLocked();
// if (isLocked) {
// double mouseX, mouseY;
// glfwGetCursorPos(Window::gWindow, &mouseX, &mouseY);
//
// // Reset mouse reference when locking the cursor (prevents jump)
// if (!wasCursorLockedLastFrame) {
// lastMouseX = mouseX;
// lastMouseY = mouseY;
// firstMouse = false;
// }
//
// float xOffset = static_cast<float>(mouseX - lastMouseX);
// float yOffset = static_cast<float>(lastMouseY - mouseY); // Reversed: y goes up
//
// lastMouseX = mouseX;
// lastMouseY = mouseY;
//
// constexpr float sensitivity = 0.002f;
// xOffset *= sensitivity;
// yOffset *= sensitivity;
//
// totalYaw += xOffset;
// totalPitch += yOffset;
//
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
//
// m_frustum.update(m_projectionMatrix * m_viewMatrix);
// }
//
// wasCursorLockedLastFrame = isLocked;
// #pragma endregion
//
// #pragma region Controller Movement
//
// if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) {
// GLFWgamepadstate state;
// if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1) && glfwGetGamepadState(GLFW_JOYSTICK_1, &state)) {
// float moveSpeed = MovementSpeed * deltaTime;
// const float rotSpeed = 1.5f * deltaTime;
//
//
// //TODO: god knows what this is
// auto deadzone = [] (float value, float threshold = 0.1f) {
// if (fabs(value) < threshold)
// return 0.0f;
// const float sign = (value > 0) ? 1.0f : -1.0f;
// return sign * (fabs(value) - threshold) / (1.0f - threshold);
// };
//
// //LT is x2 speed
// const bool isLTPressed = state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER] == GLFW_PRESS;
// if (isLTPressed) {
// moveSpeed *= 3.0f;
// }
//
// const float lx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_X]);
// const float ly = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]);
// const float rx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X]);
// const float ry = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]);
//
// m_position += m_forward * (-ly * moveSpeed);
// m_position += m_right * (lx * moveSpeed);
//
// const float lTrigger = (state.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] + 1.0f) / 2.0f;
// const float rTrigger = (state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] + 1.0f) / 2.0f;
// const float vertical = (rTrigger - lTrigger);
// m_position += m_up * vertical * moveSpeed;
//
// totalYaw += rx * rotSpeed;
// totalPitch -= ry * rotSpeed;
// }
// }
// #pragma endregion
auto& input = InputManager::GetInstance();
// --- tuning ---
float moveSpeed = m_movementSpeed;
if (input.IsKeyDown(SDL_SCANCODE_LSHIFT) || input.IsKeyDown(SDL_SCANCODE_RSHIFT)) {
moveSpeed *= 2.0f;
}
// Controller speed boost (pad0 LB)
const SDL_JoystickID pad0 = input.GetPadInstanceId(0);
if (pad0 >= 0 && input.IsPadButtonDown(pad0, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) {
moveSpeed *= 3.0f;
}
// Clamp pitch like your old code
m_pitch = glm::clamp(m_pitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
// =========================
// Movement (Keyboard)
// =========================
glm::vec3 move(0.0f);
if (input.IsKeyDown(SDL_SCANCODE_W)) move += m_forward;
if (input.IsKeyDown(SDL_SCANCODE_S)) move -= m_forward;
if (input.IsKeyDown(SDL_SCANCODE_D)) move += m_right;
if (input.IsKeyDown(SDL_SCANCODE_A)) move -= m_right;
if (input.IsKeyDown(SDL_SCANCODE_Q)) move += m_up;
if (input.IsKeyDown(SDL_SCANCODE_E)) move -= m_up;
if (glm::length2(move) > 0.0f) {
move = glm::normalize(move);
m_position += move * (moveSpeed * deltaTime);
}
// =========================
// Movement (Controller)
// =========================
if (pad0 >= 0) {
const float lx = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_LEFTX); // [-1..1]
const float ly = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_LEFTY); // [-1..1]
// SDL Y is typically +down, so invert for "forward"
glm::vec3 padMove(0.0f);
padMove += m_forward * (-ly);
padMove += m_right * ( lx);
// Triggers for vertical movement (optional)
// SDL controller triggers are axes too: 0..1-ish after normalization in our helper, but signless.
// With our NormalizeAxis, triggers will sit near 0 until pressed (depending on mapping).
// If your NormalizeAxis maps triggers weirdly, swap to raw event value approach.
const float lt = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
const float rt = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
const float vertical = (rt - lt);
padMove += m_up * vertical;
if (glm::length2(padMove) > 0.0001f) {
// do NOT normalize: preserve analog magnitude for smooth movement
m_position += padMove * (moveSpeed * deltaTime);
}
}
// =========================
// Look (Keyboard arrows only)
// =========================
// Use radians/sec so framerate-independent
const float keyLookSpeed = glm::radians(120.0f); // degrees per second
if (input.IsKeyDown(SDL_SCANCODE_UP)) m_pitch += keyLookSpeed * deltaTime;
if (input.IsKeyDown(SDL_SCANCODE_DOWN)) m_pitch -= keyLookSpeed * deltaTime;
if (input.IsKeyDown(SDL_SCANCODE_LEFT)) m_yaw -= keyLookSpeed * deltaTime;
if (input.IsKeyDown(SDL_SCANCODE_RIGHT)) m_yaw += keyLookSpeed * deltaTime;
// =========================
// Look (Controller right stick)
// =========================
if (pad0 >= 0) {
const float rx = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_RIGHTX);
const float ry = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_RIGHTY);
const float padLookSpeed = 2.2f; // radians/sec at full deflection
m_yaw += rx * padLookSpeed * deltaTime;
m_pitch -= ry * padLookSpeed * deltaTime;
}
// Clamp pitch again after modifications
m_pitch = glm::clamp(m_pitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
// Recompute basis from yaw/pitch (same convention you used)
const glm::mat4 yawMatrix = glm::rotate(glm::mat4(1.0f), -m_yaw, glm::vec3(0, 1, 0));
const glm::mat4 pitchMatrix = glm::rotate(glm::mat4(1.0f), m_pitch, glm::vec3(0, 0, 1));
const glm::mat4 rotation = yawMatrix * pitchMatrix;
m_forward = glm::normalize(glm::vec3(rotation * glm::vec4(1, 0, 0, 0))); // +X forward
m_right = glm::normalize(glm::cross(m_forward, glm::vec3(0, 1, 0)));
m_up = glm::normalize(glm::cross(m_right, m_forward));
// keep target mode off when manually controlled
m_useTarget = false;
CalculateProjectionMatrix();
CalculateViewMatrix();

View File

@@ -99,6 +99,23 @@ void GfxDevice::init(SDL_Window* window, const std::string& appName, bool vSync)
swapchainFormat = VK_FORMAT_B8G8R8A8_SRGB;
swapchain.createSwapchain(this, swapchainFormat, w, h, vSync);
VkPhysicalDeviceProperties props{};
vkGetPhysicalDeviceProperties(physicalDevice, &props);
imageCache.bindlessSetManager.init(device, props.limits.maxSamplerAnisotropy);
{ // create white texture
std::uint32_t pixel = 0xFFFFFFFF;
whiteImageId = createImage(
{
.format = VK_FORMAT_R8G8B8A8_UNORM,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
.extent = VkExtent3D{1, 1, 1},
},
"white texture",
&pixel);
}
swapchain.initSync(device);
const auto poolCreateInfo = vkinit::commandPoolCreateInfo(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, graphicsQueueFamily);
@@ -223,6 +240,30 @@ void GfxDevice::waitIdle() {
VK_CHECK(vkDeviceWaitIdle(device));
}
BindlessSetManager& GfxDevice::getBindlessSetManager() {
return imageCache.bindlessSetManager;
}
VkDescriptorSetLayout GfxDevice::getBindlessDescSetLayout() const {
return imageCache.bindlessSetManager.getDescSetLayout();
}
const VkDescriptorSet& GfxDevice::getBindlessDescSet() const {
return imageCache.bindlessSetManager.getDescSet();
}
void GfxDevice::bindBindlessDescSet(VkCommandBuffer cmd, VkPipelineLayout layout) const {
vkCmdBindDescriptorSets(
cmd,
VK_PIPELINE_BIND_POINT_GRAPHICS,
layout,
0,
1,
&imageCache.bindlessSetManager.getDescSet(),
0,
nullptr);
}
void GfxDevice::immediateSubmit(ImmediateExecuteFunction&& f) const {
executor.immediateSubmit(std::move(f));
}

View File

@@ -52,7 +52,7 @@ ImageID ImageCache::addImage(ImageID id, GPUImage image)
} else {
images.push_back(std::move(image));
}
// bindlessSetManager.addImage(gfxDevice.getDevice(), id, image.imageView);
bindlessSetManager.addImage(gfxDevice.getDevice(), id, image.imageView);
return id;
}

View File

@@ -0,0 +1,77 @@
#include <destrum/Graphics/MaterialCache.h>
#include <destrum/Graphics/GfxDevice.h>
#include <destrum/Graphics/Util.h>
#include "spdlog/spdlog.h"
void MaterialCache::init(GfxDevice& gfxDevice)
{
materialDataBuffer = gfxDevice.createBuffer(
MAX_MATERIALS * sizeof(MaterialData),
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
vkutil::addDebugLabel(gfxDevice.getDevice(), materialDataBuffer.buffer, "material data");
{ // create default normal map texture
std::uint32_t normal = 0xFFFF8080; // (0.5, 0.5, 1.0, 1.0)
defaultNormalMapTextureID = gfxDevice.createImage(
{
.format = VK_FORMAT_R8G8B8A8_UNORM,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
.extent = VkExtent3D{1, 1, 1},
},
"normal map placeholder texture",
&normal);
}
Material placeholderMaterial{.name = "PLACEHOLDER_MATERIAL"};
placeholderMaterialId = addMaterial(gfxDevice, placeholderMaterial);
}
void MaterialCache::cleanup(GfxDevice& gfxDevice)
{
gfxDevice.destroyBuffer(materialDataBuffer);
}
MaterialID MaterialCache::addMaterial(GfxDevice& gfxDevice, Material material)
{
const auto getTextureOrElse = [](ImageID imageId, ImageID placeholder) {
spdlog::warn("Using placeholder texture for material given texture ID: {}", imageId);
return imageId != NULL_IMAGE_ID ? imageId : placeholder;
};
// store on GPU
MaterialData* data = (MaterialData*)materialDataBuffer.info.pMappedData;
auto whiteTextureID = gfxDevice.getWhiteTextureID();
auto id = getFreeMaterialId();
assert(id < MAX_MATERIALS);
data[id] = MaterialData{
.baseColor = glm::vec4(material.baseColor, 1.0f),
.metalRoughnessEmissive = glm::vec4{material.metallicFactor, material.roughnessFactor, material.emissiveFactor, 0.f},
.diffuseTex = getTextureOrElse(material.diffuseTexture, whiteTextureID),
.normalTex = whiteTextureID,
.metallicRoughnessTex = whiteTextureID,
.emissiveTex = whiteTextureID,
};
// store on CPU
materials.push_back(std::move(material));
return id;
}
const Material& MaterialCache::getMaterial(MaterialID id) const
{
return materials.at(id);
}
MaterialID MaterialCache::getFreeMaterialId() const
{
return materials.size();
}
MaterialID MaterialCache::getPlaceholderMaterialId() const
{
assert(placeholderMaterialId != NULL_MATERIAL_ID && "MaterialCache::init not called");
return placeholderMaterialId;
}

View File

@@ -28,14 +28,14 @@ void MeshPipeline::init(GfxDevice& gfxDevice, VkFormat drawImageFormat, VkFormat
};
const auto pushConstantRanges = std::array{bufferRange};
// const auto layouts = std::array{gfxDevice.getBindlessDescSetLayout()};
const auto layouts = std::array{gfxDevice.getBindlessDescSetLayout()};
// m_pipelineLayout = vkutil::createPipelineLayout(device, layouts, pushConstantRanges);
// vkutil::addDebugLabel(device, pipelineLayout, "mesh pipeline layout");
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
// pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
// pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(layouts.size());
pipelineLayoutInfo.pSetLayouts = layouts.data();
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = pushConstantRanges.data();
@@ -72,9 +72,8 @@ void MeshPipeline::draw(VkCommandBuffer cmd,
const GPUBuffer& sceneDataBuffer,
const std::vector<MeshDrawCommand>& drawCommands,
const std::vector<std::size_t>& sortedDrawCommands) {
// vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
m_pipeline->bind(cmd);
// gfxDevice.bindBindlessDescSet(cmd, pipelineLayout);
gfxDevice.bindBindlessDescSet(cmd, m_pipelineLayout);
const auto viewport = VkViewport{
.x = 0,

View File

@@ -4,7 +4,7 @@
#include "spdlog/spdlog.h"
GameRenderer::GameRenderer(MeshCache& meshCache): meshCache{meshCache} {
GameRenderer::GameRenderer(MeshCache& meshCache, MaterialCache& matCache): meshCache{meshCache}, materialCache{matCache} {
}
void GameRenderer::init(GfxDevice& gfxDevice, const glm::ivec2& drawImageSize) {
@@ -89,7 +89,7 @@ void GameRenderer::draw(VkCommandBuffer cmd, GfxDevice& gfxDevice, const Camera&
void GameRenderer::cleanup() {
}
void GameRenderer::drawMesh(MeshID id, const glm::mat4& transform, MaterialId materialId) {
void GameRenderer::drawMesh(MeshID id, const glm::mat4& transform, MaterialID materialId) {
const auto& mesh = meshCache.getMesh(id);
// const auto worldBoundingSphere = edge::calculateBoundingSphereWorld(transform, mesh.boundingSphere, false);
assert(materialId != NULL_MATERIAL_ID);

View File

@@ -101,6 +101,16 @@ void vkutil::addDebugLabel(VkDevice device, VkBuffer buffer, const char* label)
vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
}
void vkutil::addDebugLabel(VkDevice device, VkSampler sampler, const char* label) {
const auto nameInfo = VkDebugUtilsObjectNameInfoEXT{
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.objectType = VK_OBJECT_TYPE_SAMPLER,
.objectHandle = (std::uint64_t)sampler,
.pObjectName = label,
};
vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
}
vkutil::RenderInfo vkutil::createRenderingInfo(const RenderingInfoParams& params) {
assert(
(params.colorImageView || params.depthImageView != nullptr) &&

View File

@@ -1,8 +1,38 @@
#include <destrum/Input/InputManager.h>
#include <algorithm>
#include <cmath>
static float NormalizeAxis(Sint16 v, int deadzone) {
// Deadzone in raw units. Return [-1..1]
const int iv = (int)v;
if (std::abs(iv) <= deadzone) return 0.0f;
// Normalize to [-1..1] while preserving sign.
// SDL axis range is roughly [-32768..32767]
const float denom = (iv < 0) ? 32768.0f : 32767.0f;
float f = (float)iv / denom;
// Optional: re-scale so that value starts at 0 outside deadzone
// (nice for sticks; comment out if you prefer raw normalize)
const float dz = (float)deadzone / 32767.0f;
const float sign = (f < 0.0f) ? -1.0f : 1.0f;
const float af = std::abs(f);
const float scaled = (af - dz) / (1.0f - dz);
return sign * std::clamp(scaled, 0.0f, 1.0f);
}
void InputManager::Init() {
// Nothing mandatory here. You can also enable relative mouse mode, etc.
// SDL_SetRelativeMouseMode(SDL_TRUE);
// Make sure gamecontroller subsystem is active (safe even if already)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
// Open any controllers already connected
const int num = SDL_NumJoysticks();
for (int i = 0; i < num; ++i) {
if (SDL_IsGameController(i)) {
AddController(i);
}
}
}
void InputManager::BeginFrame() {
@@ -21,74 +51,160 @@ void InputManager::BeginFrame() {
m_mouseDY = m_mouseY - m_prevMouseY;
m_prevMouseX = m_mouseX;
m_prevMouseY = m_mouseY;
// Clear controller "edge" sets + snapshot axes
for (auto& [id, pad]: m_pads) {
pad.buttonsPressed.clear();
pad.buttonsReleased.clear();
pad.prevAxes = pad.axes;
}
}
void InputManager::AddController(int deviceIndex) {
if (!SDL_IsGameController(deviceIndex)) return;
SDL_GameController* gc = SDL_GameControllerOpen(deviceIndex);
if (!gc) return;
SDL_Joystick* joy = SDL_GameControllerGetJoystick(gc);
SDL_JoystickID instanceId = SDL_JoystickInstanceID(joy);
PadState ps;
ps.controller = gc;
ps.instanceId = instanceId;
ps.axes.fill(0);
ps.prevAxes.fill(0);
m_pads[instanceId] = ps;
// Track order (padIndex mapping)
m_padOrder.push_back(instanceId);
}
void InputManager::RemoveController(SDL_JoystickID instanceId) {
auto it = m_pads.find(instanceId);
if (it != m_pads.end()) {
if (it->second.controller) {
SDL_GameControllerClose(it->second.controller);
}
m_pads.erase(it);
}
// Remove from order list
m_padOrder.erase(
std::remove(m_padOrder.begin(), m_padOrder.end(), instanceId),
m_padOrder.end()
);
}
void InputManager::ProcessEvent(const SDL_Event& e) {
switch (e.type) {
case SDL_KEYDOWN: {
if (e.key.repeat) break; // avoid repeat spam for "Pressed"
if (e.key.repeat) break;
SDL_Scancode sc = e.key.keysym.scancode;
m_keysDown.insert(sc);
m_keysPressed.insert(sc);
} break;
}
break;
case SDL_KEYUP: {
SDL_Scancode sc = e.key.keysym.scancode;
m_keysDown.erase(sc);
m_keysReleased.insert(sc);
} break;
}
break;
case SDL_MOUSEBUTTONDOWN: {
Uint8 b = e.button.button;
m_mouseDown.insert(b);
m_mousePressed.insert(b);
} break;
}
break;
case SDL_MOUSEBUTTONUP: {
Uint8 b = e.button.button;
m_mouseDown.erase(b);
m_mouseReleased.insert(b);
} break;
}
break;
case SDL_MOUSEMOTION: {
m_mouseX = e.motion.x;
m_mouseY = e.motion.y;
} break;
}
break;
case SDL_MOUSEWHEEL: {
// Accumulate wheel per frame
m_wheelX += e.wheel.x;
m_wheelY += e.wheel.y;
} break;
}
break;
case SDL_TEXTINPUT: {
// text typed this frame
m_textInput += e.text.text;
} break;
}
break;
// ---- Controller hotplug ----
case SDL_CONTROLLERDEVICEADDED: {
// e.cdevice.which is the device index here
AddController(e.cdevice.which);
}
break;
case SDL_CONTROLLERDEVICEREMOVED: {
// e.cdevice.which is instanceId here
RemoveController((SDL_JoystickID)e.cdevice.which);
}
break;
// ---- Controller buttons ----
case SDL_CONTROLLERBUTTONDOWN: {
SDL_JoystickID id = (SDL_JoystickID)e.cbutton.which;
auto it = m_pads.find(id);
if (it == m_pads.end()) break;
uint8_t btn = (uint8_t)e.cbutton.button;
it->second.buttonsDown.insert(btn);
it->second.buttonsPressed.insert(btn);
}
break;
case SDL_CONTROLLERBUTTONUP: {
SDL_JoystickID id = (SDL_JoystickID)e.cbutton.which;
auto it = m_pads.find(id);
if (it == m_pads.end()) break;
uint8_t btn = (uint8_t)e.cbutton.button;
it->second.buttonsDown.erase(btn);
it->second.buttonsReleased.insert(btn);
}
break;
// ---- Controller axes ----
case SDL_CONTROLLERAXISMOTION: {
SDL_JoystickID id = (SDL_JoystickID)e.caxis.which;
auto it = m_pads.find(id);
if (it == m_pads.end()) break;
const int axis = (int)e.caxis.axis;
if (axis >= 0 && axis < SDL_CONTROLLER_AXIS_MAX) {
it->second.axes[(size_t)axis] = e.caxis.value;
}
}
break;
default: break;
}
}
bool InputManager::IsKeyDown(SDL_Scancode sc) const {
return m_keysDown.find(sc) != m_keysDown.end();
}
bool InputManager::WasKeyPressed(SDL_Scancode sc) const {
return m_keysPressed.find(sc) != m_keysPressed.end();
}
bool InputManager::WasKeyReleased(SDL_Scancode sc) const {
return m_keysReleased.find(sc) != m_keysReleased.end();
}
bool InputManager::IsKeyDown(SDL_Scancode sc) const { return m_keysDown.count(sc) != 0; }
bool InputManager::WasKeyPressed(SDL_Scancode sc) const { return m_keysPressed.count(sc) != 0; }
bool InputManager::WasKeyReleased(SDL_Scancode sc) const { return m_keysReleased.count(sc) != 0; }
bool InputManager::IsMouseDown(Uint8 button) const {
return m_mouseDown.find(button) != m_mouseDown.end();
}
bool InputManager::WasMousePressed(Uint8 button) const {
return m_mousePressed.find(button) != m_mousePressed.end();
}
bool InputManager::WasMouseReleased(Uint8 button) const {
return m_mouseReleased.find(button) != m_mouseReleased.end();
}
bool InputManager::IsMouseDown(Uint8 button) const { return m_mouseDown.count(button) != 0; }
bool InputManager::WasMousePressed(Uint8 button) const { return m_mousePressed.count(button) != 0; }
bool InputManager::WasMouseReleased(Uint8 button) const { return m_mouseReleased.count(button) != 0; }
void InputManager::StartTextInput() {
if (!m_textInputActive) {
@@ -96,6 +212,7 @@ void InputManager::StartTextInput() {
m_textInputActive = true;
}
}
void InputManager::StopTextInput() {
if (m_textInputActive) {
SDL_StopTextInput();
@@ -103,6 +220,56 @@ void InputManager::StopTextInput() {
}
}
// ---- Controller direct queries ----
SDL_JoystickID InputManager::GetPadInstanceId(uint8_t padIndex) const {
if (padIndex >= m_padOrder.size()) return -1;
return m_padOrder[padIndex];
}
bool InputManager::IsPadButtonDown(SDL_JoystickID id, SDL_GameControllerButton btn) const {
auto it = m_pads.find(id);
if (it == m_pads.end()) return false;
return it->second.buttonsDown.count((uint8_t)btn) != 0;
}
bool InputManager::WasPadButtonPressed(SDL_JoystickID id, SDL_GameControllerButton btn) const {
auto it = m_pads.find(id);
if (it == m_pads.end()) return false;
return it->second.buttonsPressed.count((uint8_t)btn) != 0;
}
bool InputManager::WasPadButtonReleased(SDL_JoystickID id, SDL_GameControllerButton btn) const {
auto it = m_pads.find(id);
if (it == m_pads.end()) return false;
return it->second.buttonsReleased.count((uint8_t)btn) != 0;
}
float InputManager::GetPadAxis(SDL_JoystickID id, SDL_GameControllerAxis axis) const {
auto it = m_pads.find(id);
if (it == m_pads.end()) return 0.0f;
const int a = (int)axis;
if (a < 0 || a >= SDL_CONTROLLER_AXIS_MAX) return 0.0f;
return NormalizeAxis(it->second.axes[(size_t)a], m_axisDeadzone);
}
// ---- Binding helpers ----
InputManager::Binding InputManager::PadButton(uint8_t padIndex, SDL_GameControllerButton btn) {
Binding b;
b.device = Device::GamepadButton;
b.code = ((int)padIndex << 8) | ((int)btn & 0xFF);
return b;
}
InputManager::Binding InputManager::PadAxis(uint8_t padIndex, SDL_GameControllerAxis axis, int sign, float threshold) {
Binding b;
b.device = Device::GamepadAxis;
b.code = ((int)padIndex << 8) | ((int)axis & 0xFF);
b.sign = (sign < 0) ? -1 : +1;
b.threshold = threshold;
return b;
}
// ---- Action mapping ----
void InputManager::BindAction(const std::string& action, const Binding& binding) {
m_bindings[action].push_back(binding);
}
@@ -111,28 +278,67 @@ bool InputManager::QueryBinding(const Binding& b, ButtonState state) const {
switch (b.device) {
case Device::Keyboard: {
auto sc = static_cast<SDL_Scancode>(b.code);
if (state == ButtonState::Down) return IsKeyDown(sc);
if (state == ButtonState::Pressed) return WasKeyPressed(sc);
if (state == ButtonState::Down) return IsKeyDown(sc);
if (state == ButtonState::Pressed) return WasKeyPressed(sc);
if (state == ButtonState::Released) return WasKeyReleased(sc);
} break;
}
break;
case Device::MouseButton: {
auto mb = static_cast<Uint8>(b.code);
if (state == ButtonState::Down) return IsMouseDown(mb);
if (state == ButtonState::Pressed) return WasMousePressed(mb);
if (state == ButtonState::Down) return IsMouseDown(mb);
if (state == ButtonState::Pressed) return WasMousePressed(mb);
if (state == ButtonState::Released) return WasMouseReleased(mb);
} break;
}
break;
case Device::MouseWheel: {
// Encoding:
// +1/-1 => wheel Y (up/down), +2/-2 => wheel X (right/left)
const int c = b.code;
if (state != ButtonState::Pressed) return false; // wheel is "impulse"
if (c == 1) return m_wheelY > 0;
if (state != ButtonState::Pressed) return false;
if (c == 1) return m_wheelY > 0;
if (c == -1) return m_wheelY < 0;
if (c == 2) return m_wheelX > 0;
if (c == 2) return m_wheelX > 0;
if (c == -2) return m_wheelX < 0;
} break;
}
break;
case Device::GamepadButton: {
const uint8_t padIndex = GetPadIndexFromCode(b.code);
const uint8_t btn = GetLow8FromCode(b.code);
const SDL_JoystickID id = GetPadInstanceId(padIndex);
if (id < 0) return false;
if (state == ButtonState::Down) return IsPadButtonDown(id, (SDL_GameControllerButton)btn);
if (state == ButtonState::Pressed) return WasPadButtonPressed(id, (SDL_GameControllerButton)btn);
if (state == ButtonState::Released) return WasPadButtonReleased(id, (SDL_GameControllerButton)btn);
}
break;
case Device::GamepadAxis: {
// Axis bindings are treated as "digital" by thresholding
const uint8_t padIndex = GetPadIndexFromCode(b.code);
const uint8_t axis = GetLow8FromCode(b.code);
const SDL_JoystickID id = GetPadInstanceId(padIndex);
if (id < 0) return false;
auto it = m_pads.find(id);
if (it == m_pads.end()) return false;
if (axis >= SDL_CONTROLLER_AXIS_MAX) return false;
const float cur = NormalizeAxis(it->second.axes[axis], m_axisDeadzone) * (float)b.sign;
const float prev = NormalizeAxis(it->second.prevAxes[axis], m_axisDeadzone) * (float)b.sign;
const bool curOn = cur >= b.threshold;
const bool prevOn = prev >= b.threshold;
if (state == ButtonState::Down) return curOn;
if (state == ButtonState::Pressed) return (curOn && !prevOn);
if (state == ButtonState::Released) return (!curOn && prevOn);
}
break;
}
return false;
}
@@ -141,7 +347,7 @@ bool InputManager::GetAction(const std::string& action, ButtonState state) const
auto it = m_bindings.find(action);
if (it == m_bindings.end()) return false;
for (const auto& b : it->second) {
for (const auto& b: it->second) {
if (QueryBinding(b, state)) return true;
}
return false;