7 #include <bx/readerwriter.h>
11 static const bgfx::Memory* loadMem(bx::FileReaderI* _reader,
const char* _filePath)
13 if (bx::open(_reader, _filePath))
15 uint32_t size = (uint32_t)bx::getSize(_reader);
16 const bgfx::Memory* mem = bgfx::alloc(size + 1);
17 bx::read(_reader, mem->data, static_cast<int32_t>(size));
19 mem->data[mem->size - 1] =
'\0';
23 bx::debugPrintf(
"Failed to load %s.\n", _filePath);
27 static bgfx::ShaderHandle loadShader(bx::FileReaderI* _reader,
const char* _name)
31 const char* shaderPath =
"???";
33 switch (bgfx::getRendererType())
35 case bgfx::RendererType::Noop:
36 case bgfx::RendererType::Direct3D9: shaderPath =
"shaders/dx9/";
break;
37 case bgfx::RendererType::Direct3D11:
38 case bgfx::RendererType::Direct3D12: shaderPath =
"shaders/dx11/";
break;
39 case bgfx::RendererType::Gnm: shaderPath =
"shaders/pssl/";
break;
40 case bgfx::RendererType::Metal: shaderPath =
"shaders/metal/";
break;
41 case bgfx::RendererType::OpenGL: shaderPath =
"shaders/glsl/";
break;
42 case bgfx::RendererType::OpenGLES: shaderPath =
"shaders/essl/";
break;
43 case bgfx::RendererType::Vulkan: shaderPath =
"shaders/spirv/";
break;
45 case bgfx::RendererType::Count:
46 BX_CHECK(
false,
"You should not be here!");
50 bx::strCopy(filePath, BX_COUNTOF(filePath), shaderPath);
51 bx::strCat(filePath, BX_COUNTOF(filePath), _name);
52 bx::strCat(filePath, BX_COUNTOF(filePath),
".bin");
54 bgfx::ShaderHandle handle = bgfx::createShader(loadMem(_reader, filePath));
55 bgfx::setName(handle, filePath);
60 bgfx::ShaderHandle loadShader(
const char* _name)
62 bx::FileReader fileReader;
63 return loadShader(&fileReader, _name);
66 bgfx::ProgramHandle loadProgram(bx::FileReaderI* _reader,
const char* _vsName,
const char* _fsName)
68 bgfx::ShaderHandle vsh = loadShader(_reader, _vsName);
69 bgfx::ShaderHandle fsh = BGFX_INVALID_HANDLE;
70 if (
nullptr != _fsName)
72 fsh = loadShader(_reader, _fsName);
75 return bgfx::createProgram(vsh, fsh,
true );
78 bgfx::ProgramHandle loadProgram(
const char* _vsName,
const char* _fsName)
80 bx::FileReader fileReader;
81 return loadProgram(&fileReader, _vsName, _fsName);