WebGLPrograms.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { BackSide, DoubleSide, FlatShading, CubeUVRefractionMapping, CubeUVReflectionMapping, GammaEncoding, LinearEncoding } from '../../constants';
  5. import { WebGLProgram } from './WebGLProgram';
  6. function WebGLPrograms( renderer, capabilities ) {
  7. var programs = [];
  8. var shaderIDs = {
  9. MeshDepthMaterial: 'depth',
  10. MeshNormalMaterial: 'normal',
  11. MeshBasicMaterial: 'basic',
  12. MeshLambertMaterial: 'lambert',
  13. MeshPhongMaterial: 'phong',
  14. MeshToonMaterial: 'phong',
  15. MeshStandardMaterial: 'physical',
  16. MeshPhysicalMaterial: 'physical',
  17. LineBasicMaterial: 'basic',
  18. LineDashedMaterial: 'dashed',
  19. PointsMaterial: 'points'
  20. };
  21. var parameterNames = [
  22. "precision", "supportsVertexTextures", "map", "mapEncoding", "envMap", "envMapMode", "envMapEncoding",
  23. "lightMap", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "displacementMap", "specularMap",
  24. "roughnessMap", "metalnessMap", "gradientMap",
  25. "alphaMap", "combine", "vertexColors", "fog", "useFog", "fogExp",
  26. "flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
  27. "maxBones", "useVertexTexture", "morphTargets", "morphNormals",
  28. "maxMorphTargets", "maxMorphNormals", "premultipliedAlpha",
  29. "numDirLights", "numPointLights", "numSpotLights", "numHemiLights", "numRectAreaLights",
  30. "shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights',
  31. "alphaTest", "doubleSided", "flipSided", "numClippingPlanes", "numClipIntersection", "depthPacking", "dithering"
  32. ];
  33. function allocateBones( object ) {
  34. var skeleton = object.skeleton;
  35. var bones = skeleton.bones;
  36. if ( capabilities.floatVertexTextures ) {
  37. return 1024;
  38. } else {
  39. // default for when object is not specified
  40. // ( for example when prebuilding shader to be used with multiple objects )
  41. //
  42. // - leave some extra space for other uniforms
  43. // - limit here is ANGLE's 254 max uniform vectors
  44. // (up to 54 should be safe)
  45. var nVertexUniforms = capabilities.maxVertexUniforms;
  46. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  47. var maxBones = Math.min( nVertexMatrices, bones.length );
  48. if ( maxBones < bones.length ) {
  49. console.warn( 'THREE.WebGLRenderer: Skeleton has ' + bones.length + ' bones. This GPU supports ' + maxBones + '.' );
  50. return 0;
  51. }
  52. return maxBones;
  53. }
  54. }
  55. function getTextureEncodingFromMap( map, gammaOverrideLinear ) {
  56. var encoding;
  57. if ( ! map ) {
  58. encoding = LinearEncoding;
  59. } else if ( map.isTexture ) {
  60. encoding = map.encoding;
  61. } else if ( map.isWebGLRenderTarget ) {
  62. console.warn( "THREE.WebGLPrograms.getTextureEncodingFromMap: don't use render targets as textures. Use their .texture property instead." );
  63. encoding = map.texture.encoding;
  64. }
  65. // add backwards compatibility for WebGLRenderer.gammaInput/gammaOutput parameter, should probably be removed at some point.
  66. if ( encoding === LinearEncoding && gammaOverrideLinear ) {
  67. encoding = GammaEncoding;
  68. }
  69. return encoding;
  70. }
  71. this.getParameters = function ( material, lights, fog, nClipPlanes, nClipIntersection, object ) {
  72. var shaderID = shaderIDs[ material.type ];
  73. // heuristics to create shader parameters according to lights in the scene
  74. // (not to blow over maxLights budget)
  75. var maxBones = object.isSkinnedMesh ? allocateBones( object ) : 0;
  76. var precision = renderer.getPrecision();
  77. if ( material.precision !== null ) {
  78. precision = capabilities.getMaxPrecision( material.precision );
  79. if ( precision !== material.precision ) {
  80. console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
  81. }
  82. }
  83. var currentRenderTarget = renderer.getRenderTarget();
  84. var parameters = {
  85. shaderID: shaderID,
  86. precision: precision,
  87. supportsVertexTextures: capabilities.vertexTextures,
  88. outputEncoding: getTextureEncodingFromMap( ( ! currentRenderTarget ) ? null : currentRenderTarget.texture, renderer.gammaOutput ),
  89. map: !! material.map,
  90. mapEncoding: getTextureEncodingFromMap( material.map, renderer.gammaInput ),
  91. envMap: !! material.envMap,
  92. envMapMode: material.envMap && material.envMap.mapping,
  93. envMapEncoding: getTextureEncodingFromMap( material.envMap, renderer.gammaInput ),
  94. envMapCubeUV: ( !! material.envMap ) && ( ( material.envMap.mapping === CubeUVReflectionMapping ) || ( material.envMap.mapping === CubeUVRefractionMapping ) ),
  95. lightMap: !! material.lightMap,
  96. aoMap: !! material.aoMap,
  97. emissiveMap: !! material.emissiveMap,
  98. emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap, renderer.gammaInput ),
  99. bumpMap: !! material.bumpMap,
  100. normalMap: !! material.normalMap,
  101. displacementMap: !! material.displacementMap,
  102. roughnessMap: !! material.roughnessMap,
  103. metalnessMap: !! material.metalnessMap,
  104. specularMap: !! material.specularMap,
  105. alphaMap: !! material.alphaMap,
  106. gradientMap: !! material.gradientMap,
  107. combine: material.combine,
  108. vertexColors: material.vertexColors,
  109. fog: !! fog,
  110. useFog: material.fog,
  111. fogExp: ( fog && fog.isFogExp2 ),
  112. flatShading: material.shading === FlatShading,
  113. sizeAttenuation: material.sizeAttenuation,
  114. logarithmicDepthBuffer: capabilities.logarithmicDepthBuffer,
  115. skinning: material.skinning && maxBones > 0,
  116. maxBones: maxBones,
  117. useVertexTexture: capabilities.floatVertexTextures,
  118. morphTargets: material.morphTargets,
  119. morphNormals: material.morphNormals,
  120. maxMorphTargets: renderer.maxMorphTargets,
  121. maxMorphNormals: renderer.maxMorphNormals,
  122. numDirLights: lights.directional.length,
  123. numPointLights: lights.point.length,
  124. numSpotLights: lights.spot.length,
  125. numRectAreaLights: lights.rectArea.length,
  126. numHemiLights: lights.hemi.length,
  127. numClippingPlanes: nClipPlanes,
  128. numClipIntersection: nClipIntersection,
  129. dithering: material.dithering,
  130. shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && lights.shadows.length > 0,
  131. shadowMapType: renderer.shadowMap.type,
  132. toneMapping: renderer.toneMapping,
  133. physicallyCorrectLights: renderer.physicallyCorrectLights,
  134. premultipliedAlpha: material.premultipliedAlpha,
  135. alphaTest: material.alphaTest,
  136. doubleSided: material.side === DoubleSide,
  137. flipSided: material.side === BackSide,
  138. depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false
  139. };
  140. return parameters;
  141. };
  142. this.getProgramCode = function ( material, parameters ) {
  143. var array = [];
  144. if ( parameters.shaderID ) {
  145. array.push( parameters.shaderID );
  146. } else {
  147. array.push( material.fragmentShader );
  148. array.push( material.vertexShader );
  149. }
  150. if ( material.defines !== undefined ) {
  151. for ( var name in material.defines ) {
  152. array.push( name );
  153. array.push( material.defines[ name ] );
  154. }
  155. }
  156. for ( var i = 0; i < parameterNames.length; i ++ ) {
  157. array.push( parameters[ parameterNames[ i ] ] );
  158. }
  159. array.push( material.onBeforeCompile.toString() );
  160. array.push( renderer.gammaOutput );
  161. return array.join();
  162. };
  163. this.acquireProgram = function ( material, shader, parameters, code ) {
  164. var program;
  165. // Check if code has been already compiled
  166. for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
  167. var programInfo = programs[ p ];
  168. if ( programInfo.code === code ) {
  169. program = programInfo;
  170. ++ program.usedTimes;
  171. break;
  172. }
  173. }
  174. if ( program === undefined ) {
  175. program = new WebGLProgram( renderer, code, material, shader, parameters );
  176. programs.push( program );
  177. }
  178. return program;
  179. };
  180. this.releaseProgram = function ( program ) {
  181. if ( -- program.usedTimes === 0 ) {
  182. // Remove from unordered set
  183. var i = programs.indexOf( program );
  184. programs[ i ] = programs[ programs.length - 1 ];
  185. programs.pop();
  186. // Free WebGL resources
  187. program.destroy();
  188. }
  189. };
  190. // Exposed for resource monitoring & error feedback via renderer.info:
  191. this.programs = programs;
  192. }
  193. export { WebGLPrograms };