void DiGLShaderParam::_BindTexture2Ds() { for (auto it = mShaderParams[VARIABLE_SAMPLER2D].begin(); it != mShaderParams[VARIABLE_SAMPLER2D].end(); ++it) { const DiAny& data = it->second; if (data.isEmpty()) continue; GLuint location = 0; int samplerUnit = 0; auto sampler = mShaderLinker->GetSampler(it->first); if (!sampler) continue; location = sampler->location; samplerUnit = (int)sampler->unit; glUniform1ivARB(location, 1, &samplerUnit); DiTexture* tex = any_cast<DiTexture*>(data); tex->Bind((uint32)samplerUnit); } }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_ARBShaderObjects_nglUniform1ivARB(JNIEnv *env, jclass clazz, jint location, jint count, jobject values, jint values_position, jlong function_pointer) { const GLint *values_address = ((const GLint *)(*env)->GetDirectBufferAddress(env, values)) + values_position; glUniform1ivARBPROC glUniform1ivARB = (glUniform1ivARBPROC)((intptr_t)function_pointer); glUniform1ivARB(location, count, values_address); }
void GLSLShader::SetIntVector(GLint variable, GLsizei count, const GLint *value) { if (variable!=-1) glUniform1ivARB(variable, count, value); }
void DiGLShaderParam::Bind() const { if (!mShaderLinker || !mShaderLinker->GetGLHandle()) return; // bind built-in (global) uniforms auto env = Driver->GetShaderEnvironment(); for (auto it = mBuiltinFuncs.begin(); it != mBuiltinFuncs.end(); ++it) { it->second(env, it->first); } // bind custom uniforms for (uint32 i = 0; i < NUM_VARIABLE_TYPES; ++i) { for (auto it = mShaderParams[i].begin(); it != mShaderParams[i].end(); ++it) { const DiAny& data = it->second; if (data.isEmpty()) continue; GLuint location = 0; int samplerUnit = 0; if (i == VARIABLE_SAMPLER2D || i == VARIABLE_SAMPLERCUBE) { auto sampler = mShaderLinker->GetSampler(it->first); if (!sampler) continue; location = sampler->location; samplerUnit = (int)sampler->unit; } else { auto constant = mShaderLinker->GetConstant(it->first); if (!constant) continue; location = constant->location; } switch (i) { case DiShaderParameter::VARIABLE_FLOAT: { float val = any_cast<float>(data); glUniform1fvARB(location, 1, &val); break; } case DiShaderParameter::VARIABLE_FLOAT2: { DiVec2 vec2 = any_cast<DiVec2>(data); glUniform2fvARB(location, 1, vec2.ptr()); break; } case DiShaderParameter::VARIABLE_FLOAT3: { DiVec3 vec3 = any_cast<DiVec3>(data); glUniform3fvARB(location, 1, vec3.ptr()); break; } case DiShaderParameter::VARIABLE_FLOAT4: { DiVec4 vec4 = any_cast<DiVec4>(data); glUniform4fvARB(location, 1, vec4.ptr()); break; } case DiShaderParameter::VARIABLE_MAT4: { DiMat4 vec4 = any_cast<DiMat4>(data); glUniformMatrix4fvARB(location, 1, GL_TRUE, vec4[0]); break; } case DiShaderParameter::VARIABLE_COLOR: { DiColor c = any_cast<DiColor>(data); DiVec4 vec4(c.r,c.g,c.b,c.a); glUniform4fvARB(location, 1, vec4.ptr()); break; } case DiShaderParameter::VARIABLE_FLOAT4_ARRAY: { DiPair<DiVec4*,uint32> v4Arr = any_cast<DiPair<DiVec4*,uint32>>(data); glUniform4fvARB(location, v4Arr.second, v4Arr.first->ptr()); break; } case DiShaderParameter::VARIABLE_SAMPLER2D: case DiShaderParameter::VARIABLE_SAMPLERCUBE: { glUniform1ivARB(location, 1, &samplerUnit); DiTexture* tex = any_cast<DiTexture*>(data); tex->Bind((uint32)samplerUnit); } break; } } } }
//----------------------------------------------------------------------- void GLSLLinkProgram::updateUniforms(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType) { // iterate through uniform reference list and update uniform values GLUniformReferenceIterator currentUniform = mGLUniformReferences.begin(); GLUniformReferenceIterator endUniform = mGLUniformReferences.end(); for (; currentUniform != endUniform; ++currentUniform) { // Only pull values from buffer it's supposed to be in (vertex or fragment) // This method will be called twice, once for vertex program params, // and once for fragment program params. if (fromProgType == currentUniform->mSourceProgType) { const GpuConstantDefinition* def = currentUniform->mConstantDef; if (def->variability & mask) { GLsizei glArraySize = (GLsizei)def->arraySize; // get the index in the parameter real list switch (def->constType) { case GCT_FLOAT1: glUniform1fvARB(currentUniform->mLocation, glArraySize, params->getFloatPointer(def->physicalIndex)); break; case GCT_FLOAT2: glUniform2fvARB(currentUniform->mLocation, glArraySize, params->getFloatPointer(def->physicalIndex)); break; case GCT_FLOAT3: glUniform3fvARB(currentUniform->mLocation, glArraySize, params->getFloatPointer(def->physicalIndex)); break; case GCT_FLOAT4: glUniform4fvARB(currentUniform->mLocation, glArraySize, params->getFloatPointer(def->physicalIndex)); break; case GCT_MATRIX_2X2: glUniformMatrix2fvARB(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); break; case GCT_MATRIX_2X3: if (GLEW_VERSION_2_1) { glUniformMatrix2x3fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_2X4: if (GLEW_VERSION_2_1) { glUniformMatrix2x4fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_3X2: if (GLEW_VERSION_2_1) { glUniformMatrix3x2fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_3X3: glUniformMatrix3fvARB(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); break; case GCT_MATRIX_3X4: if (GLEW_VERSION_2_1) { glUniformMatrix3x4fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_4X2: if (GLEW_VERSION_2_1) { glUniformMatrix4x2fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_4X3: if (GLEW_VERSION_2_1) { glUniformMatrix4x3fv(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); } break; case GCT_MATRIX_4X4: glUniformMatrix4fvARB(currentUniform->mLocation, glArraySize, GL_TRUE, params->getFloatPointer(def->physicalIndex)); break; case GCT_INT1: glUniform1ivARB(currentUniform->mLocation, glArraySize, (GLint*)params->getIntPointer(def->physicalIndex)); break; case GCT_INT2: glUniform2ivARB(currentUniform->mLocation, glArraySize, (GLint*)params->getIntPointer(def->physicalIndex)); break; case GCT_INT3: glUniform3ivARB(currentUniform->mLocation, glArraySize, (GLint*)params->getIntPointer(def->physicalIndex)); break; case GCT_INT4: glUniform4ivARB(currentUniform->mLocation, glArraySize, (GLint*)params->getIntPointer(def->physicalIndex)); break; case GCT_SAMPLER1D: case GCT_SAMPLER1DSHADOW: case GCT_SAMPLER2D: case GCT_SAMPLER2DSHADOW: case GCT_SAMPLER3D: case GCT_SAMPLERCUBE: // samplers handled like 1-element ints glUniform1ivARB(currentUniform->mLocation, 1, (GLint*)params->getIntPointer(def->physicalIndex)); break; case GCT_UNKNOWN: break; } // end switch #if OGRE_DEBUG_MODE checkForGLSLError( "GLSLLinkProgram::updateUniforms", "Error updating uniform", 0 ); #endif } // variability & mask } // fromProgType == currentUniform->mSourceProgType } // end for }
ShaderProgram &u1(const char *var, GLint *f) { glUniform1ivARB(uniform(var), 1, f); return *this; }
void MGLContext::sendUniformInt(unsigned int fxId, const char * name, int * values, const int count){ GLint uValue = glGetUniformLocationARB((GLhandleARB)fxId, name); if(uValue != -1) glUniform1ivARB(uValue, count, values); }
void pattern_render(struct pattern * pattern, GLuint input_tex) { GLenum e; glLoadIdentity(); glViewport(0, 0, config.pattern.master_width, config.pattern.master_height); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, pattern->fb); pattern->intensity_integral = fmod(pattern->intensity_integral + pattern->intensity / config.ui.fps, MAX_INTEGRAL); for (int i = pattern->n_shaders - 1; i >= 0; i--) { glUseProgramObjectARB(pattern->shader[i]); // Don't worry about this part. for(int j = 0; j < pattern->n_shaders; j++) { // Or, worry about it, but don't think about it. glActiveTexture(GL_TEXTURE1 + j); glBindTexture(GL_TEXTURE_2D, pattern->tex[(pattern->flip + j + (i < j)) % (pattern->n_shaders + 1)]); } glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, pattern->tex[(pattern->flip + i + 1) % (pattern->n_shaders + 1)], 0); if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e)); GLint loc; loc = glGetUniformLocationARB(pattern->shader[i], "iTime"); glUniform1fARB(loc, time_master.beat_frac + time_master.beat_index); loc = glGetUniformLocationARB(pattern->shader[i], "iAudioHi"); glUniform1fARB(loc, audio_hi); loc = glGetUniformLocationARB(pattern->shader[i], "iAudioMid"); glUniform1fARB(loc, audio_mid); loc = glGetUniformLocationARB(pattern->shader[i], "iAudioLow"); glUniform1fARB(loc, audio_low); loc = glGetUniformLocationARB(pattern->shader[i], "iAudioLevel"); glUniform1fARB(loc, audio_level); loc = glGetUniformLocationARB(pattern->shader[i], "iResolution"); glUniform2fARB(loc, config.pattern.master_width, config.pattern.master_height); loc = glGetUniformLocationARB(pattern->shader[i], "iIntensity"); glUniform1fARB(loc, pattern->intensity); loc = glGetUniformLocationARB(pattern->shader[i], "iIntensityIntegral"); glUniform1fARB(loc, pattern->intensity_integral); loc = glGetUniformLocationARB(pattern->shader[i], "iFPS"); glUniform1fARB(loc, config.ui.fps); loc = glGetUniformLocationARB(pattern->shader[i], "iFrame"); glUniform1iARB(loc, 0); loc = glGetUniformLocationARB(pattern->shader[i], "iChannel"); glUniform1ivARB(loc, pattern->n_shaders, pattern->uni_tex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, input_tex); if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e)); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glVertex2d(-1, -1); glVertex2d(-1, 1); glVertex2d(1, 1); glVertex2d(1, -1); glEnd(); if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e)); } pattern->flip = (pattern->flip + 1) % (pattern->n_shaders + 1); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); if((e = glGetError()) != GL_NO_ERROR) FAIL("OpenGL error: %s\n", gluErrorString(e)); pattern->tex_output = pattern->tex[pattern->flip]; }
bool r_create_shaders(void) { int i, frames[1 + FRAME_TRACE]; OPENGL_EVENT_BEGIN(0, __PRETTY_FUNCTION__); // create the terrain GPU program if (!r_create_program("Terrain", TERRAIN_VS, TERRAIN_FS, &r_ter_vs, &r_ter_fs, &r_ter_prog)) { OPENGL_EVENT_END(); return false; } // create the compositor GPU program if (!r_create_program("Compositor", COMPOSITOR_VS, (m_compatshader ? COMPOSITOR_COMPAT_FS : COMPOSITOR_FS), &r_comp_vs, &r_comp_fs, &r_comp_prog)) { // try the compat shader before definitely failing if (!m_compatshader) { fprintf(stderr, "Failed compositor shader compilation, " "falling back to compat\n"); if (!r_create_program("Compositor", COMPOSITOR_VS, COMPOSITOR_COMPAT_FS, &r_comp_vs, &r_comp_fs, &r_comp_prog)) { OPENGL_EVENT_END(); return false; } } else { OPENGL_EVENT_END(); return false; } } // create the font GPU program if (!r_create_program("Font", COMPOSITOR_VS, FONT_FS, &r_font_vs, &r_font_fs, &r_font_prog)) { OPENGL_EVENT_END(); return false; } // create the prop GPU program if (!r_create_program("Prop", PROP_VS, PROP_FS, &r_prop_vs, &r_prop_fs, &r_prop_prog)) { OPENGL_EVENT_END(); return false; } // create the sprite GPU program if (!r_create_program("Sprite", SPRITE_VS, SPRITE_FS, &r_sprite_vs, &r_sprite_fs, &r_sprite_prog)) { OPENGL_EVENT_END(); return false; } // create the footmobile GPU program if (!r_create_program("Footmobile", FOOTMOBILE_VS, FONT_FS, &r_fmb_vs, &r_fmb_fs, &r_fmb_prog)) { OPENGL_EVENT_END(); return false; } // set the terrain shader up glUseProgramObjectARB(r_ter_prog); if ((i = glGetUniformLocationARB(r_ter_prog, "terTex")) < 0) { fprintf(stderr, "Failed to find terrain texture uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); if ((i = glGetUniformLocationARB(r_ter_prog, "constParams")) < 0) { fprintf(stderr, "Failed to find constant params uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform2fARB(i, HEIGHTMAP_SIZE, HEIGHT_SCALE); if ((r_ter_patch_params = glGetUniformLocationARB(r_ter_prog, "patchParams")) < 0) { fprintf(stderr, "Failed to find per-patch params uniform variable\n"); OPENGL_EVENT_END(); return false; } if ((r_ter_height_samples = glGetUniformLocationARB(r_ter_prog, "heightSamples")) < 0) { fprintf(stderr, "Failed to find height samples uniform variable\n"); OPENGL_EVENT_END(); return false; } // set the prop shader up glUseProgramObjectARB(r_prop_prog); if ((i = glGetUniformLocationARB(r_prop_prog, "propTex")) < 0) { fprintf(stderr, "Failed to find prop texture uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); // set the sprite shader up glUseProgramObjectARB(r_sprite_prog); if ((i = glGetUniformLocationARB(r_sprite_prog, "spriteTex")) < 0) { fprintf(stderr, "Failed to find sprite texture uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); // set the footmobile shader up glUseProgramObjectARB(r_fmb_prog); if ((i = glGetUniformLocationARB(r_fmb_prog, "fontTex")) < 0) { fprintf(stderr, "Failed to find footmobile texture uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); // set the font shader up glUseProgramObjectARB(r_font_prog); if ((i = glGetUniformLocationARB(r_font_prog, "fontTex")) < 0) { fprintf(stderr, "Failed to find font texture uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); // find uniform locations glUseProgramObjectARB(r_comp_prog); if ((i = glGetUniformLocationARB(r_comp_prog, "overlay")) < 0) { fprintf(stderr, "Failed to find overlay uniform variable\n"); OPENGL_EVENT_END(); return false; } glUniform1iARB(i, 0); if ((r_comp_frames = glGetUniformLocationARB(r_comp_prog, "frames")) < 0) { fprintf(stderr, "Failed to find frames uniform variable\n"); OPENGL_EVENT_END(); return false; } if ((r_comp_neg = glGetUniformLocationARB(r_comp_prog, "negative")) < 0) { fprintf(stderr, "Failed to find negative uniform variable\n"); OPENGL_EVENT_END(); return false; } if ((r_comp_contrast = glGetUniformLocationARB(r_comp_prog, "cont")) < 0) { fprintf(stderr, "Failed to find contrast uniform variable\n"); OPENGL_EVENT_END(); return false; } // fill the frames array; frames at GL_TEXTURE1 + i for (i = 0; i < 1 + FRAME_TRACE; i++) frames[i] = i + 1; glUniform1ivARB(r_comp_frames, 1 + FRAME_TRACE, frames); glUseProgramObjectARB(0); OPENGL_EVENT_END(); return true; }