1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 const char kDrawTexOESGles2_vshader[] = R"( 18 precision highp float; 19 attribute highp vec3 pos; 20 attribute highp vec2 texcoord; 21 varying highp vec2 texcoord_varying; 22 void main() { 23 gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); 24 texcoord_varying = texcoord; 25 } 26 )"; 27 28 const char kDrawTexOESGles2_fshader[] = R"( 29 precision highp float; 30 uniform sampler2D tex_sampler; 31 varying highp vec2 texcoord_varying; 32 void main() { 33 gl_FragColor = texture2D(tex_sampler, texcoord_varying); 34 } 35 )"; 36 37 const char kDrawTexOESCore_vshader[] = R"(#version 330 core 38 layout(location = 0) in vec3 pos; 39 layout(location = 1) in vec2 texcoord; 40 out vec2 texcoord_varying; 41 void main() { 42 gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); 43 texcoord_varying = texcoord; 44 } 45 )"; 46 47 const char kDrawTexOESCore_fshader[] = R"(#version 330 core 48 uniform sampler2D tex_sampler; 49 in vec2 texcoord_varying; 50 out vec4 frag_color; 51 void main() { 52 frag_color = texture(tex_sampler, texcoord_varying); 53 } 54 )"; 55 56 // version, flat, 57 const char kGeometryDrawVShaderSrcTemplateCore[] = R"(%s 58 layout(location = 0) in vec4 pos; 59 layout(location = 1) in vec3 normal; 60 layout(location = 2) in vec4 color; 61 layout(location = 3) in float pointsize; 62 layout(location = 4) in vec4 texcoord; 63 64 uniform mat4 projection; 65 uniform mat4 modelview; 66 uniform mat4 modelview_invtr; 67 uniform mat4 texture_matrix; 68 69 uniform bool enable_rescale_normal; 70 uniform bool enable_normalize; 71 72 out vec4 pos_varying; 73 out vec3 normal_varying; 74 %s out vec4 color_varying; 75 out float pointsize_varying; 76 out vec4 texcoord_varying; 77 78 void main() { 79 80 pos_varying = modelview * pos; 81 mat3 mvInvTr3 = mat3(modelview_invtr); 82 normal_varying = mvInvTr3 * normal; 83 84 if (enable_rescale_normal) { 85 float rescale = 1.0; 86 vec3 rescaleVec = vec3(mvInvTr3[2]); 87 float len = length(rescaleVec); 88 if (len > 0.0) { 89 rescale = 1.0 / len; 90 } 91 normal_varying *= rescale; 92 } 93 94 if (enable_normalize) { 95 normal_varying = normalize(normal_varying); 96 } 97 98 color_varying = color; 99 pointsize_varying = pointsize; 100 texcoord_varying = texture_matrix * texcoord; 101 102 gl_Position = projection * modelview * pos; 103 } 104 )"; 105 106 // version, flat, 107 const char kGeometryDrawFShaderSrcTemplateCore[] = R"(%s 108 // Defines 109 #define kMaxLights 8 110 111 #define kModulate 0x2100 112 #define kCombine 0x8570 113 #define kReplace 0x1E01 114 115 #define kAlpha 0x1906 116 #define kRGB 0x1907 117 #define kRGBA 0x1908 118 #define kLuminance 0x1909 119 #define kLuminanceAlpha 0x190A 120 121 #define kLinear 0x2601 122 #define kExp 0x0800 123 #define kExp2 0x0801 124 125 precision highp float; 126 uniform sampler2D tex_sampler; 127 uniform samplerCube tex_cube_sampler; 128 uniform bool enable_textures; 129 uniform bool enable_lighting; 130 uniform bool enable_color_material; 131 uniform bool enable_fog; 132 uniform bool enable_reflection_map; 133 134 uniform int texture_env_mode; 135 uniform int texture_format; 136 137 // material (front+back) 138 uniform vec4 material_ambient; 139 uniform vec4 material_diffuse; 140 uniform vec4 material_specular; 141 uniform vec4 material_emissive; 142 uniform float material_specular_exponent; 143 144 // lights 145 uniform vec4 light_model_scene_ambient; 146 uniform bool light_model_two_sided; 147 148 uniform bool light_enables[kMaxLights]; 149 uniform vec4 light_ambients[kMaxLights]; 150 uniform vec4 light_diffuses[kMaxLights]; 151 uniform vec4 light_speculars[kMaxLights]; 152 uniform vec4 light_positions[kMaxLights]; 153 uniform vec3 light_directions[kMaxLights]; 154 uniform float light_spotlight_exponents[kMaxLights]; 155 uniform float light_spotlight_cutoff_angles[kMaxLights]; 156 uniform float light_attenuation_consts[kMaxLights]; 157 uniform float light_attenuation_linears[kMaxLights]; 158 uniform float light_attenuation_quadratics[kMaxLights]; 159 160 // fog 161 uniform int fog_mode; 162 uniform float fog_density; 163 uniform float fog_start; 164 uniform float fog_end; 165 uniform vec4 fog_color; 166 167 in vec4 pos_varying; 168 in vec3 normal_varying; 169 %s in vec4 color_varying; 170 in float pointsize_varying; 171 in vec4 texcoord_varying; 172 173 out vec4 frag_color; 174 175 float posDot(vec3 a, vec3 b) { 176 return max(dot(a, b), 0.0); 177 } 178 179 void main() { 180 vec4 currentColor; 181 182 if (enable_textures) { 183 vec4 textureColor; 184 if (enable_reflection_map) { 185 textureColor = texture(tex_cube_sampler, reflect(pos_varying.xyz, normalize(normal_varying))); 186 currentColor = textureColor; 187 } else { 188 textureColor = texture(tex_sampler, texcoord_varying.xy); 189 if (texture_format == kAlpha) { 190 currentColor.rgb = color_varying.rgb; 191 if (texture_env_mode == kReplace) { 192 currentColor.a = textureColor.a; 193 } else { 194 currentColor.a = color_varying.a * textureColor.a; 195 } 196 } 197 if (texture_format == kRGBA || texture_format == kLuminanceAlpha) { 198 if (texture_env_mode == kReplace) { 199 currentColor.rgba = textureColor.rgba; 200 } else { 201 currentColor.rgba = color_varying.rgba * textureColor.rgba; 202 } 203 } else { 204 if (texture_env_mode == kReplace) { 205 currentColor.rgb = textureColor.rgb; 206 } else { 207 currentColor.rgb = color_varying.rgb * textureColor.rgb; 208 } 209 currentColor.a = color_varying.a; 210 } 211 } 212 } else { 213 currentColor = color_varying; 214 } 215 216 if (enable_lighting) { 217 218 vec4 materialAmbientActual = material_ambient; 219 vec4 materialDiffuseActual = material_diffuse; 220 221 if (enable_color_material || enable_textures) { 222 materialAmbientActual = currentColor; 223 materialDiffuseActual = currentColor; 224 } 225 226 vec4 lit = material_emissive + 227 materialAmbientActual * light_model_scene_ambient; 228 229 for (int i = 0; i < kMaxLights; i++) { 230 231 if (!light_enables[i]) continue; 232 233 vec4 lightAmbient = light_ambients[i]; 234 vec4 lightDiffuse = light_diffuses[i]; 235 vec4 lightSpecular = light_speculars[i]; 236 vec4 lightPos = light_positions[i]; 237 vec3 lightDir = light_directions[i]; 238 float attConst = light_attenuation_consts[i]; 239 float attLinear = light_attenuation_linears[i]; 240 float attQuadratic = light_attenuation_quadratics[i]; 241 float spotAngle = light_spotlight_cutoff_angles[i]; 242 float spotExponent = light_spotlight_exponents[i]; 243 244 vec3 toLight; 245 if (lightPos.w == 0.0) { 246 toLight = lightPos.xyz; 247 } else { 248 toLight = (lightPos.xyz / lightPos.w - pos_varying.xyz); 249 } 250 251 float lightDist = length(toLight); 252 vec3 h = normalize(toLight) + vec3(0.0, 0.0, 1.0); 253 float ndotL = posDot(normal_varying, normalize(toLight)); 254 float ndoth = posDot(normal_varying, normalize(h)); 255 256 float specAtt; 257 258 if (ndotL != 0.0) { 259 specAtt = 1.0; 260 } else { 261 specAtt = 0.0; 262 } 263 264 float att; 265 266 if (lightPos.w != 0.0) { 267 float attDenom = (attConst + attLinear * lightDist + 268 attQuadratic * lightDist * lightDist); 269 att = 1.0 / attDenom; 270 } else { 271 att = 1.0; 272 } 273 274 float spot; 275 276 float spotAngleCos = cos(radians(spotAngle)); 277 vec3 toSurfaceDir = -normalize(toLight); 278 float spotDot = posDot(toSurfaceDir, normalize(lightDir)); 279 280 if (spotAngle == 180.0 || lightPos.w == 0.0) { 281 spot = 1.0; 282 } else { 283 if (spotDot < spotAngleCos) { 284 spot = 0.0; 285 } else { 286 spot = pow(spotDot, spotExponent); 287 } 288 } 289 290 vec4 contrib = materialAmbientActual * lightAmbient; 291 contrib += ndotL * materialDiffuseActual * lightDiffuse; 292 if (ndoth > 0.0 && material_specular_exponent > 0.0) { 293 contrib += specAtt * pow(ndoth, material_specular_exponent) * 294 material_specular * lightSpecular; 295 } else { 296 if (ndoth > 0.0) { 297 contrib += specAtt * material_specular * lightSpecular; 298 } 299 } 300 contrib *= att * spot; 301 lit += contrib; 302 } 303 304 currentColor = lit; 305 306 } 307 308 if (enable_fog) { 309 310 float eyeDist = -pos_varying.z / pos_varying.w; 311 float f = 1.0; 312 switch (fog_mode) { 313 case kExp: 314 f = exp(-fog_density * eyeDist); 315 break; 316 case kExp2: 317 f = exp(-(pow(fog_density * eyeDist, 2.0))); 318 break; 319 case kLinear: 320 f = (fog_end - eyeDist) / (fog_end - fog_start); 321 break; 322 default: 323 break; 324 } 325 326 currentColor = f * currentColor + (1.0 - f) * fog_color; 327 328 } 329 330 frag_color = currentColor; 331 } 332 )"; 333