summaryrefslogtreecommitdiff
path: root/video.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'video.cpp')
-rw-r--r--video.cpp179
1 files changed, 101 insertions, 78 deletions
diff --git a/video.cpp b/video.cpp
index 9e2ca05..1670a61 100644
--- a/video.cpp
+++ b/video.cpp
@@ -11,9 +11,10 @@
extern "C" {
#include "memory.h"
+#include "pack.h"
#include "plat.h"
-#include "str.h"
#include "sc/sh_enums.h"
+#include "str.h"
}
#include <algorithm>
@@ -337,7 +338,7 @@ struct Shader_Vk : public Shader {
int attr_count;
int binding_count;
- bool init(Device_Vk* dev, FILE* f);
+ bool init(Device_Vk* dev, Pack_File* f);
void destroy(Device_Vk* dev);
int find_binding(const char* name);
@@ -349,7 +350,7 @@ struct Shader_Vk : public Shader {
char entrypoints[shader_type_count][24];
Vertex_Format vfd;
- bool init(Device_Vk* dev, FILE* f);
+ bool init(Device_Vk* dev, Pack_File* f);
bool init_module(
Device_Vk* dev,
int stage,
@@ -596,6 +597,12 @@ struct std::hash<Render_Pass> {
}
};
+struct Shader_Loader : public Asset_Loader {
+ Device_Vk* dev;
+ void init(Device_Vk* d);
+ Asset* load(Arena* a, Pack_File* f) override;
+};
+
struct Device_Vk : public Device {
VkAllocationCallbacks ac;
VkInstance inst;
@@ -611,6 +618,7 @@ struct Device_Vk : public Device {
Swapchain swapchain;
Context_Vk contexts[max_contexts];
Context_Vk* current_ctx;
+ Shader_Loader shader_loader;
#ifdef DEBUG
VkDebugUtilsMessengerEXT msg;
#endif
@@ -971,6 +979,8 @@ void Device_Vk::init_internal() {
shader_count = 1;
rpo_cache.init();
pso_cache.init();
+ shader_loader.init(this);
+ register_asset_loader("CSH2", &shader_loader);
find_exts(exts, ext_count);
init_ac();
create_inst(exts, ext_count);
@@ -1983,52 +1993,6 @@ void Vertex_Format_Vk::destroy(Device_Vk* dev) {
heap_free(dev->heap, bindings);
}
-/* todo proper asset manager which will load this stuff */
-bool Shader_Vk::init(Device_Vk* dev, FILE* f) {
- char magic[4];
- int binding_count, target_count, i;
- fread(magic, 4, 1, f);
- if (
- magic[0] != 'C' ||
- magic[1] != 'S' ||
- magic[2] != 'H' ||
- magic[3] != '2'
- ) return false;
- fread(&type, 4, 1, f);
- fread(&binding_count, 4, 1, f);
- fread(&target_count, 4, 1, f);
- vfd.binding_count = binding_count;
- assert(binding_count);
- if (!vfd.init(dev, f))
- return false;
- vf = dev->create_vf(*this);
- fseek(
- f,
- 32 * target_count,
- SEEK_CUR
- );
- for (i = 0; i < shader_type_count; i++) {
- int o, s;
- fread(&o, 4, 1, f);
- fread(&s, 4, 1, f);
- if (o) {
- bool r;
- int before = ftell(f);
- char* buf = (char*)heap_alloc(dev->heap, s);
- fseek(f, o, SEEK_SET);
- fread(buf, 1, s, f);
- r = init_module(dev, i, buf, s);
- heap_free(dev->heap, buf);
- fseek(f, before, SEEK_SET);
- if (!r) return false;
- } else {
- modules[i] = VK_NULL_HANDLE;
- }
- fread(entrypoints[i], 1, 24, f);
- }
- return true;
-}
-
bool Shader_Vk::init_module(
Device_Vk* dev,
int stage,
@@ -2076,27 +2040,27 @@ int Shader_Vk::Vertex_Format::find_attribute(const char* name) {
bool Shader_Vk::Vertex_Format::init(
Device_Vk* dev,
- FILE* f
+ Pack_File* f
) {
int i, attr_index = 0;
- int start = ftell(f);
+ int start = pack_tell(f);
attr_count = 0;
for (i = 0; i < binding_count; i++) {
char name[24];
int count, j;
SBinding_Rate rate;
- fread(name, 1, sizeof name, f);
- fread(&rate, 4, 1, f);
- fread(&count, 4, 1, f);
+ pack_read(f, name, sizeof name);
+ pack_read(f, &rate, 4);
+ pack_read(f, &count, 4);
for (j = 0; j < count; j++) {
char aname[28];
SVariable_Type type;
- fread(aname, 1, sizeof aname, f);
- fread(&type, 4, 1, f);
+ pack_read(f, aname, sizeof aname);
+ pack_read(f, &type, 4);
attr_count++;
}
}
- fseek(f, start, SEEK_SET);
+ pack_seek(f, start, seek_rel_start);
bindings = (Binding*)heap_alloc(
dev->heap,
binding_count * sizeof *bindings
@@ -2114,9 +2078,9 @@ bool Shader_Vk::Vertex_Format::init(
char name[24];
int count, j;
SBinding_Rate rate;
- fread(name, 1, sizeof name, f);
- fread(&rate, 4, 1, f);
- fread(&count, 4, 1, f);
+ pack_read(f, name, sizeof name);
+ pack_read(f, &rate, 4);
+ pack_read(f, &count, 4);
binding = &bindings[find_binding(name)];
strcpy(binding->name, name);
binding->rate = rate;
@@ -2131,8 +2095,8 @@ bool Shader_Vk::Vertex_Format::init(
Attribute* attr;
char aname[28];
SVariable_Type type;
- fread(aname, 1, sizeof aname, f);
- fread(&type, 4, 1, f);
+ pack_read(f, aname, sizeof aname);
+ pack_read(f, &type, 4);
bucket = find_attribute(aname);
binding->attributes[j] = bucket;
attr = &attributes[bucket];
@@ -2276,27 +2240,86 @@ Shader& Device::get_shader(Shader_Id id) {
return ((Device_Vk*)this)->shaders[id];
}
-Shader_Id Device::load_shader(const char* fname) {
- FILE* f = fopen(fname, "rb");
+bool Shader::load(
+ Asset_Loader* loader,
+ Arena* a,
+ Pack_File* f
+) {
+ Device_Vk* dev = ((Shader_Loader*)loader)->dev;
+ Shader_Vk& sh = *(Shader_Vk*)this;
+ return sh.init(dev, f);
+}
+
+void Shader::unload(Asset_Loader* loader_) {
+ Shader_Loader* loader = (Shader_Loader*)loader_;
+ Device_Vk* dev = loader->dev;
+ Shader_Vk& sh = *(Shader_Vk*)this;
+ sh.destroy(dev);
+ dev->shaders.remove(id);
+}
+
+void Shader_Loader::init(Device_Vk* d) {
+ dev = d;
+}
+
+Asset* Shader_Loader::load(
+ Arena* a,
+ Pack_File* f
+) {
Shader_Vk* s;
- Device_Vk* dev = (Device_Vk*)this;
Shader_Id id;
- bool r;
- if (!f) return Shader_Id(0);
id = dev->alloc_shader();
- s = (Shader_Vk*)&get_shader(id);
- r = s->init(dev, f);
- fclose(f);
- if (!r) {
- heap_free(heap, s);
+ s = (Shader_Vk*)&dev->get_shader(id);
+ s->id = id;
+ if (!s->load(this, a, f)) {
+ dev->shaders.remove(id);
return 0;
}
- return id;
+ return s;
}
-void Device::destroy_shader(Shader_Id id) {
- Device_Vk* dev = (Device_Vk*)this;
- Shader_Vk& buf = *(Shader_Vk*)&get_shader(id);
- buf.destroy(dev);
- dev->shaders.remove(id);
+bool Shader_Vk::init(Device_Vk* dev, Pack_File* f) {
+ char magic[4];
+ int binding_count, target_count, i;
+ pack_read(f, magic, 4);
+ if (
+ magic[0] != 'C' ||
+ magic[1] != 'S' ||
+ magic[2] != 'H' ||
+ magic[3] != '2'
+ ) return false;
+ pack_read(f, &type, 4);
+ pack_read(f, &binding_count, 4);
+ pack_read(f, &target_count, 4);
+ vfd.binding_count = binding_count;
+ assert(binding_count);
+ if (!vfd.init(dev, f))
+ return false;
+ vf = dev->create_vf(*this);
+ pack_seek(
+ f,
+ 32 * target_count,
+ seek_rel_cur
+ );
+ for (i = 0; i < shader_type_count; i++) {
+ int o, s;
+ pack_read(f, &o, 4);
+ pack_read(f, &s, 4);
+ if (o) {
+ bool r;
+ int before = pack_tell(f);
+ char* buf = (char*)heap_alloc(dev->heap, s);
+ pack_seek(f, o, seek_rel_start);
+ pack_read(f, buf, s);
+ r = init_module(dev, i, buf, s);
+ heap_free(dev->heap, buf);
+ pack_seek(f, before, seek_rel_start);
+ if (!r) return false;
+ } else {
+ modules[i] = VK_NULL_HANDLE;
+ }
+ pack_read(f, entrypoints[i], 24);
+ }
+ return true;
}
+