Binary that renders a triangle using Vulkan and scans the resulting image to a PNG. To be used in developing Turnip.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

triangle.cc 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <png.h>
  2. #include <vulkan/vulkan.h>
  3. #include <iostream>
  4. #include <vector>
  5. const uint32_t kWidth = 16;
  6. const uint32_t kHeight = 16;
  7. int main() {
  8. // Example code for creating a VkInstance.
  9. VkApplicationInfo app_info = {};
  10. app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  11. app_info.apiVersion = VK_API_VERSION_1_1;
  12. VkInstanceCreateInfo create_info = {};
  13. create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  14. create_info.pApplicationInfo = &app_info;
  15. create_info.enabledExtensionCount = 0;
  16. create_info.ppEnabledExtensionNames = nullptr;
  17. create_info.enabledLayerCount = 0;
  18. create_info.ppEnabledLayerNames = nullptr;
  19. VkInstance instance;
  20. VkResult res = vkCreateInstance(&create_info, nullptr, &instance);
  21. if (res != VK_SUCCESS) {
  22. std::cerr << "Error creating VkInstance: " << res;
  23. }
  24. // Example code for exporting a PNG.
  25. FILE *f = fopen("triangle.png", "wb");
  26. std::vector<uint8_t> pixels(kWidth * kHeight * 4);
  27. for (size_t i = 0; i < kHeight; i++) {
  28. for (size_t j = 0; j < kWidth; j++) {
  29. uint32_t base_index = (i * kWidth * 4) + (j * 4);
  30. pixels[base_index] = 255;
  31. pixels[base_index + 1] = 255;
  32. pixels[base_index + 2] = 0;
  33. pixels[base_index + 3] = 255;
  34. }
  35. }
  36. png_image image_info = {};
  37. image_info.version = PNG_IMAGE_VERSION;
  38. image_info.width = kWidth;
  39. image_info.height = kHeight;
  40. image_info.format = PNG_FORMAT_RGBA;
  41. if (png_image_write_to_stdio(&image_info, f, 0, pixels.data(), kWidth * 4, nullptr) == 0) {
  42. std::cout << "Error writing PNG: " << image_info.message << std::endl;
  43. }
  44. fclose(f);
  45. return 0;
  46. }