ref: 2456c5671629b47378665485801e73aea7234856
parent: a66be92f63ec59c1811403eb7b95fa72588beee6
author: sirjofri <[email protected]>
date: Fri Feb 16 12:29:26 EST 2024
adds missing objects.h, adds backend descriptor pools, sets, uniform binding, shader binding (untestet)
--- /dev/null
+++ b/objects.h
@@ -1,0 +1,24 @@
+typedef struct Buffer Buffer;
+struct Buffer {
+ char *buffer;
+ long len;
+};
+
+typedef struct Shader Shader;
+struct Shader {
+ u32int *buffer;
+ long len;
+ int descpool;
+};
+
+typedef struct DescSet DescSet;
+struct DescSet {
+ vlong *bindings; // id of buffer
+ int numbindings;
+};
+
+typedef struct DescPool DescPool;
+struct DescPool {
+ DescSet *sets;
+ int numsets;
+};
--- a/vm.c
+++ b/vm.c
@@ -85,6 +85,9 @@
Object objects[256];
int numobjects = 0;
+DescPool descpools[8];
+int numdescpools = 0;
+
vlong
genshader(void)
{
@@ -292,4 +295,89 @@
buf = &objects[id].b.buffer[offset];
memcpy(data, buf, n);
return n;
+}
+
+int
+gendescpool(int numsets)
+{
+ int id;
+ if (numdescpools >= 8) {
+ werrstr("not enough pools reserved");
+ return -1;
+ }
+
+ id = numdescpools++;
+ descpools[id].sets = malloc(sizeof(DescSet)*numsets);
+ if (!descpools[id].sets) {
+ sysfatal("out of memory");
+ return -1;
+ }
+ descpools[id].numsets = numsets;
+ memset(descpools[id].sets, 0, sizeof(DescSet)*numsets);
+
+ return id;
+}
+
+int
+allocdescset(int pool, int set, int numbindings)
+{
+ if (pool >= numdescpools) {
+ werrstr("invalid pool id %d", pool);
+ return 0;
+ }
+
+ if (set >= descpools[pool].numsets) {
+ werrstr("invalid set id %d", pool);
+ return 0;
+ }
+
+ descpools[pool].sets[set].bindings = malloc(sizeof(vlong)*numbindings);
+ if (!descpools[pool].sets[set].bindings) {
+ sysfatal("out of memory");
+ return 0;
+ }
+ for (int i = 0; i < numbindings; i++) {
+ descpools[pool].sets[set].bindings[i] = -1;
+ }
+ descpools[pool].sets[set].numbindings = numbindings;
+ return 1;
+}
+
+int
+binduniform(vlong id, int pool, int set, int binding)
+{
+ if (pool >= numdescpools) {
+ werrstr("invalid pool id %d", pool);
+ return 0;
+ }
+
+ if (set >= descpools[pool].numsets) {
+ werrstr("pool has not enough sets");
+ return 0;
+ }
+
+ if (binding >= descpools[pool].sets[set].numbindings) {
+ werrstr("set has not enough bindings");
+ return 0;
+ }
+
+ descpools[pool].sets[set].bindings[binding] = id;
+ return 1;
+}
+
+int
+bindshader(vlong id, int pool)
+{
+ if (!IDVALID(id)) {
+ werrstr("invalid id %lld", id);
+ return 0;
+ }
+
+ if (objects[id].type != SHADER) {
+ werrstr("invalid object type: %lld != SHADER", id);
+ return 0;
+ }
+
+ objects[id].s.descpool = pool;
+ return 1;
}
--- a/vm.h
+++ b/vm.h
@@ -25,3 +25,9 @@
// length data written
int readshader(vlong id, void *data, long n, long offset);
int readbuffer(vlong id, void *data, long n, long offset);
+
+// descriptor sets
+int gendescpool(int numsets);
+int allocdescset(int pool, int set, int numbindings);
+int binduniform(vlong id, int pool, int set, int binding);
+int bindshader(vlong id, int pool);