load shaders explicitely inside the samples

This commit is contained in:
Jeremie Roy 2013-05-07 15:51:19 +02:00
parent d470ac2567
commit 6cf5f67eae
5 changed files with 130 additions and 19 deletions

View file

@ -12,7 +12,6 @@
#include <stdio.h>
#include <string.h>
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;

View file

@ -13,6 +13,37 @@
#include <string.h>
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.

View file

@ -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

View file

@ -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)
{

View file

@ -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);