Advertisement
Guest User

TestRenderer.cpp

a guest
Feb 22nd, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "DumbRenderer.hpp"
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #include "stb/stb_image.h"
  7.  
  8. VkResult CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugReportCallbackEXT* pCallback) {
  9.     auto func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT");
  10.     if (func != nullptr) {
  11.         return func(instance, pCreateInfo, pAllocator, pCallback);
  12.     }
  13.     else {
  14.         return VK_ERROR_EXTENSION_NOT_PRESENT;
  15.     }
  16. }
  17.  
  18. void DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks* pAllocator) {
  19.     auto func = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT");
  20.     if (func != nullptr) {
  21.         func(instance, callback, pAllocator);
  22.     }
  23. }
  24.  
  25. void GlfwErrorCallback(int code, const char* msg) {
  26.     std::cerr << "GFLW Error: code " << code << ": " << msg << "\n";
  27. }
  28.  
  29. BinaryFileData ReadBinaryFile(std::string_view name) {
  30.     std::ifstream file(name.data(), std::ios::ate | std::ios::binary);
  31.  
  32.     if (!file.is_open()) {
  33.         std::stringstream ss;
  34.         ss << "Failed to open binary file: " << name << "\n";
  35.         throw std::runtime_error(ss.str());
  36.     }
  37.  
  38.     size_t fileSize = (size_t)file.tellg();
  39.     std::vector<char> buffer(fileSize);
  40.  
  41.     file.seekg(0);
  42.     file.read(buffer.data(), fileSize);
  43.     file.close();
  44.  
  45.     return buffer;
  46. }
  47.  
  48.  
  49. void TestRenderer::CreateTextureImage() {
  50.     int texWidth, texHeight, texChannels;
  51.     stbi_uc* pixels = stbi_load("texture.png", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
  52.     VkDeviceSize imageSize = texWidth * texHeight * 4;
  53.  
  54.     if (!pixels) {
  55.         throw std::runtime_error("failed to load texture image!");
  56.     }
  57.  
  58.     VkBuffer stagingBuffer;
  59.     VkDeviceMemory stagingBufferMemory;
  60.     CreateBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
  61.  
  62.     void* data;
  63.     vkMapMemory(mLogicalDevice, stagingBufferMemory, 0, imageSize, 0, &data);
  64.     memcpy(data, pixels, static_cast<size_t>(imageSize));
  65.     vkUnmapMemory(mLogicalDevice, stagingBufferMemory);
  66.  
  67.     stbi_image_free(pixels);
  68.  
  69.     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);
  70.  
  71.     TransitionImageLayout(mTextureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
  72.     CopyBufferToImage(stagingBuffer, mTextureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
  73.  
  74.     TransitionImageLayout(mTextureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
  75.  
  76.     vkDestroyBuffer(mLogicalDevice, stagingBuffer, nullptr);
  77.     vkFreeMemory(mLogicalDevice, stagingBufferMemory, nullptr);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement