diff options
Diffstat (limited to 'src/osd/modules/render/bgfx/shadermanager.cpp')
-rw-r--r-- | src/osd/modules/render/bgfx/shadermanager.cpp | 90 |
1 files changed, 64 insertions, 26 deletions
diff --git a/src/osd/modules/render/bgfx/shadermanager.cpp b/src/osd/modules/render/bgfx/shadermanager.cpp index 7583b58dec5..282029cb96a 100644 --- a/src/osd/modules/render/bgfx/shadermanager.cpp +++ b/src/osd/modules/render/bgfx/shadermanager.cpp @@ -9,15 +9,17 @@ // //============================================================ -#include <bx/math.h> -#include <bx/readerwriter.h> -#include <bx/file.h> +#include "shadermanager.h" -#include "emu.h" +#include "emucore.h" -#include <bgfx/bgfx.h> +#include "osdfile.h" +#include "modules/lib/osdobj_common.h" + +#include <bx/file.h> +#include <bx/math.h> +#include <bx/readerwriter.h> -#include "shadermanager.h" shader_manager::~shader_manager() { @@ -28,7 +30,7 @@ shader_manager::~shader_manager() m_shaders.clear(); } -bgfx::ShaderHandle shader_manager::shader(std::string name) +bgfx::ShaderHandle shader_manager::get_or_load_shader(const osd_options &options, const std::string &name) { std::map<std::string, bgfx::ShaderHandle>::iterator iter = m_shaders.find(name); if (iter != m_shaders.end()) @@ -36,13 +38,60 @@ bgfx::ShaderHandle shader_manager::shader(std::string name) return iter->second; } - return load_shader(name); + bgfx::ShaderHandle handle = load_shader(options, name); + if (handle.idx != bgfx::kInvalidHandle) + { + m_shaders[name] = handle; + } + + return handle; +} + +bgfx::ShaderHandle shader_manager::load_shader(const osd_options &options, const std::string &name) +{ + std::string shader_path = make_path_string(options, name); + const bgfx::Memory* mem = load_mem(shader_path + name + ".bin"); + if (mem != nullptr) + { + return bgfx::createShader(mem); + } + + return BGFX_INVALID_HANDLE; } -bgfx::ShaderHandle shader_manager::load_shader(std::string name) +bool shader_manager::is_shader_present(const osd_options &options, const std::string &name) { - std::string shader_path(m_options.bgfx_path()); + std::string shader_path = make_path_string(options, name); + std::string file_name = shader_path + name + ".bin"; + bx::FileReader reader; + bx::ErrorAssert err; + if (bx::open(&reader, file_name.c_str())) + { + uint32_t expected_size(bx::getSize(&reader)); + uint8_t *data = new uint8_t[expected_size]; + uint32_t read_size = (uint32_t)bx::read(&reader, data, expected_size, &err); + delete [] data; + bx::close(&reader); + + return expected_size == read_size; + } + + return false; +} + +std::string shader_manager::make_path_string(const osd_options &options, const std::string &name) +{ + std::string shader_path(options.bgfx_path()); shader_path += PATH_SEPARATOR "shaders" PATH_SEPARATOR; + +#if defined(SDLMAME_EMSCRIPTEN) + // Hard-code renderer type to OpenGL ES for emscripten builds since the + // bgfx::getRendererType() is called here before BGFX has been + // initialized and therefore gives the wrong renderer type (Noop). + shader_path += "essl" PATH_SEPARATOR; + return shader_path; +#endif + switch (bgfx::getRendererType()) { case bgfx::RendererType::Noop: @@ -79,29 +128,18 @@ bgfx::ShaderHandle shader_manager::load_shader(std::string name) fatalerror("Unknown BGFX renderer type %d", bgfx::getRendererType()); } shader_path += PATH_SEPARATOR; - osd_subst_env(shader_path, shader_path); - - const bgfx::Memory* mem = load_mem(shader_path + name + ".bin"); - if (mem != nullptr) - { - bgfx::ShaderHandle handle = bgfx::createShader(mem); - - m_shaders[name] = handle; - - return handle; - } - - return BGFX_INVALID_HANDLE; + return shader_path; } -const bgfx::Memory* shader_manager::load_mem(std::string name) +const bgfx::Memory* shader_manager::load_mem(const std::string &name) { bx::FileReader reader; if (bx::open(&reader, name.c_str())) { + bx::ErrorAssert err; uint32_t size(bx::getSize(&reader)); const bgfx::Memory* mem = bgfx::alloc(size + 1); - bx::read(&reader, mem->data, size); + bx::read(&reader, mem->data, size, &err); bx::close(&reader); mem->data[mem->size - 1] = '\0'; @@ -109,7 +147,7 @@ const bgfx::Memory* shader_manager::load_mem(std::string name) } else { - printf("Unable to load shader %s\n", name.c_str()); + osd_printf_error("Unable to load shader %s\n", name); } return nullptr; } |