std::shared_ptr<ShaderProgram> ContentManager::LoadNew(const std::string &name, bool async) { FilePtr fp = FileManager::Instance()->OpenFile(name); if (!fp) { LOG("Failed loading '%s': file not found", name.c_str()); return nullptr; } int size = fp->GetSize(); char *data = new char[size + 1]; fp->Read(data, size); fp->Close(); data[size] = 0; ShaderProgramPtr program = std::make_shared<ShaderProgram>(); ShaderPtr vs = std::make_shared<Shader>(GL_VERTEX_SHADER); if (!vs->Load(data)) { LOG("Error loading '%s' vertex shader: %s", name.c_str(), vs->ErrorMessage().c_str()); return nullptr; } program->AddShader(vs); ShaderPtr ps = std::make_shared<Shader>(GL_FRAGMENT_SHADER); if (!ps->Load(data)) { LOG("Error loading '%s' fragment shader: %s", name.c_str(), vs->ErrorMessage().c_str()); return nullptr; } program->AddShader(ps); if (!program->Link()) { LOG("Error loading '%s': link failed", name.c_str()); return nullptr; } ContentManager::Instance()->CacheObject(name, program); LOG("Loaded shader '%s'", name.c_str()); return program; }
MaterialPassPtr PassParser::CreateObject(Sys_GraphicsPtr pGraphics) { ShaderProgramPtr pProgram = pGraphics->CreateShaderProgram(); if(_VertexShader != "" && pProgram->AttachShaderFromFile(ST_VERTEX_SHADER, _VertexShader.string().c_str(), _VertexShaderEntry) == false) { Error(0, "failed to create vertex shader: '" + _VertexShader.string() + "'"); pProgram->Release(); return nullptr; } if(_PixelShader != "" && pProgram->AttachShaderFromFile(ST_PIXEL_SHADER, _PixelShader.string().c_str(), _PixelShaderEntry) == false) { Error(0, "Failed to create pixel shader: '" + _PixelShader.string() + "'"); pProgram->Release(); return nullptr; } if(_GeometryShader != "" && pProgram->AttachShaderFromFile(ST_GEOMETRY_SHADER, _GeometryShader.string().c_str(), _GeometryShaderEntry) == false) { Error(0, "Failed to create geometry shader: '" + _GeometryShader.string() + "'"); pProgram->Release(); return nullptr; } if(false == pProgram->Link()) { pProgram->Release(); return nullptr; } MaterialPassPtr pPass = std::make_shared<MaterialPass>(m_name, pGraphics); pPass->AttachProgram(pProgram); for(auto v : _SamplerLink) { SamplerStateParserPtr pParser = std::dynamic_pointer_cast<SamplerStateParser>(FindSymbol(v.second, false)); if(pParser == nullptr) { assert(0); continue; } if(pProgram->FindParameterByName(v.first.c_str()) == -1) { Error(0, "sampler name not found: '" + v.first + "'"); continue; } pPass->AddSamplerLink(v.first, pParser->CreateObject(pGraphics)); } RenderStateParserPtr pRSParser = std::dynamic_pointer_cast<RenderStateParser>(FindSymbol(_RenderState, false)); if(pRSParser) { pPass->AttachRenderState(pRSParser->CreateObject(pGraphics)); } else { pPass->AttachRenderState(pGraphics->CreateRenderState()); } return pPass; }