809 int perm[256]= {151,160,137,91,90,15,
810 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
811 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
812 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
813 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
814 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
815 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
816 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
817 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
818 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
819 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
820 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
821 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
823 int grad3[16][3] = {{0,1, 1},{ 0, 1,-1},{ 0,-1,1},{ 0,-1,-1},
824 {1,0, 1},{ 1, 0,-1},{-1, 0,1},{-1, 0,-1},
825 {1,1, 0},{ 1,-1, 0},{-1, 1,0},{-1,-1, 0},
826 {1,0,-1},{-1, 0,-1},{ 0,-1,1},{ 0, 1, 1}};
831 pixels = (
char*)malloc( 256*256*4 );
832 for(i = 0; i<256; i++)
833 for(j = 0; j<256; j++) {
834 int offset = (i*256+j)*4;
835 char value =
perm[(j+
perm[i]) & 0xFF];
836 pixels[offset+2] = grad3[value & 0x0F][0] * 64 + 64;
837 pixels[offset+1] = grad3[value & 0x0F][1] * 64 + 64;
838 pixels[offset+0] = grad3[value & 0x0F][2] * 64 + 64;
839 pixels[offset+3] = value;
842 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels );
843 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
844 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
845 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
846 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
863 int perm[256]= {151,160,137,91,90,15,
864 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
865 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
866 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
867 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
868 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
869 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
870 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
871 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
872 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
873 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
874 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
875 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
877 int grad4[32][4]= {{ 0, 1,1,1}, { 0, 1, 1,-1}, { 0, 1,-1,1}, { 0, 1,-1,-1},
878 { 0,-1,1,1}, { 0,-1, 1,-1}, { 0,-1,-1,1}, { 0,-1,-1,-1},
879 { 1, 0,1,1}, { 1, 0, 1,-1}, { 1, 0,-1,1}, { 1, 0,-1,-1},
880 {-1, 0,1,1}, {-1, 0, 1,-1}, {-1, 0,-1,1}, {-1, 0,-1,-1},
881 { 1, 1,0,1}, { 1, 1, 0,-1}, { 1,-1, 0,1}, { 1,-1, 0,-1},
882 {-1, 1,0,1}, {-1, 1, 0,-1}, {-1,-1, 0,1}, {-1,-1, 0,-1},
883 { 1, 1,1,0}, { 1, 1,-1, 0}, { 1,-1, 1,0}, { 1,-1,-1, 0},
884 {-1, 1,1,0}, {-1, 1,-1, 0}, {-1,-1, 1,0}, {-1,-1,-1, 0}};
889 pixels = (
char*)malloc( 256*256*4 );
890 for(i = 0; i<256; i++)
891 for(j = 0; j<256; j++) {
892 int offset = (i*256+j)*4;
893 char value =
perm[(j+
perm[i]) & 0xFF];
894 pixels[offset+2] = grad4[value & 0x1F][0] * 64 + 64;
895 pixels[offset+1] = grad4[value & 0x1F][1] * 64 + 64;
896 pixels[offset+0] = grad4[value & 0x1F][2] * 64 + 64;
897 pixels[offset+3] = grad4[value & 0x1F][3] * 64 + 64;
900 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels );
901 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
902 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
903 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
904 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
941 dispatch_semaphore_wait((dispatch_semaphore_t)shader->lock, DISPATCH_TIME_FOREVER);
945 dispatch_semaphore_signal((dispatch_semaphore_t)shader->lock);
953 CGLContextObj cgl_ctx = (CGLContextObj)glContext;
955 bool alreadyActiveOnThisContext =
false;
961 if (i->second == program->program.programName)
962 alreadyActiveOnThisContext =
true;
964 i->second = program->program.programName;
971 if (!alreadyActiveOnThisContext)
975 glValidateProgram(program->program.programName);
977 int infologLength = 0;
978 glGetProgramiv(program->program.programName, GL_INFO_LOG_LENGTH, &infologLength);
979 if (infologLength > 0)
981 char *infoLog = (
char *)malloc(infologLength);
982 int charsWritten = 0;
983 glGetProgramInfoLog(program->program.programName, infologLength, &charsWritten, infoLog);
989 glUseProgram(program->program.programName);
992 GLuint textureUnit = 0;
993 bool explicitColorBuffer =
false;
994 for (
unsigned int i = 0; i < shader->uniformsCount; ++i)
999 if (strncmp(uniform.type,
"VuoList_", 8) == 0)
1004 VDebugLog(
"Warning: Shader '%s' has a value for '%s', but the linked program has no uniform by that name.", shader->name, uniform.name);
1009 if (strcmp(uniform.type,
"VuoList_VuoBoolean") == 0)
1013 GLint *itemDataGL = (GLint *)malloc(
sizeof(GLint) * itemCount);
1014 for (
size_t i = 0; i < itemCount; ++i)
1015 itemDataGL[i] = itemData[i];
1016 glUniform1iv(location, itemCount, itemDataGL);
1018 else if (strcmp(uniform.type,
"VuoList_VuoInteger") == 0)
1022 GLint *itemDataGL = (GLint *)malloc(
sizeof(GLint) * itemCount);
1023 for (
size_t i = 0; i < itemCount; ++i)
1024 itemDataGL[i] = itemData[i];
1025 glUniform1iv(location, itemCount, itemDataGL);
1027 else if (strcmp(uniform.type,
"VuoList_VuoReal") == 0)
1031 GLfloat *itemDataGL = (GLfloat *)malloc(
sizeof(GLfloat) * itemCount);
1032 for (
size_t i = 0; i < itemCount; ++i)
1033 itemDataGL[i] = itemData[i];
1034 glUniform1fv(location, itemCount, itemDataGL);
1036 else if (strcmp(uniform.type,
"VuoList_VuoPoint2d") == 0)
1040 GLfloat *itemDataGL = (GLfloat *)malloc(
sizeof(GLfloat) * itemCount * 2);
1041 for (
size_t i = 0; i < itemCount; ++i)
1043 itemDataGL[i*2 + 0] = itemData[i].x;
1044 itemDataGL[i*2 + 1] = itemData[i].y;
1046 glUniform2fv(location, itemCount, itemDataGL);
1048 else if (strcmp(uniform.type,
"VuoList_VuoPoint3d") == 0)
1052 GLfloat *itemDataGL = (GLfloat *)malloc(
sizeof(GLfloat) * itemCount * 3);
1053 for (
size_t i = 0; i < itemCount; ++i)
1055 itemDataGL[i*3 + 0] = itemData[i].x;
1056 itemDataGL[i*3 + 1] = itemData[i].y;
1057 itemDataGL[i*3 + 2] = itemData[i].z;
1059 glUniform3fv(location, itemCount, itemDataGL);
1061 else if (strcmp(uniform.type,
"VuoList_VuoPoint4d") == 0)
1065 GLfloat *itemDataGL = (GLfloat *)malloc(
sizeof(GLfloat) * itemCount * 4);
1066 for (
size_t i = 0; i < itemCount; ++i)
1068 itemDataGL[i*4 + 0] = itemData[i].x;
1069 itemDataGL[i*4 + 1] = itemData[i].y;
1070 itemDataGL[i*4 + 2] = itemData[i].z;
1071 itemDataGL[i*4 + 3] = itemData[i].w;
1073 glUniform4fv(location, itemCount, itemDataGL);
1075 else if (strcmp(uniform.type,
"VuoList_VuoColor") == 0)
1079 GLfloat *itemDataGL = (GLfloat *)malloc(
sizeof(GLfloat) * itemCount * 4);
1080 for (
size_t i = 0; i < itemCount; ++i)
1082 itemDataGL[i*4 + 0] = itemData[i].r;
1083 itemDataGL[i*4 + 1] = itemData[i].g;
1084 itemDataGL[i*4 + 2] = itemData[i].b;
1085 itemDataGL[i*4 + 3] = itemData[i].a;
1087 glUniform4fv(location, itemCount, itemDataGL);
1090 VDebugLog(
"Warning: Shader '%s' has unknown type '%s' for uniform '%s'.", shader->name, uniform.type, uniform.name);
1093 if (listLengthUniform != -1)
1094 glUniform1i(listLengthUniform, itemCount);
1101 if (strcmp(uniform.type,
"VuoImage") == 0 && uniform.value.image)
1103 VuoImage image = uniform.value.image;
1107 if (imgRectUniform != -1)
1109 if (image->glTextureTarget == GL_TEXTURE_2D)
1110 glUniform4f(imgRectUniform, 0, 0, 1, 1);
1112 glUniform4f(imgRectUniform, 0, 0, image->pixelsWide, image->pixelsHigh);
1116 if (imgSizeUniform != -1)
1117 glUniform2f(imgSizeUniform, image->pixelsWide, image->pixelsHigh);
1121 if (flipUniform != -1)
1122 glUniform1i(flipUniform, 0);
1128 VDebugLog(
"Warning: Shader '%s' has a value for '%s', but the linked program has no uniform by that name.", shader->name, uniform.name);
1132 if (strcmp(uniform.type,
"VuoImage") == 0)
1134 VuoImage image = uniform.value.image;
1136 glActiveTexture(GL_TEXTURE0 + textureUnit);
1138 glBindTexture(image->glTextureTarget, image->glTextureName);
1146 glBindTexture(GL_TEXTURE_2D, 0);
1147 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
1149 glUniform1i(location, textureUnit);
1152 if (strcmp(uniform.name,
"colorBuffer") == 0)
1153 explicitColorBuffer =
true;
1155 else if (strcmp(uniform.type,
"VuoBoolean") == 0)
1156 glUniform1i(location, uniform.value.boolean);
1157 else if (strcmp(uniform.type,
"VuoInteger") == 0)
1158 glUniform1i(location, uniform.value.integer);
1159 else if (strcmp(uniform.type,
"VuoReal") == 0)
1160 glUniform1f(location, uniform.value.real);
1161 else if (strcmp(uniform.type,
"VuoPoint2d") == 0)
1162 glUniform2f(location, uniform.value.point2d.x, uniform.value.point2d.y);
1163 else if (strcmp(uniform.type,
"VuoPoint3d") == 0)
1164 glUniform3f(location, uniform.value.point3d.x, uniform.value.point3d.y, uniform.value.point3d.z);
1165 else if (strcmp(uniform.type,
"VuoPoint4d") == 0)
1166 glUniform4f(location, uniform.value.point4d.x, uniform.value.point4d.y, uniform.value.point4d.z, uniform.value.point4d.w);
1167 else if (strcmp(uniform.type,
"VuoColor") == 0)
1168 glUniform4f(location, uniform.value.color.r, uniform.value.color.g, uniform.value.color.b, uniform.value.color.a);
1169 else if (strcmp(uniform.type,
"mat2") == 0)
1170 glUniformMatrix2fv(location, 1, GL_FALSE, uniform.value.mat2);
1171 else if (strcmp(uniform.type,
"mat3") == 0)
1172 glUniformMatrix3fv(location, 1, GL_FALSE, uniform.value.mat3);
1173 else if (strcmp(uniform.type,
"mat4") == 0)
1174 glUniformMatrix4fv(location, 1, GL_FALSE, uniform.value.mat4);
1176 VUserLog(
"Error: Unknown type %s for '%s'", uniform.type, uniform.name);
1181 if (perlinTextureUniform != -1)
1183 glActiveTexture(GL_TEXTURE0 + textureUnit);
1185 static dispatch_once_t initPerlinTexture = 0;
1186 dispatch_once(&initPerlinTexture, ^{
1191 glUniform1i(perlinTextureUniform, textureUnit);
1196 if (gradTextureUniform != -1)
1198 glActiveTexture(GL_TEXTURE0 + textureUnit);
1200 static dispatch_once_t initGradTexture = 0;
1201 dispatch_once(&initGradTexture, ^{
1206 glUniform1i(gradTextureUniform, textureUnit);
1214 GLint width, height;
1215 if ((!explicitColorBuffer && colorBufferUniform != -1) || depthBufferUniform != -1)
1218 glGetIntegerv(GL_VIEWPORT, viewport);
1219 width = viewport[2];
1220 height = viewport[3];
1223 if (!explicitColorBuffer && colorBufferUniform != -1)
1226 glActiveTexture(GL_TEXTURE0 + textureUnit);
1228 GLuint colorBufferTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, NULL);
1229 glBindTexture(GL_TEXTURE_2D, colorBufferTexture);
1231 GLint multisampling, multisampledFramebuffer;
1232 glGetIntegerv(GL_SAMPLE_BUFFERS, &multisampling);
1233 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &multisampledFramebuffer);
1234 if (multisampling && multisampledFramebuffer)
1236 GLuint resolvedFramebuffer;
1237 glGenFramebuffers(1, &resolvedFramebuffer);
1238 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolvedFramebuffer);
1241 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBufferTexture, 0);
1243 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, multisampledFramebuffer);
1244 glReadBuffer(GL_COLOR_ATTACHMENT0);
1245 glDrawBuffer(GL_COLOR_ATTACHMENT0);
1246 glBlitFramebuffer(0, 0, width, height,
1247 0, 0, width, height,
1248 GL_COLOR_BUFFER_BIT,
1251 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
1254 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, multisampledFramebuffer);
1255 glDeleteFramebuffers(1, &resolvedFramebuffer);
1258 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
1260 shader->colorBuffer =
VuoImage_make(colorBufferTexture, GL_RGBA, width, height);
1263 glUniform1i(colorBufferUniform, textureUnit);
1267 if (depthBufferUniform != -1)
1270 glActiveTexture(GL_TEXTURE0 + textureUnit);
1272 GLuint depthBufferTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, GL_TEXTURE_2D, GL_DEPTH_COMPONENT16, width, height, GL_DEPTH_COMPONENT, NULL);
1273 glBindTexture(GL_TEXTURE_2D, depthBufferTexture);
1276 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 0, 0, width, height, 0);
1278 shader->depthBuffer =
VuoImage_make(depthBufferTexture, GL_DEPTH_COMPONENT16, width, height);
1281 glUniform1i(depthBufferUniform, textureUnit);
1292 if (timedeltaUniform != -1)
1293 glUniform1f(timedeltaUniform, now - shader->lastActivationTime);
1294 shader->lastActivationTime = now;
1297 if (dateUniform != -1)
1306 glUniform4f(dateUniform, (
float)year, (
float)month, (
float)dayOfMonth, (
float)(second + (minute + (hour * 60)) * 60));
1310 if (frameindexUniform != -1)
1311 glUniform1i(frameindexUniform, shader->activationCount);
1312 ++shader->activationCount;
1317 *outputProgram = program->program;