src/helpers.c
  1#include "alba.h"
  2#include "internal.h"
  3
  4#include <stdio.h>
  5
  6void alba_color_print(const AlbaColor* color)
  7{
  8    printf("AlbaColor{%f, %f, %f, %f}\n", color->r, color->g, color->b, color->a);
  9}
 10
 11int alba_color_is_transparent(const AlbaColor* color)
 12{
 13    return color->r == 0 && color->g == 0 && color->b == 0 && color->a == 0;
 14}
 15
 16void alba_vector_print(const AlbaVector* vector)
 17{
 18    printf("AlbaVector{%f, %f}\n", vector->x, vector->y);
 19}
 20
 21void alba_attribute_print(const AlbaAttribute* attribute)
 22{
 23    printf("AlbaAttribute{\n    ");
 24    alba_color_print(&attribute->color);
 25    printf("    ");
 26    alba_vector_print(&attribute->uv);
 27    printf("}\n");
 28}
 29
 30WGPUBuffer create_buffer(
 31    const AlbaWindow* window,
 32    const uint64_t size, const void* data,
 33    const WGPUBufferUsageFlags flags
 34)
 35{
 36    WGPUBufferDescriptor buffer_options = {0};
 37    buffer_options.usage = WGPUBufferUsage_CopyDst | flags;
 38    buffer_options.size = size;
 39    const WGPUBuffer buffer = wgpuDeviceCreateBuffer(window->device, &buffer_options);
 40    wgpuQueueWriteBuffer(window->queue, buffer, 0, data, size);
 41    return buffer;
 42}
 43
 44void release_buffer(const WGPUBuffer buffer)
 45{
 46    if (buffer != NULL)
 47    {
 48        wgpuBufferDestroy(buffer);
 49        wgpuBufferRelease(buffer);
 50    }
 51}
 52
 53WGPUBuffer update_buffer(
 54    const AlbaWindow* window, WGPUBuffer buffer, const WGPUBufferUsage usage, AlbaArray data
 55)
 56{
 57    // Deallocate old buffer if any
 58    release_buffer(buffer);
 59    // Copy data to new buffer
 60    buffer = create_buffer(window, data.length * data.element_size, data.data, usage);
 61    // Clear local copy for next frame
 62    alba_array_clear(&data);
 63    return buffer;
 64}
 65
 66WGPUTexture create_texture(
 67    const AlbaWindow* window,
 68    const WGPUTextureUsage usage,
 69    const AlbaVector size,
 70    const WGPUTextureFormat format, const uint8_t samples,
 71    const void* data
 72)
 73{
 74    WGPUTextureDescriptor texture_options = {0};
 75    texture_options.usage = usage;
 76    texture_options.format = format;
 77    texture_options.dimension = WGPUTextureDimension_2D;
 78    texture_options.size.width = size.x;
 79    texture_options.size.height = size.y;
 80    texture_options.size.depthOrArrayLayers = 1;
 81    texture_options.sampleCount = samples;
 82    texture_options.mipLevelCount = 1;
 83
 84    const WGPUTexture texture = wgpuDeviceCreateTexture(window->device, &texture_options);
 85
 86    if (data != NULL)
 87    {
 88        const uint64_t bpp = format == WGPUTextureFormat_R8Unorm ? 1 : 4;
 89
 90        WGPUImageCopyTexture destination = {0};
 91        destination.texture = texture;
 92        destination.aspect = WGPUTextureAspect_All;
 93
 94        WGPUTextureDataLayout source = {0};
 95        source.bytesPerRow = size.x * bpp;
 96        source.rowsPerImage = size.y;
 97
 98        wgpuQueueWriteTexture(
 99            window->queue,
100            &destination, data,
101            size.x * size.y * bpp,
102            &source,
103            &texture_options.size
104        );
105    }
106
107    return texture;
108}
109
110void release_texture(const WGPUTexture texture)
111{
112    wgpuTextureDestroy(texture);
113    wgpuTextureRelease(texture);
114}