18 #include "precompiled.h"
38 template<
typename S,
typename T>
bool operator()(
const std::pair<S, T>& a,
const std::pair<S, T>& b)
const
40 return b.second < a.second;
46 #if USE_SHADER_XML_VALIDATION
51 LOGERROR(L
"Failed to read grammar shaders/program.rng");
55 LOGERROR(L
"Failed to load grammar shaders/program.rng");
72 std::map<CacheKey, CShaderProgramPtr>::iterator it =
m_ProgramCache.find(key);
79 LOGERROR(L
"Failed to load shader '%hs'", name);
90 if (str ==
"gl_Vertex")
return 0;
91 if (str ==
"gl_Normal")
return 2;
92 if (str ==
"gl_Color")
return 3;
93 if (str ==
"gl_SecondaryColor")
return 4;
94 if (str ==
"gl_FogCoord")
return 5;
95 if (str ==
"gl_MultiTexCoord0")
return 8;
96 if (str ==
"gl_MultiTexCoord1")
return 9;
97 if (str ==
"gl_MultiTexCoord2")
return 10;
98 if (str ==
"gl_MultiTexCoord3")
return 11;
99 if (str ==
"gl_MultiTexCoord4")
return 12;
100 if (str ==
"gl_MultiTexCoord5")
return 13;
101 if (str ==
"gl_MultiTexCoord6")
return 14;
102 if (str ==
"gl_MultiTexCoord7")
return 15;
106 if (str ==
"CustomAttribute0")
return 1;
107 if (str ==
"CustomAttribute1")
return 6;
108 if (str ==
"CustomAttribute2")
return 7;
119 if (strncmp(name,
"fixed:", 6) == 0)
135 #if USE_SHADER_XML_VALIDATION
150 #define EL(x) int el_##x = XeroFile.GetElementID(#x)
151 #define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
177 std::map<CStrIntern, int> vertexUniforms;
178 std::map<CStrIntern, CShaderProgram::frag_index_pair_t> fragmentUniforms;
179 std::map<CStrIntern, int> vertexAttribs;
184 if (Child.GetNodeName() == el_define)
186 defines.
Add(
CStrIntern(Child.GetAttributes().GetNamedItem(at_name)),
CStrIntern(Child.GetAttributes().GetNamedItem(at_value)));
188 else if (Child.GetNodeName() == el_vertex)
190 vertexFile = L
"shaders/" + Child.GetAttributes().GetNamedItem(at_file).FromUTF8();
200 if (Param.GetNodeName() == el_uniform)
204 else if (Param.GetNodeName() == el_stream)
207 if (StreamName ==
"pos")
209 else if (StreamName ==
"normal")
211 else if (StreamName ==
"color")
213 else if (StreamName ==
"uv0")
215 else if (StreamName ==
"uv1")
217 else if (StreamName ==
"uv2")
219 else if (StreamName ==
"uv3")
222 else if (Param.GetNodeName() == el_attrib)
229 else if (Child.GetNodeName() == el_fragment)
231 fragmentFile = L
"shaders/" + Child.GetAttributes().GetNamedItem(at_file).FromUTF8();
241 if (Param.GetNodeName() == el_uniform)
246 GLenum type = GL_TEXTURE_2D;
248 if (t ==
"sampler1D")
249 type = GL_TEXTURE_1D;
250 else if (t ==
"sampler2D")
251 type = GL_TEXTURE_2D;
252 else if (t ==
"sampler3D")
253 type = GL_TEXTURE_3D;
254 else if (t ==
"samplerCube")
255 type = GL_TEXTURE_CUBE_MAP;
258 std::make_pair(Attrs.
GetNamedItem(at_loc).ToInt(), type);
292 if (str ==
"greater")
294 if (str ==
"notequal")
306 if (str ==
"src_color")
308 if (str ==
"one_minus_src_color")
309 return GL_ONE_MINUS_SRC_COLOR;
310 if (str ==
"dst_color")
312 if (str ==
"one_minus_dst_color")
313 return GL_ONE_MINUS_DST_COLOR;
314 if (str ==
"src_alpha")
316 if (str ==
"one_minus_src_alpha")
317 return GL_ONE_MINUS_SRC_ALPHA;
318 if (str ==
"dst_alpha")
320 if (str ==
"one_minus_dst_alpha")
321 return GL_ONE_MINUS_DST_ALPHA;
322 if (str ==
"constant_color")
323 return GL_CONSTANT_COLOR;
324 if (str ==
"one_minus_constant_color")
325 return GL_ONE_MINUS_CONSTANT_COLOR;
326 if (str ==
"constant_alpha")
327 return GL_CONSTANT_ALPHA;
328 if (str ==
"one_minus_constant_alpha")
329 return GL_ONE_MINUS_CONSTANT_ALPHA;
330 if (str ==
"src_alpha_saturate")
331 return GL_SRC_ALPHA_SATURATE;
386 if (strncmp(name,
"shader:", 7) == 0)
405 #define EL(x) int el_##x = XeroFile.GetElementID(#x)
406 #define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
413 EL(sort_by_distance);
428 bool hasARB = (baseDefines.
GetInt(
"SYS_HAS_ARB") != 0);
429 bool hasGLSL = (baseDefines.
GetInt(
"SYS_HAS_GLSL") != 0);
430 bool preferGLSL = (baseDefines.
GetInt(
"SYS_PREFER_GLSL") != 0);
440 std::vector<std::pair<XMBElement, int> > usableTechs;
445 bool isUsable =
true;
450 if (Child.GetNodeName() == el_require)
484 usableTechs.push_back(std::make_pair(Technique, preference));
487 if (usableTechs.empty())
494 std::stable_sort(usableTechs.begin(), usableTechs.end(),
revcompare2nd());
500 if (Child.GetNodeName() == el_define)
502 techDefines.Add(
CStrIntern(Child.GetAttributes().GetNamedItem(at_name)),
CStrIntern(Child.GetAttributes().GetNamedItem(at_value)));
504 else if (Child.GetNodeName() == el_sort_by_distance)
506 tech->SetSortByDistance(
true);
508 else if (Child.GetNodeName() == el_pass)
516 if (Element.GetNodeName() == el_define)
518 passDefines.
Add(
CStrIntern(Element.GetAttributes().GetNamedItem(at_name)),
CStrIntern(Element.GetAttributes().GetNamedItem(at_value)));
520 else if (Element.GetNodeName() == el_alpha)
523 float ref = Element.GetAttributes().GetNamedItem(at_ref).ToFloat();
526 else if (Element.GetNodeName() == el_blend)
528 GLenum src =
ParseBlendFunc(Element.GetAttributes().GetNamedItem(at_src));
529 GLenum dst =
ParseBlendFunc(Element.GetAttributes().GetNamedItem(at_dst));
532 else if (Element.GetNodeName() == el_depth)
534 if (!Element.GetAttributes().GetNamedItem(at_func).empty())
537 if (!Element.GetAttributes().GetNamedItem(at_mask).empty())
538 pass.
DepthMask(Element.GetAttributes().GetNamedItem(at_mask) ==
"true" ? 1 : 0);
543 pass.
SetShader(
LoadProgram(Child.GetAttributes().GetNamedItem(at_shader).c_str(), passDefines));
569 for (std::set<boost::weak_ptr<CShaderProgram> >::iterator it = files->second.begin(); it != files->second.end(); ++it)
571 if (shared_ptr<CShaderProgram> program = it->lock())
void Add(CStrIntern name, CStrIntern value)
Add a name and associated value to the map of defines.
static GLenum ParseComparisonFunc(const CStr &str)
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Load from an XML file (with invisible XMB caching).
const PSRETURN PSRETURN_OK
Reads a file, then gives read-only access to the contents.
static GLenum ParseAttribSemantics(const CStr &str)
HotloadFilesMap m_HotloadFiles
bool operator()(const std::pair< S, T > &a, const std::pair< S, T > &b) const
void AlphaFunc(GLenum func, GLclampf ref)
XMBAttributeList GetAttributes() const
#define XERO_ITER_EL(parent_element, child_element)
bool TestConditional(const CStr &expr)
Key for effect cache lookups.
shared_ptr< CShaderTechnique > CShaderTechniquePtr
std::map< CacheKey, CShaderProgramPtr > m_ProgramCache
static CShaderProgram * ConstructARB(const VfsPath &vertexFile, const VfsPath &fragmentFile, const CShaderDefines &defines, const std::map< CStrIntern, int > &vertexIndexes, const std::map< CStrIntern, frag_index_pair_t > &fragmentIndexes, int streamflags)
Construct based on ARB vertex/fragment program files.
bool NewEffect(const char *name, const CShaderDefines &defines, CShaderTechniquePtr &tech)
Status ReloadChangedFile(const VfsPath &path)
Implements a render pass consisting of various GL state changes and a shader, used by CShaderTechniqu...
#define TIMER_ACCRUE(client)
Measure the time taken to execute code up until end of the current scope; bill it to the given TimerC...
static CShaderProgram * ConstructFFP(const std::string &id, const CShaderDefines &defines)
Construct an instance of a pre-defined fixed-function pipeline setup.
u32 GetHash() const
Returns cached FNV1-A hash of the string.
CStr GetAsString() const
Returns contents of file as a string.
const char * c_str() const
Returns null-terminated string.
#define PROFILE2(region)
Starts timing from now until the end of the current scope.
void BlendFunc(GLenum src, GLenum dst)
void UnregisterFileReloadFunc(FileReloadFunc func, void *obj)
delete a callback function registered with RegisterFileReloadFunc (removes any with the same func and...
void RegisterFileReloadFunc(FileReloadFunc func, void *obj)
register a callback function to be called by ReloadChangedFiles
CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines &defines1, const CShaderDefines &defines2)
Load a shader effect.
EffectCacheMap m_EffectCache
#define XML_SetPrettyPrint(enabled)
Implements a render technique consisting of a sequence of passes.
i64 Status
Error handling system.
#define XML_WriteXMB(xero)
CStr8 GetNamedItem(const int AttributeName) const
bool operator==(const EffectCacheKey &b) const
RelaxNGValidator m_Validator
void DepthFunc(GLenum func)
size_t GetHash() const
Return a hash of the current mapping.
std::wstring wstring_from_utf8(const std::string &src, Status *err)
convert UTF-8 to a wide string (UTF-16 or UCS-4, depending on the platform's wchar_t).
static Status ReloadChangedFileCB(void *param, const VfsPath &path)
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
static GLenum ParseBlendFunc(const CStr &str)
#define PROFILE2_ATTR
Associates a string (with printf-style formatting) with the current region or event.
void AddDefines(const CShaderDefines &defines)
void SetMany(const CShaderParams ¶ms)
Add all the names and values from another set of parameters.
void DepthMask(GLboolean mask)
#define TIMER_ADD_CLIENT(id)
"allocate" a new TimerClient that will keep track of the total time billed to it, along with a descri...
XMBElement GetRoot() const
int GetInt(const char *name) const
Return the value for the given name as an integer, or 0 if not defined.
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
bool LoadGrammar(const std::string &grammar)
#define debug_warn(expr)
display the error dialog with the given text.
CShaderProgramPtr LoadProgram(const char *name, const CShaderDefines &defines)
Load a shader program.
size_t GetNumEffectsLoaded()
Returns the number of shader effects that are currently loaded.
size_t operator()(const EffectCacheKey &key) const
Convenience wrapper around CPreprocessor.
bool NewProgram(const char *name, const CShaderDefines &defines, CShaderProgramPtr &program)
void SetShader(const CShaderProgramPtr &shader)
Set the shader program used for rendering with this pass.
static CShaderProgram * ConstructGLSL(const VfsPath &vertexFile, const VfsPath &fragmentFile, const CShaderDefines &defines, const std::map< CStrIntern, int > &vertexAttribs, int streamflags)
Construct based on GLSL vertex/fragment shader files.
shared_ptr< CShaderProgram > CShaderProgramPtr
Shader manager: loads and caches shader programs.
bool ValidateEncoded(const std::wstring &filename, const std::string &document)