Shader::Shader(const std::string& vertShader, const std::string& fragShader) { auto vertSrc = getShaderSource(vertShader); auto fragSrc = getShaderSource(fragShader); createVertexShader(vertSrc); createFragmentShader(fragSrc); createProgram(); freeResources(); }
void GraphicsContext3D::getShaderiv(Platform3DObject shader, GC3Denum pname, GC3Dint* value) { ASSERT(shader); makeContextCurrent(); HashMap<Platform3DObject, ShaderSourceEntry>::iterator result = m_shaderSourceMap.find(shader); switch (pname) { case DELETE_STATUS: case SHADER_TYPE: ::glGetShaderiv(shader, pname, value); break; case COMPILE_STATUS: if (result == m_shaderSourceMap.end()) { *value = static_cast<int>(false); return; } *value = static_cast<int>(result->second.isValid); break; case INFO_LOG_LENGTH: if (result == m_shaderSourceMap.end()) { *value = 0; return; } *value = getShaderInfoLog(shader).length(); break; case SHADER_SOURCE_LENGTH: *value = getShaderSource(shader).length(); break; default: synthesizeGLError(INVALID_ENUM); } }
static inline void dumpProgram(JSONWriter &json, GLint program) { GLint attached_shaders = 0; glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders); if (!attached_shaders) { return; } ShaderMap shaderMap; GLuint *shaders = new GLuint[attached_shaders]; GLsizei count = 0; glGetAttachedShaders(program, attached_shaders, &count, shaders); std::sort(shaders, shaders + count); for (GLsizei i = 0; i < count; ++ 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(); } }
GLuint createShader(std::string shaderName, GLenum shaderType) { LOGV("Creating shader from %s...", shaderName.c_str()); GLuint shaderHandle = glCreateShader(shaderType); LOGV("created shader with handle %d", shaderHandle); if (shaderType != 0) { const char* shaderSource = getShaderSource(shaderName); if (shaderSource != nullptr) { glShaderSource(shaderHandle, 1, &shaderSource, nullptr); glCompileShader(shaderHandle); GLint compileStatus; glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileStatus); if (compileStatus != GL_TRUE) { GLint infoLogSize; glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogSize); char* infoLog = new char[infoLogSize]; glGetShaderInfoLog(shaderHandle, infoLogSize, nullptr, infoLog); LOGE("Can't compile shader %s: %s", shaderName.c_str(), infoLog); delete[] infoLog; glDeleteShader(shaderHandle); shaderHandle = 0; } } else { glDeleteShader(shaderHandle); shaderHandle = 0; } } if (shaderHandle == 0) { LOGE("Error creating shader %s", shaderName.c_str()); } //TODO: glError? return shaderHandle; }
static inline void dumpProgram(StateWriter &writer, Context &context, GLint program) { if (program <= 0) { return; } GLint attached_shaders = 0; glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders); if (!attached_shaders) { return; } ShaderMap shaderMap; GLuint *shaders = new GLuint[attached_shaders]; GLsizei count = 0; glGetAttachedShaders(program, attached_shaders, &count, shaders); std::sort(shaders, shaders + count); for (GLsizei i = 0; i < count; ++ i) { getShaderSource(shaderMap, shaders[i]); } delete [] shaders; for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) { writer.beginMember(it->first); writer.writeString(it->second); writer.endMember(); } // Dump NVIDIA GPU programs via GL_ARB_get_program_binary if (context.ARB_get_program_binary) { GLint binaryLength = 0; glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binaryLength); if (binaryLength > 0) { std::vector<char> binary(binaryLength); GLenum format = GL_NONE; glGetProgramBinary(program, binaryLength, NULL, &format, &binary[0]); if (format == 0x8e21) { if (0) { FILE *fp = fopen("program.bin", "wb"); if (fp) { fwrite(&binary[0], 1, binaryLength, fp); fclose(fp); } } // Extract NVIDIA GPU programs std::string str(binary.begin(), binary.end()); size_t end = 0; while (true) { // Each program starts with a !!NV header token size_t start = str.find("!!NV", end); if (start == std::string::npos) { break; } // And is preceeded by a length DWORD assert(start >= end + 4); if (start < end + 4) { break; } uint32_t length; str.copy(reinterpret_cast<char *>(&length), 4, start - 4); assert(start + length <= binaryLength); if (start + length > binaryLength) { break; } std::string nvProg = str.substr(start, length); size_t eol = nvProg.find('\n'); std::string nvHeader = nvProg.substr(2, eol - 2); writer.beginMember(nvHeader); writer.writeString(nvProg); writer.endMember(); end = start + length; } } } } }
/** Compiles and links program with variable amount of shaders * * @param po_id Program handle * @param out_has_compilation_failed Deref will be set to true, if shader compilation * failed for any of the submitted shaders. * Will be set to false otherwise. Can be NULL. * @param sh_stages Shader stages * @for all shader stages * { * @param sh_id Shader handle. 0 means "skip" * @param sh_parts Number of shader source code parts. * 0 means that it's already compiled. * @param sh_code Shader source code. * } **/ bool TestCaseBase::buildProgramVA(glw::GLuint po_id, bool* out_has_compilation_failed, unsigned int sh_stages, ...) { const glw::Functions& gl = m_context.getRenderContext().getFunctions(); std::vector<glw::GLuint> vec_sh_id; va_list values; va_start(values, sh_stages); /* Shaders compilation */ glw::GLint compilation_status = GL_FALSE; for (unsigned int stage = 0; stage < sh_stages; ++stage) { glw::GLuint sh_id = va_arg(values, glw::GLuint); unsigned int sh_parts = va_arg(values, unsigned int); const char* const* sh_code = va_arg(values, const char* const*); if (sh_id == 0) { continue; } if (sh_parts != 0) { std::string sh_merged_string = specializeShader(sh_parts, sh_code); const char* sh_merged_ptr = sh_merged_string.c_str(); gl.shaderSource(sh_id, 1, &sh_merged_ptr, NULL); GLU_EXPECT_NO_ERROR(gl.getError(), "glShaderSource() failed!"); gl.compileShader(sh_id); GLU_EXPECT_NO_ERROR(gl.getError(), "glCompileShader() failed!"); gl.getShaderiv(sh_id, GL_COMPILE_STATUS, &compilation_status); GLU_EXPECT_NO_ERROR(gl.getError(), "glGetShaderiv() failed!"); if (compilation_status != GL_TRUE) { glw::GLint shader_type = 0; std::string info_log = getCompilationInfoLog(sh_id); gl.getShaderiv(sh_id, GL_SHADER_TYPE, &shader_type); std::string shader_type_str = getShaderTypeName(shader_type); m_testCtx.getLog() << tcu::TestLog::Message << shader_type_str << " compilation failure:\n\n" << info_log << "\n\n" << shader_type_str << " source:\n\n" << sh_merged_string << "\n\n" << tcu::TestLog::EndMessage; break; } } gl.attachShader(po_id, sh_id); GLU_EXPECT_NO_ERROR(gl.getError(), "glAttachShader(VERTEX_SHADER) call failed"); vec_sh_id.push_back(sh_id); } va_end(values); if (out_has_compilation_failed != NULL) { *out_has_compilation_failed = (compilation_status == GL_FALSE); } if (compilation_status != GL_TRUE) { return false; } /* Linking the program */ glw::GLint link_status = GL_FALSE; gl.linkProgram(po_id); GLU_EXPECT_NO_ERROR(gl.getError(), "glLinkProgram() failed!"); gl.getProgramiv(po_id, GL_LINK_STATUS, &link_status); GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramiv() failed!"); if (link_status != GL_TRUE) { /* Dump link log */ std::string link_log = getLinkingInfoLog(po_id); m_testCtx.getLog() << tcu::TestLog::Message << "Link failure:\n\n" << link_log << "\n\n" << tcu::TestLog::EndMessage; /* Dump shader source */ for (std::vector<glw::GLuint>::iterator it = vec_sh_id.begin(); it != vec_sh_id.end(); ++it) { glw::GLint shader_type = 0; gl.getShaderiv(*it, GL_SHADER_TYPE, &shader_type); std::string shader_type_str = getShaderTypeName(shader_type); std::string shader_source = getShaderSource(*it); m_testCtx.getLog() << tcu::TestLog::Message << shader_type_str << " source:\n\n" << shader_source << "\n\n" << tcu::TestLog::EndMessage; } return false; } return true; }
void RuntimeShaderEditor::Update() { timeval tval = {0, 100}; fd_set readset; FD_ZERO(&readset); FD_SET(m_socket, &readset); if(m_client) { FD_SET(m_client, &readset); } int no_events = select(m_socket > m_client ? (m_socket + 1) : m_client + 1, &readset, NULL, NULL, &tval); if(no_events == 0) { return; } // check for incomming connections if(FD_ISSET(m_socket, &readset)) { m_client = accept(m_socket, NULL, NULL); char rawMessage[20]; m_message.prepareMessage("RSE-ACK\r\n", rawMessage); send(m_client, rawMessage,strlen(rawMessage), 0); RSE_LOG("New connection received"); } if(FD_ISSET(m_client, &readset)) { char buffer[SHADER_BUFFER_SIZE]; int len = recv(m_client, buffer, SHADER_BUFFER_SIZE-1, 0); if(len < 1) { RSE_LOG("Connection lost!"); m_client = 0; } else { buffer[len] = 0x00; m_message.addData(buffer); if (m_message.isComplete()) { const char *msg = m_message.getMessage(); RSE_LOG("New command received: %s, len:%i", msg, len); if (strstr(msg, "getShaderSource")) { int program = 0; sscanf(msg, "getShaderSource(%i)\r\n", &program); RSE_LOG("Se ha solicitado el codigo del shader: %i", program); getShaderSource(program, buffer, SHADER_BUFFER_SIZE); char rawMessage[SHADER_BUFFER_SIZE * 2]; m_message.prepareMessage(buffer, rawMessage); send(m_client, rawMessage, strlen(rawMessage), 0); } else if (strstr(msg, "patchShader")) { int program = 0; int shaderType = 0; sscanf(msg, "patchShader(%i,%i)", &program, &shaderType); const char* aux = strstr(msg, ")"); if (shaderType == 0) { shaderType = GL_VERTEX_SHADER; } else { shaderType = GL_FRAGMENT_SHADER; } GLuint shaderHandler = PatchShader(program, &aux[1], shaderType); char log[1024]; char raw[1024]; int len = getCompilationError(log, 1024, shaderHandler); if(len == 0) m_message.prepareMessage("done\r\n", raw); else m_message.prepareMessage(log, raw); send(m_client, raw, strlen(raw), 0); } else if (strstr(msg, "getProgramList()")) { int len = m_existingPrograms.length() + 10; char *buff = new char[len]; m_message.prepareMessage(m_existingPrograms.c_str(), buff); send(m_client, buff, strlen(buff), 0); delete[] buff; } m_message.clear(); } } } }
void installShaders(void) { // // (1) Create shader objects // vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); // // (2) Load source code into shader objects. // const GLchar *vertexShaderSource = getShaderSource("vertex.vs"); const GLchar *fragmentShaderSource = getShaderSource("fragment.fs"); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); // // (3) Compile shaders. // glCompileShader(vertexShader); GLint success; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { GLchar infoLog[200]; GLint charsWritten; glGetShaderInfoLog(vertexShader, sizeof(infoLog), &charsWritten, infoLog); fprintf(stderr, "vertex shader info log:\n%s\n\n", infoLog); } checkOpenGLError(__LINE__); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { GLchar infoLog[200]; GLint charsWritten; glGetShaderInfoLog(fragmentShader, sizeof(infoLog), &charsWritten, infoLog); fprintf(stderr, "fragment shader info log:\n%s\n\n", infoLog); } checkOpenGLError(__LINE__); // // (4) Create program object and attach vertex and fragment shader. // program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); checkOpenGLError(__LINE__); // // (5) Link program. // glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &success); if (!success) { GLchar infoLog[200]; GLint charsWritten; glGetProgramInfoLog(program, sizeof(infoLog), &charsWritten, infoLog); fprintf(stderr, "program info log:\n%s\n\n", infoLog); } checkOpenGLError(__LINE__); // // (7) Get vertex attribute locations // vertexPositionAttr = glGetAttribLocation(program, "vertexPosition"); vertexNormalAttr = glGetAttribLocation(program, "vertexNormal"); // vertexTexCoordAttr = glGetAttribLocation(program, "vertexTexCoord"); if (vertexPositionAttr == -1 || vertexNormalAttr == -1) { fprintf(stderr, "Error fetching vertex position or normal attribute !\n"); } // // (8) Fetch handles for uniform variables in program. // ModelViewProjectionUniform = glGetUniformLocation(program, "ModelViewProjection"); if (ModelViewProjectionUniform == -1) { fprintf(stderr, "Error fetching modelViewProjectionUniform !\n"); // exit(-1); } ModelViewMatrixUniform = glGetUniformLocation(program, "ModelViewMatrix"); if (ModelViewMatrixUniform == -1) { fprintf(stderr, "Error fetching modelViewMatrixUniform!\n"); // exit(-1); } NormalMatrixUniform = glGetUniformLocation(program, "NormalMatrix"); if (NormalMatrixUniform == -1) { fprintf(stderr, "Error fetching normalMatrixUniform!\n"); // exit(-1); } ambientLightUniform = glGetUniformLocation(program, "ambientLight"); if (ambientLightUniform == -1) { fprintf(stderr, "Error fetching ambientLightUniform!\n"); exit(-1); } light0ColorUniform = glGetUniformLocation(program, "light0Color"); if (light0ColorUniform == -1) { fprintf(stderr, "Error fetching light0ColorUniform!\n"); // exit(-1); } light0PositionUniform = glGetUniformLocation(program, "light0Position"); if (light0PositionUniform == -1) { fprintf(stderr, "Error fetching light0PositionUniform!\n"); // exit(-1); } materialAmbientUniform = glGetUniformLocation(program, "materialAmbient"); if (materialAmbientUniform == -1) { fprintf(stderr, "Error fetching materialAmbientUniform!\n"); // exit(-1); } materialDiffuseUniform = glGetUniformLocation(program, "materialDiffuse"); if (materialDiffuseUniform == -1) { fprintf(stderr, "Error fetching materialDiffuseUniform!\n"); //exit(-1); } materialSpecularUniform = glGetUniformLocation(program, "materialSpecular"); materialShininessUniform = glGetUniformLocation(program, "materialShininess"); nearPlaneUniform = glGetUniformLocation(program, "nearPlane"); farPlaneUniform = glGetUniformLocation(program, "farPlane"); // // (9) Tell GL to use our program // glUseProgram(program); }
bool OsgProgram::getGeometryShaderSource(std::string* source) const { return getShaderSource(SHADER_TYPE_GEOMETRY, source); }
bool OsgProgram::getVertexShaderSource(std::string* source) const { return getShaderSource(SHADER_TYPE_VERTEX, source); }
bool OsgProgram::getFragmentShaderSource(std::string* source) const { return getShaderSource(SHADER_TYPE_FRAGMENT, source); }