WebGLPrograms.js 7.9 KB

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