summaryrefslogtreecommitdiff
path: root/src/ral/ral_common.c
blob: 53e35f0d02b426f6048d1055c5e773f1541c65ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ral_common.h"
#include "ral_impl.h"

void BackendPools_Init(arena* a, GPU_BackendPools* backend_pools) {
  PipelineLayout_pool pipeline_layout_pool =
      PipelineLayout_pool_create(a, MAX_PIPELINES, sizeof(GPU_PipelineLayout));
  backend_pools->pipeline_layouts = pipeline_layout_pool;
  Pipeline_pool pipeline_pool = Pipeline_pool_create(a, MAX_PIPELINES, sizeof(GPU_Pipeline));
  backend_pools->pipelines = pipeline_pool;
  Renderpass_pool rpass_pool = Renderpass_pool_create(a, MAX_RENDERPASSES, sizeof(GPU_Renderpass));
  backend_pools->renderpasses = rpass_pool;
}

void ResourcePools_Init(arena* a, struct ResourcePools* res_pools) {
  Buffer_pool buf_pool = Buffer_pool_create(a, MAX_BUFFERS, sizeof(GPU_Buffer));
  res_pools->buffers = buf_pool;
  Texture_pool tex_pool = Texture_pool_create(a, MAX_TEXTURES, sizeof(GPU_Texture));
  res_pools->textures = tex_pool;
}

VertexDescription static_3d_vertex_description() {
  VertexDescription builder = { .debug_label = "Standard static 3d vertex format" };
  VertexDesc_AddAttr(&builder, "inPosition", ATTR_F32x3);
  VertexDesc_AddAttr(&builder, "inNormal", ATTR_F32x3);
  VertexDesc_AddAttr(&builder, "inTexCoords", ATTR_F32x2);
  builder.use_full_vertex_size = true;
  return builder;
}

void VertexDesc_AddAttr(VertexDescription* builder, const char* name, VertexAttribType type) {
  u32 i = builder->attributes_count;

  size_t size = VertexAttribSize(type);
  builder->attributes[i] = type;
  // builder->stride += size;
  builder->attr_names[i] = name;

  builder->attributes_count++;
}

size_t VertexAttribSize(VertexAttribType attr) {
  switch (attr) {
    case ATTR_F32:
    case ATTR_U32:
    case ATTR_I32:
      return 4;
    case ATTR_F32x2:
    case ATTR_U32x2:
    case ATTR_I32x2:
      return 8;
    case ATTR_F32x3:
    case ATTR_U32x3:
    case ATTR_I32x3:
      return 12;
    case ATTR_F32x4:
    case ATTR_U32x4:
    case ATTR_I32x4:
      return 16;
      break;
  }
}

size_t VertexDesc_CalcStride(VertexDescription* desc) {
    size_t stride = 0;
    for (int i = 0; i < desc->attributes_count; i++) {
        size_t size =VertexAttribSize(desc->attributes[i]);
        stride += size;
    }
    return stride;
}