1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
38 #include "glstate.hpp"
39 #include "glstate_internal.hpp"
45 // Mapping from shader type to shader source, used to accumulated the sources
46 // of different shaders with same type.
47 typedef std::map<std::string, std::string> ShaderMap;
51 getShaderSource(ShaderMap &shaderMap, GLuint shader)
57 GLint shader_type = 0;
58 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
63 GLint source_length = 0;
64 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
69 GLchar *source = new GLchar[source_length];
72 glGetShaderSource(shader, source_length, &length, source);
74 shaderMap[enumToString(shader_type)] += source;
81 getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj)
87 GLint object_type = 0;
88 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &object_type);
89 if (object_type != GL_SHADER_OBJECT_ARB) {
93 GLint shader_type = 0;
94 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SUBTYPE_ARB, &shader_type);
99 GLint source_length = 0;
100 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
101 if (!source_length) {
105 GLcharARB *source = new GLcharARB[source_length];
108 glGetShaderSource(shaderObj, source_length, &length, source);
110 shaderMap[enumToString(shader_type)] += source;
117 dumpProgram(JSONWriter &json, GLint program)
119 GLint attached_shaders = 0;
120 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
121 if (!attached_shaders) {
127 GLuint *shaders = new GLuint[attached_shaders];
129 glGetAttachedShaders(program, attached_shaders, &count, shaders);
130 std::sort(shaders, shaders + count);
131 for (GLsizei i = 0; i < count; ++ i) {
132 getShaderSource(shaderMap, shaders[i]);
136 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
137 json.beginMember(it->first);
138 json.writeString(it->second);
145 dumpProgramObj(JSONWriter &json, GLhandleARB programObj)
147 GLint attached_shaders = 0;
148 glGetObjectParameterivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
149 if (!attached_shaders) {
155 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
157 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
158 std::sort(shaderObjs, shaderObjs + count);
159 for (GLsizei i = 0; i < count; ++ i) {
160 getShaderObjSource(shaderMap, shaderObjs[i]);
162 delete [] shaderObjs;
164 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
165 json.beginMember(it->first);
166 json.writeString(it->second);
172 * Built-in uniforms can't be queried through glGetUniform*.
175 isBuiltinUniform(const GLchar *name)
177 return name[0] == 'g' && name[1] == 'l' && name[2] == '_';
181 * When fetching the uniform name of an array we usually get name[0]
182 * so we need to cut the trailing "[0]" in order to properly construct
183 * array names later. Otherwise we endup with stuff like
184 * uniformArray[0][0],
185 * uniformArray[0][1],
191 resolveUniformName(const GLchar *name, GLint size)
193 std::string qualifiedName(name);
195 std::string::size_type nameLength = qualifiedName.length();
196 static const char * const arrayStart = "[0]";
197 static const int arrayStartLen = 3;
198 if (qualifiedName.rfind(arrayStart) == (nameLength - arrayStartLen)) {
199 qualifiedName = qualifiedName.substr(0, nameLength - 3);
202 return qualifiedName;
207 dumpUniformValues(JSONWriter &json, GLenum type, const void *values, GLint matrix_stride = 0, GLboolean is_row_major = GL_FALSE) {
209 GLint numCols, numRows;
210 _gl_uniform_size(type, elemType, numCols, numRows);
211 if (!numCols || !numRows) {
215 size_t elemSize = _gl_type_size(elemType);
217 GLint row_stride = 0;
218 GLint col_stride = 0;
220 row_stride = elemSize;
221 col_stride = matrix_stride ? matrix_stride : numRows * elemSize;
223 col_stride = elemSize;
224 row_stride = matrix_stride ? matrix_stride : numCols * elemSize;
231 for (GLint row = 0; row < numRows; ++row) {
236 for (GLint col = 0; col < numCols; ++col) {
238 const GLbyte *rawvalue;
239 const GLfloat *fvalue;
240 const GLdouble *dvalue;
242 const GLuint *uivalue;
245 u.rawvalue = (const GLbyte *)values + row*row_stride + col*col_stride;
249 json.writeFloat(*u.fvalue);
252 json.writeFloat(*u.dvalue);
255 json.writeInt(*u.ivalue);
257 case GL_UNSIGNED_INT:
258 json.writeInt(*u.uivalue);
261 json.writeBool(*u.uivalue);
282 * Dump an uniform that belows to an uniform block.
285 dumpUniformBlock(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name, GLuint index, GLuint block_index) {
288 GLint array_stride = 0;
289 GLint matrix_stride = 0;
290 GLint is_row_major = GL_FALSE;
291 glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_OFFSET, &offset);
292 glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_ARRAY_STRIDE, &array_stride);
293 glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_MATRIX_STRIDE, &matrix_stride);
294 glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_IS_ROW_MAJOR, &is_row_major);
297 glGetActiveUniformBlockiv(program, block_index, GL_UNIFORM_BLOCK_BINDING, &slot);
303 GLint active_uniform_block_max_name_length = 0;
304 glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &active_uniform_block_max_name_length);
306 GLchar* block_name = new GLchar[active_uniform_block_max_name_length];
308 GLint block_data_size = 0;
309 glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_DATA_SIZE, &block_data_size);
312 glGetActiveUniformBlockName(program, index, active_uniform_block_max_name_length, &length, block_name);
315 << "uniform `" << name << "`, size " << size << ", type " << enumToString(type) << "\n"
316 << " block " << block_index << ", name `" << block_name << "`, size " << block_data_size << "; binding " << slot << "; \n"
317 << " offset " << offset << ", array stride " << array_stride << ", matrix stride " << matrix_stride << ", row_major " << is_row_major << "\n"
324 glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, slot, &ubo);
326 GLint previous_ubo = 0;
327 glGetIntegerv(GL_UNIFORM_BUFFER_BINDING, &previous_ubo);
329 glBindBuffer(GL_UNIFORM_BUFFER, ubo);
331 const GLbyte *raw_data = (const GLbyte *)glMapBuffer(GL_UNIFORM_BUFFER, GL_READ_ONLY);
333 std::string qualifiedName = resolveUniformName(name, size);
335 for (GLint i = 0; i < size; ++i) {
336 std::stringstream ss;
340 ss << '[' << i << ']';
343 std::string elemName = ss.str();
345 json.beginMember(elemName);
347 const GLbyte *row = raw_data + offset + array_stride*i;
349 dumpUniformValues(json, type, row, matrix_stride, is_row_major);
354 glUnmapBuffer(GL_UNIFORM_BUFFER);
357 glBindBuffer(GL_UNIFORM_BUFFER, previous_ubo);
362 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
364 GLint numCols, numRows;
365 _gl_uniform_size(type, elemType, numCols, numRows);
366 if (elemType == GL_NONE) {
371 GLfloat fvalues[4*4];
372 GLdouble dvalues[4*4];
374 GLuint uivalues[4*4];
379 std::string qualifiedName = resolveUniformName(name, size);
381 for (i = 0; i < size; ++i) {
382 std::stringstream ss;
386 ss << '[' << i << ']';
389 std::string elemName = ss.str();
391 GLint location = glGetUniformLocation(program, elemName.c_str());
392 assert(location != -1);
393 if (location == -1) {
397 json.beginMember(elemName);
401 glGetUniformfv(program, location, u.fvalues);
404 glGetUniformdv(program, location, u.dvalues);
407 glGetUniformiv(program, location, u.ivalues);
409 case GL_UNSIGNED_INT:
410 glGetUniformuiv(program, location, u.uivalues);
413 glGetUniformiv(program, location, u.ivalues);
420 dumpUniformValues(json, type, &u);
428 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
430 GLint numCols, numRows;
431 _gl_uniform_size(type, elemType, numCols, numRows);
432 GLint numElems = numCols * numRows;
433 if (elemType == GL_NONE) {
437 GLfloat fvalues[4*4];
439 GLdouble dvalues[4*4];
440 GLfloat fvalues[4*4];
446 std::string qualifiedName = resolveUniformName(name, size);
448 for (i = 0; i < size; ++i) {
449 std::stringstream ss;
453 ss << '[' << i << ']';
456 std::string elemName = ss.str();
458 json.beginMember(elemName);
460 GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
461 if (location == -1) {
467 // glGetUniformdvARB does not exists
468 glGetUniformfvARB(programObj, location, fvalues);
469 for (j = 0; j < numElems; ++j) {
470 u.dvalues[j] = fvalues[j];
474 glGetUniformfvARB(programObj, location, fvalues);
476 case GL_UNSIGNED_INT:
477 // glGetUniformuivARB does not exists
479 glGetUniformivARB(programObj, location, u.ivalues);
482 glGetUniformivARB(programObj, location, u.ivalues);
489 dumpUniformValues(json, type, &u);
497 dumpProgramUniforms(JSONWriter &json, GLint program)
499 GLint active_uniforms = 0;
500 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
501 if (!active_uniforms) {
505 GLint active_uniform_max_length = 0;
506 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
507 GLchar *name = new GLchar[active_uniform_max_length];
512 for (GLuint index = 0; (GLint)index < active_uniforms; ++index) {
515 GLenum type = GL_NONE;
516 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
518 if (isBuiltinUniform(name)) {
522 GLint location = glGetUniformLocation(program, name);
523 if (location != -1) {
524 dumpUniform(json, program, size, type, name);
528 GLint block_index = -1;
529 glGetActiveUniformsiv(program, 1, &index, GL_UNIFORM_BLOCK_INDEX, &block_index);
530 if (block_index != -1) {
531 dumpUniformBlock(json, program, size, type, name, index, block_index);
543 dumpProgramObjUniforms(JSONWriter &json, GLhandleARB programObj)
545 GLint active_uniforms = 0;
546 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
547 if (!active_uniforms) {
551 GLint active_uniform_max_length = 0;
552 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
553 GLchar *name = new GLchar[active_uniform_max_length];
558 for (GLint index = 0; index < active_uniforms; ++index) {
561 GLenum type = GL_NONE;
562 glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
564 if (isBuiltinUniform(name)) {
568 dumpUniformARB(json, programObj, size, type, name);
576 dumpArbProgram(JSONWriter &json, GLenum target)
578 if (!glIsEnabled(target)) {
582 GLint program_length = 0;
583 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
584 if (!program_length) {
588 GLchar *source = new GLchar[program_length + 1];
590 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
591 source[program_length] = 0;
593 json.beginMember(enumToString(target));
594 json.writeString(source);
602 dumpArbProgramUniforms(JSONWriter &json, GLenum target, const char *prefix)
604 if (!glIsEnabled(target)) {
608 GLint program_parameters = 0;
609 glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
610 if (!program_parameters) {
614 GLint max_program_local_parameters = 0;
615 glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
616 for (GLint index = 0; index < max_program_local_parameters; ++index) {
617 GLdouble params[4] = {0, 0, 0, 0};
618 glGetProgramLocalParameterdvARB(target, index, params);
620 if (!params[0] && !params[1] && !params[2] && !params[3]) {
625 snprintf(name, sizeof name, "%sprogram.local[%i]", prefix, index);
627 json.beginMember(name);
629 json.writeFloat(params[0]);
630 json.writeFloat(params[1]);
631 json.writeFloat(params[2]);
632 json.writeFloat(params[3]);
637 GLint max_program_env_parameters = 0;
638 glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
639 for (GLint index = 0; index < max_program_env_parameters; ++index) {
640 GLdouble params[4] = {0, 0, 0, 0};
641 glGetProgramEnvParameterdvARB(target, index, params);
643 if (!params[0] && !params[1] && !params[2] && !params[3]) {
648 snprintf(name, sizeof name, "%sprogram.env[%i]", prefix, index);
650 json.beginMember(name);
652 json.writeFloat(params[0]);
653 json.writeFloat(params[1]);
654 json.writeFloat(params[2]);
655 json.writeFloat(params[3]);
662 dumpProgramUniformsStage(JSONWriter &json, GLint program, const char *stage)
665 json.beginMember(stage);
667 dumpProgramUniforms(json, program);
674 dumpShadersUniforms(JSONWriter &json, Context &context)
677 GLint vertex_program = 0;
678 GLint fragment_program = 0;
679 GLint geometry_program = 0;
680 GLint tess_control_program = 0;
681 GLint tess_evaluation_program = 0;
684 glGetIntegerv(GL_PROGRAM_PIPELINE_BINDING, &pipeline);
686 glGetProgramPipelineiv(pipeline, GL_VERTEX_SHADER, &vertex_program);
687 glGetProgramPipelineiv(pipeline, GL_FRAGMENT_SHADER, &fragment_program);
688 glGetProgramPipelineiv(pipeline, GL_GEOMETRY_SHADER, &geometry_program);
689 glGetProgramPipelineiv(pipeline, GL_TESS_CONTROL_SHADER, &tess_control_program);
690 glGetProgramPipelineiv(pipeline, GL_TESS_EVALUATION_SHADER, &tess_evaluation_program);
695 GLhandleARB programObj = 0;
697 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
698 if (!context.ES && !program) {
699 programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
703 json.beginMember("shaders");
706 dumpProgram(json, vertex_program);
707 dumpProgram(json, fragment_program);
708 dumpProgram(json, geometry_program);
709 dumpProgram(json, tess_control_program);
710 dumpProgram(json, tess_evaluation_program);
711 } else if (program) {
712 dumpProgram(json, program);
713 } else if (programObj) {
714 dumpProgramObj(json, programObj);
716 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
717 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
720 json.endMember(); // shaders
722 json.beginMember("uniforms");
725 dumpProgramUniformsStage(json, vertex_program, "GL_VERTEX_SHADER");
726 dumpProgramUniformsStage(json, fragment_program, "GL_FRAGMENT_SHADER");
727 dumpProgramUniformsStage(json, geometry_program, "GL_GEOMETRY_SHADER");
728 dumpProgramUniformsStage(json, tess_control_program, "GL_TESS_CONTROL_SHADER");
729 dumpProgramUniformsStage(json, tess_evaluation_program, "GL_TESS_EVALUATION_SHADER");
730 } else if (program) {
731 dumpProgramUniforms(json, program);
732 } else if (programObj) {
733 dumpProgramObjUniforms(json, programObj);
735 dumpArbProgramUniforms(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
736 dumpArbProgramUniforms(json, GL_VERTEX_PROGRAM_ARB, "vp.");
739 json.endMember(); // uniforms
743 } /* namespace glstate */