Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DumbRenderer.hpp"
- #include <fstream>
- #include <sstream>
- #define STB_IMAGE_IMPLEMENTATION
- #include "stb/stb_image.h"
- VkResult CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugReportCallbackEXT* pCallback) {
- auto func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT");
- if (func != nullptr) {
- return func(instance, pCreateInfo, pAllocator, pCallback);
- }
- else {
- return VK_ERROR_EXTENSION_NOT_PRESENT;
- }
- }
- void DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks* pAllocator) {
- auto func = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT");
- if (func != nullptr) {
- func(instance, callback, pAllocator);
- }
- }
- void GlfwErrorCallback(int code, const char* msg) {
- std::cerr << "GFLW Error: code " << code << ": " << msg << "\n";
- }
- BinaryFileData ReadBinaryFile(std::string_view name) {
- std::ifstream file(name.data(), std::ios::ate | std::ios::binary);
- if (!file.is_open()) {
- std::stringstream ss;
- ss << "Failed to open binary file: " << name << "\n";
- throw std::runtime_error(ss.str());
- }
- size_t fileSize = (size_t)file.tellg();
- std::vector<char> buffer(fileSize);
- file.seekg(0);
- file.read(buffer.data(), fileSize);
- file.close();
- return buffer;
- }
- void TestRenderer::CreateTextureImage() {
- int texWidth, texHeight, texChannels;
- stbi_uc* pixels = stbi_load("texture.png", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
- VkDeviceSize imageSize = texWidth * texHeight * 4;
- if (!pixels) {
- throw std::runtime_error("failed to load texture image!");
- }
- VkBuffer stagingBuffer;
- VkDeviceMemory stagingBufferMemory;
- CreateBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
- void* data;
- vkMapMemory(mLogicalDevice, stagingBufferMemory, 0, imageSize, 0, &data);
- memcpy(data, pixels, static_cast<size_t>(imageSize));
- vkUnmapMemory(mLogicalDevice, stagingBufferMemory);
- stbi_image_free(pixels);
- CreateImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, mTextureImage, mTextureImageMemory);
- TransitionImageLayout(mTextureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
- CopyBufferToImage(stagingBuffer, mTextureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
- TransitionImageLayout(mTextureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
- vkDestroyBuffer(mLogicalDevice, stagingBuffer, nullptr);
- vkFreeMemory(mLogicalDevice, stagingBufferMemory, nullptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement