Added file utilities, began work on texture loading.
After that, it will be time to begin graphics pipeline construction
This commit is contained in:
parent
b1bb57ac96
commit
952176f4c9
@ -1,29 +1,20 @@
|
||||
#include <Renderer/Shader.hpp>
|
||||
|
||||
#include <util/log.hpp>
|
||||
#include <util/file.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
static std::string slurp(const std::string& fname) {
|
||||
std::ifstream in(fname, std::ifstream::binary | std::ifstream::in);
|
||||
std::stringstream sstr;
|
||||
sstr << in.rdbuf();
|
||||
in.close();
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
Shader::Shader(vk::Device dev, const std::string& fname) : fname(fname) {
|
||||
std::string src;
|
||||
std::vector<uint8_t> src;
|
||||
try {
|
||||
src = slurp(fname);
|
||||
src = file::slurpb(fname);
|
||||
} catch (std::exception& e) {
|
||||
Log::error("Failed to read file " + fname + ": "+ e.what() + "\n");
|
||||
}
|
||||
|
||||
auto module_info = vk::ShaderModuleCreateInfo{
|
||||
.codeSize = src.size(),
|
||||
.pCode = reinterpret_cast<const uint32_t*>(src.c_str()),
|
||||
.pCode = reinterpret_cast<const uint32_t*>(src.data()),
|
||||
};
|
||||
|
||||
if (!(module = dev.createShaderModule(module_info))) {
|
||||
|
||||
46
Resources/Texture.cpp
Normal file
46
Resources/Texture.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include <Resources/Texture.hpp>
|
||||
#include <Memory/Memory.hpp>
|
||||
|
||||
#include <util/file.hpp>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
|
||||
Texture::Texture(vk::Device dev, vk::PhysicalDevice phys_dev, const std::string& fname) {
|
||||
vk::Extent3D extent;
|
||||
auto image_data = stbi_load(fname.c_str(), reinterpret_cast<int*>(&extent.width), reinterpret_cast<int*>(&extent.height), NULL, NULL);
|
||||
extent.depth = 1;
|
||||
|
||||
auto image_info = vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = vk::Format::eR8G8B8A8Unorm,
|
||||
.extent = extent,
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eSampled,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
};
|
||||
|
||||
image = dev.createImage(image_info);
|
||||
|
||||
auto reqs = dev.getImageMemoryRequirements(image);
|
||||
|
||||
|
||||
auto image_alloc_info = vk::MemoryAllocateInfo{
|
||||
.allocationSize = reqs.size,
|
||||
.memoryTypeIndex = mem::choose_heap(phys_dev, reqs, vk::MemoryPropertyFlagBits::eHostVisible),
|
||||
};
|
||||
|
||||
image_alloc = dev.allocateMemory(image_alloc_info);
|
||||
dev.bindImageMemory(image, image_alloc, 0);
|
||||
|
||||
/* TODO: Copy memory into image using buffers */
|
||||
}
|
||||
|
||||
void Texture::cleanup(vk::Device dev) {
|
||||
dev.freeMemory(image_alloc);
|
||||
}
|
||||
11
Resources/Texture.hpp
Normal file
11
Resources/Texture.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#define VULKAN_HPP_NO_STRUCT_CONSTRUCTORS
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
struct Texture {
|
||||
vk::Image image;
|
||||
vk::DeviceMemory image_alloc;
|
||||
Texture(vk::Device dev, vk::PhysicalDevice phys_dev, const std::string& fname);
|
||||
void cleanup(vk::Device dev);
|
||||
};
|
||||
7992
include/stb_image.h
Normal file
7992
include/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
24
util/file.hpp
Normal file
24
util/file.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace file {
|
||||
static std::vector<uint8_t> slurpb(const std::string& fname) {
|
||||
std::ifstream in(fname, std::ifstream::binary | std::ifstream::in | std::ifstream::ate);
|
||||
auto sz = in.tellg();
|
||||
in.seekg(0, std::fstream::beg);
|
||||
std::vector<uint8_t> ret(sz);
|
||||
in.read(reinterpret_cast<char*>(ret.data()), sz);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static std::string slurp(const std::string& fname) {
|
||||
std::ifstream in(fname, std::ifstream::in);
|
||||
std::stringstream sstr;
|
||||
sstr << in.rdbuf();
|
||||
in.close();
|
||||
return sstr.str();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user