From a15b7fefe442011a3599f9ce61a465f97a12b6f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 16 Jul 2011 21:08:16 -0700 Subject: [PATCH] Accumulate the sources of different shader objects with the same type. --- glstate.cpp | 41 ++++++++++++++++++++++++++++++----------- json.hpp | 11 ++++++++++- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/glstate.cpp b/glstate.cpp index a55982a..a689f11 100644 --- a/glstate.cpp +++ b/glstate.cpp @@ -25,8 +25,10 @@ #include -#include + #include +#include +#include #include "image.hpp" #include "json.hpp" @@ -76,8 +78,13 @@ restorePixelPackState(void) { } +// Mapping from shader type to shader source, used to accumulated the sources +// of different shaders with same type. +typedef std::map ShaderMap; + + static void -dumpShader(JSONWriter &json, GLuint shader) +getShaderSource(ShaderMap &shaderMap, GLuint shader) { if (!shader) { return; @@ -100,16 +107,14 @@ dumpShader(JSONWriter &json, GLuint shader) source[0] = 0; glGetShaderSource(shader, source_length, &length, source); - json.beginMember(enumToString(shader_type)); - json.writeString(source); - json.endMember(); + shaderMap[enumToString(shader_type)] += source; delete [] source; } static void -dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj) +getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj) { if (!shaderObj) { return; @@ -132,9 +137,7 @@ dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj) source[0] = 0; glGetShaderSource(shaderObj, source_length, &length, source); - json.beginMember(enumToString(shader_type)); - json.writeString(source); - json.endMember(); + shaderMap[enumToString(shader_type)] += source; delete [] source; } @@ -155,13 +158,21 @@ dumpCurrentProgram(JSONWriter &json) return; } + ShaderMap shaderMap; + GLuint *shaders = new GLuint[attached_shaders]; GLsizei count = 0; glGetAttachedShaders(program, attached_shaders, &count, shaders); for (GLsizei i = 0; i < count; ++ i) { - dumpShader(json, shaders[i]); + getShaderSource(shaderMap, shaders[i]); } delete [] shaders; + + for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) { + json.beginMember(it->first); + json.writeString(it->second); + json.endMember(); + } } @@ -179,13 +190,21 @@ dumpCurrentProgramObj(JSONWriter &json) return; } + ShaderMap shaderMap; + GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders]; GLsizei count = 0; glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs); for (GLsizei i = 0; i < count; ++ i) { - dumpShaderObj(json, shaderObjs[i]); + getShaderObjSource(shaderMap, shaderObjs[i]); } delete [] shaderObjs; + + for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) { + json.beginMember(it->first); + json.writeString(it->second); + json.endMember(); + } } diff --git a/json.hpp b/json.hpp index d13793e..9e6b960 100644 --- a/json.hpp +++ b/json.hpp @@ -34,8 +34,9 @@ #include #include -#include #include +#include +#include class JSONWriter @@ -240,6 +241,10 @@ public: value = false; } + inline void beginMember(const std::string &name) { + beginMember(name.c_str()); + } + inline void endMember(void) { assert(value); value = true; @@ -271,6 +276,10 @@ public: space = ' '; } + inline void writeString(const std::string &s) { + writeString(s.c_str()); + } + inline void writeBase64(const void *bytes, size_t size) { separator(); encodeBase64String((const unsigned char *)bytes, size); -- 2.7.4