Pārlūkot izejas kodu

Set up code examples for Vulkan and libpng

master
Brian Ho pirms 5 gadiem
revīzija
a6222e0080
3 mainītis faili ar 113 papildinājumiem un 0 dzēšanām
  1. 2
    0
      .gitignore
  2. 55
    0
      Makefile
  3. 56
    0
      triangle.cc

+ 2
- 0
.gitignore Parādīt failu

@@ -0,0 +1,2 @@
triangle
triangle.png

+ 55
- 0
Makefile Parādīt failu

@@ -0,0 +1,55 @@
ifeq ($(target), cheza)
CXX = /usr/bin/armv7a-cros-linux-gnueabihf-clang++
CC = /usr/bin/armv7a-cros-linux-gnueabihf-clang
SYSROOT = /build/cheza
SSH_DUT = cheza
else ifeq ($(target), atlas)
CXX = /usr/bin/x86_64-cros-linux-gnu-clang++
CC = /usr/bin/x86_64-cros-linux-gnu-clang
SYSROOT = /build/atlas
SSH_DUT = atlas
else ifeq ($(target), local)
CXX = clang++
CC = clang
else
CXX = INVALID
CC = INVALID
endif

NAME = triangle
CFLAGS = -std=c++17 --sysroot="$(SYSROOT)" -Wall
LDFLAGS = -lvulkan -lpng

all: build deploy run

build: check
@echo Building...
@$(CXX) $(CFLAGS) -o ${NAME} ${NAME}.cc $(LDFLAGS)

deploy: check
ifneq ($(target), local)
@echo Deploying to $(SSH_DUT)...
@scp ${NAME} $(SSH_DUT):~/
endif

run: check
ifneq ($(target), local)
@echo Running on $(SSH_DUT)...
@ssh $(SSH_DUT) '~/${NAME}'
@scp $(SSH_DUT):~/${NAME}.png .
else
@echo Running locally...
@./$(NAME)
endif

clean: check
@rm -f ${NAME}
@rm -f ${NAME}.png
ifneq ($(target), local)
@ssh $(SSH_DUT) 'rm -f ~/${NAME} ~/${NAME}.png'
endif

check:
ifeq ($(CXX), INVALID)
$(error $$target must be one of [atlas, cheza, local] )
endif

+ 56
- 0
triangle.cc Parādīt failu

@@ -0,0 +1,56 @@
#include <png.h>
#include <vulkan/vulkan.h>

#include <iostream>
#include <vector>

const uint32_t kWidth = 16;
const uint32_t kHeight = 16;

int main() {
// Example code for creating a VkInstance.
VkApplicationInfo app_info = {};
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
app_info.apiVersion = VK_API_VERSION_1_1;

VkInstanceCreateInfo create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
create_info.pApplicationInfo = &app_info;
create_info.enabledExtensionCount = 0;
create_info.ppEnabledExtensionNames = nullptr;
create_info.enabledLayerCount = 0;
create_info.ppEnabledLayerNames = nullptr;

VkInstance instance;
VkResult res = vkCreateInstance(&create_info, nullptr, &instance);
if (res != VK_SUCCESS) {
std::cerr << "Error creating VkInstance: " << res;
}

// Example code for exporting a PNG.
FILE *f = fopen("triangle.png", "wb");

std::vector<uint8_t> pixels(kWidth * kHeight * 4);
for (size_t i = 0; i < kHeight; i++) {
for (size_t j = 0; j < kWidth; j++) {
uint32_t base_index = (i * kWidth * 4) + (j * 4);
pixels[base_index] = 255;
pixels[base_index + 1] = 255;
pixels[base_index + 2] = 0;
pixels[base_index + 3] = 255;
}
}

png_image image_info = {};
image_info.version = PNG_IMAGE_VERSION;
image_info.width = kWidth;
image_info.height = kHeight;
image_info.format = PNG_FORMAT_RGBA;
if (png_image_write_to_stdio(&image_info, f, 0, pixels.data(), kWidth * 4, nullptr) == 0) {
std::cout << "Error writing PNG: " << image_info.message << std::endl;
}

fclose(f);

return 0;
}

Notiek ielāde…
Atcelt
Saglabāt