From 6cf5f67eae8ba4e4ef10a0559b2dfe019e2e54da Mon Sep 17 00:00:00 2001 From: Jeremie Roy Date: Tue, 7 May 2013 15:51:19 +0200 Subject: [PATCH] load shaders explicitely inside the samples --- examples/10-font/font.cpp | 39 ++++++++++-- examples/11-fontsdf/fontsdf.cpp | 63 +++++++++++++++++++- examples/common/font/makefile | 16 ++--- examples/common/font/text_buffer_manager.cpp | 26 ++++++-- examples/common/font/text_buffer_manager.h | 5 +- 5 files changed, 130 insertions(+), 19 deletions(-) diff --git a/examples/10-font/font.cpp b/examples/10-font/font.cpp index 5c9cb165..802ef7af 100644 --- a/examples/10-font/font.cpp +++ b/examples/10-font/font.cpp @@ -12,7 +12,6 @@ #include #include - static const char* s_shaderPath = NULL; long int fsize(FILE* _file) { @@ -45,6 +44,7 @@ static const bgfx::Memory* loadShader(const char* _shaderPath, const char* _shad return NULL; } + int _main_(int /*_argc*/, char** /*_argv*/) { uint32_t width = 1280; @@ -52,8 +52,7 @@ int _main_(int /*_argc*/, char** /*_argv*/) uint32_t debug = BGFX_DEBUG_TEXT; uint32_t reset = 0; - bgfx::init(); - + bgfx::init(); bgfx::reset(width, height); @@ -91,11 +90,37 @@ int _main_(int /*_argc*/, char** /*_argv*/) break; } + const bgfx::Memory* mem; + mem = loadShader(s_shaderPath, "vs_font_basic"); + bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_basic"); + bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _basicProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + mem = loadShader(s_shaderPath, "vs_font_distance_field"); + vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_distance_field"); + fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _distanceProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + mem = loadShader(s_shaderPath, "vs_font_distance_field_subpixel"); + vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_distance_field_subpixel"); + fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _distanceSubpixelProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + //init the text rendering system FontManager* fontManager = new FontManager(512); TextBufferManager* textBufferManager = new TextBufferManager(fontManager); - textBufferManager->init(s_shaderPath); - + textBufferManager->init(_basicProgram, _distanceProgram, _distanceSubpixelProgram); + //load some truetype files TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/times.ttf"); TrueTypeHandle consola_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/consola.ttf"); @@ -229,6 +254,10 @@ int _main_(int /*_argc*/, char** /*_argv*/) textBufferManager->destroyTextBuffer(staticText); textBufferManager->destroyTextBuffer(transientText); + bgfx::destroyProgram(_basicProgram); + bgfx::destroyProgram(_distanceProgram); + bgfx::destroyProgram(_distanceSubpixelProgram); + delete textBufferManager; delete fontManager; diff --git a/examples/11-fontsdf/fontsdf.cpp b/examples/11-fontsdf/fontsdf.cpp index 4eecaff4..7df4aedd 100644 --- a/examples/11-fontsdf/fontsdf.cpp +++ b/examples/11-fontsdf/fontsdf.cpp @@ -13,6 +13,37 @@ #include static const char* s_shaderPath = NULL; +long int fsize(FILE* _file) +{ + long int pos = ftell(_file); + fseek(_file, 0L, SEEK_END); + long int size = ftell(_file); + fseek(_file, pos, SEEK_SET); + return size; +} + +static const bgfx::Memory* loadShader(const char* _shaderPath, const char* _shaderName) +{ + char out[512]; + strcpy(out, _shaderPath); + strcat(out, _shaderName); + strcat(out, ".bin"); + + FILE* file = fopen(out, "rb"); + if (NULL != file) + { + uint32_t size = (uint32_t)fsize(file); + const bgfx::Memory* mem = bgfx::alloc(size+1); + /*size_t ignore =*/ fread(mem->data, 1, size, file); + /*BX_UNUSED(ignore);*/ + fclose(file); + mem->data[mem->size-1] = '\0'; + return mem; + } + + return NULL; +} + int _main_(int /*_argc*/, char** /*_argv*/) { @@ -61,10 +92,35 @@ int _main_(int /*_argc*/, char** /*_argv*/) break; } + const bgfx::Memory* mem; + mem = loadShader(s_shaderPath, "vs_font_basic"); + bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_basic"); + bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _basicProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + mem = loadShader(s_shaderPath, "vs_font_distance_field"); + vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_distance_field"); + fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _distanceProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + mem = loadShader(s_shaderPath, "vs_font_distance_field_subpixel"); + vsh = bgfx::createVertexShader(mem); + mem = loadShader(s_shaderPath, "fs_font_distance_field_subpixel"); + fsh = bgfx::createFragmentShader(mem); + bgfx::ProgramHandle _distanceSubpixelProgram = bgfx::createProgram(vsh, fsh); + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + //init the text rendering system FontManager* fontManager = new FontManager(512); TextBufferManager* textBufferManager = new TextBufferManager(fontManager); - textBufferManager->init(s_shaderPath); + textBufferManager->init(_basicProgram, _distanceProgram, _distanceSubpixelProgram); //load a truetype files TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/times.ttf"); @@ -148,6 +204,11 @@ int _main_(int /*_argc*/, char** /*_argv*/) } textBufferManager->destroyTextBuffer(staticText); + + bgfx::destroyProgram(_basicProgram); + bgfx::destroyProgram(_distanceProgram); + bgfx::destroyProgram(_distanceSubpixelProgram); + delete textBufferManager; delete fontManager; // Shutdown bgfx. diff --git a/examples/common/font/makefile b/examples/common/font/makefile index b38ac842..c9953f66 100644 --- a/examples/common/font/makefile +++ b/examples/common/font/makefile @@ -7,12 +7,14 @@ BGFX_DIR=../../.. RUNTIME_DIR=$(BGFX_DIR)/examples/runtime BUILD_DIR=../../../.build -include $(BGFX_DIR)/premake/shader-embeded.mk +# include $(BGFX_DIR)/premake/shader-embeded.mk +include $(BGFX_DIR)/premake/shader.mk rebuild: - @make -s --no-print-directory clean all -# @make -s --no-print-directory TARGET=0 clean all -# @make -s --no-print-directory TARGET=1 clean all -# @make -s --no-print-directory TARGET=2 clean all -# @make -s --no-print-directory TARGET=3 clean all -# @make -s --no-print-directory TARGET=4 clean all +# @make -s --no-print-directory clean all + @make -s --no-print-directory TARGET=0 clean all + @make -s --no-print-directory TARGET=1 clean all + @make -s --no-print-directory TARGET=2 clean all + @make -s --no-print-directory TARGET=3 clean all + @make -s --no-print-directory TARGET=4 clean all + diff --git a/examples/common/font/text_buffer_manager.cpp b/examples/common/font/text_buffer_manager.cpp index 1891d9ca..2e391f31 100644 --- a/examples/common/font/text_buffer_manager.cpp +++ b/examples/common/font/text_buffer_manager.cpp @@ -489,11 +489,27 @@ TextBufferManager::~TextBufferManager() bgfx::destroyUniform(u_texColor); bgfx::destroyUniform(u_inverse_gamma); - bgfx::destroyProgram(m_basicProgram); - bgfx::destroyProgram(m_distanceProgram); - bgfx::destroyProgram(m_distanceSubpixelProgram); + //bgfx::destroyProgram(m_basicProgram); + //bgfx::destroyProgram(m_distanceProgram); + //bgfx::destroyProgram(m_distanceSubpixelProgram); } +void TextBufferManager::init(bgfx::ProgramHandle _basicProgram, bgfx::ProgramHandle _distanceProgram, bgfx::ProgramHandle _distanceSubpixelProgram) +{ + m_basicProgram = _basicProgram; + m_distanceProgram = _distanceProgram; + m_distanceSubpixelProgram = _distanceSubpixelProgram; + + m_vertexDecl.begin(); + m_vertexDecl.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float); + m_vertexDecl.add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Int16, true); + m_vertexDecl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true); + m_vertexDecl.end(); + + u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv); + u_inverse_gamma = bgfx::createUniform("u_inverse_gamma", bgfx::UniformType::Uniform1f); +} +/* void TextBufferManager::init(const char* _shaderPath) { m_vertexDecl.begin(); @@ -514,7 +530,7 @@ void TextBufferManager::init(const char* _shaderPath) bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); - mem = loadShader(_shaderPath, "vs_font_distance_field"); + mem = loadShader(_shaderPath, "vs_font_distance_field"); vsh = bgfx::createVertexShader(mem); mem = loadShader(_shaderPath, "fs_font_distance_field"); fsh = bgfx::createFragmentShader(mem); @@ -529,7 +545,7 @@ void TextBufferManager::init(const char* _shaderPath) m_distanceSubpixelProgram = bgfx::createProgram(vsh, fsh); bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); -} +}*/ TextBufferHandle TextBufferManager::createTextBuffer(FontType _type, BufferType _bufferType) { diff --git a/examples/common/font/text_buffer_manager.h b/examples/common/font/text_buffer_manager.h index 4d662b3b..662974e2 100644 --- a/examples/common/font/text_buffer_manager.h +++ b/examples/common/font/text_buffer_manager.h @@ -31,7 +31,10 @@ public: TextBufferManager(FontManager* _fontManager = NULL); ~TextBufferManager(); - void init(const char* _shaderPath); + //shaders program + + void init(bgfx::ProgramHandle _basicProgram, bgfx::ProgramHandle _distanceProgram, bgfx::ProgramHandle _distanceSubpixelProgram); + //void init(const char* _shaderPath); TextBufferHandle createTextBuffer(FontType _type, BufferType _bufferType); void destroyTextBuffer(TextBufferHandle _handle);