2
0

WebGLPrograms.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { WebGLProgram } from './WebGLProgram';
  5. import { BackSide, DoubleSide, FlatShading, CubeUVRefractionMapping, CubeUVReflectionMapping, GammaEncoding, LinearEncoding } from '../../constants';
  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"
  32. ];
  33. function allocateBones( object ) {
  34. if ( capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  35. return 1024;
  36. } else {
  37. // default for when object is not specified
  38. // ( for example when prebuilding shader to be used with multiple objects )
  39. //
  40. // - leave some extra space for other uniforms
  41. // - limit here is ANGLE's 254 max uniform vectors
  42. // (up to 54 should be safe)
  43. var nVertexUniforms = capabilities.maxVertexUniforms;
  44. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  45. var maxBones = nVertexMatrices;
  46. if ( object !== undefined && (object && object.isSkinnedMesh) ) {
  47. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  48. if ( maxBones < object.skeleton.bones.length ) {
  49. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  50. }
  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 = allocateBones( object );
  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.getCurrentRenderTarget();
  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,
  116. maxBones: maxBones,
  117. useVertexTexture: capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture,
  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. shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && lights.shadows.length > 0,
  130. shadowMapType: renderer.shadowMap.type,
  131. toneMapping: renderer.toneMapping,
  132. physicallyCorrectLights: renderer.physicallyCorrectLights,
  133. premultipliedAlpha: material.premultipliedAlpha,
  134. alphaTest: material.alphaTest,
  135. doubleSided: material.side === DoubleSide,
  136. flipSided: material.side === BackSide,
  137. depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false
  138. };
  139. return parameters;
  140. };
  141. this.getProgramCode = function ( material, parameters ) {
  142. var array = [];
  143. if ( parameters.shaderID ) {
  144. array.push( parameters.shaderID );
  145. } else {
  146. array.push( material.fragmentShader );
  147. array.push( material.vertexShader );
  148. }
  149. if ( material.defines !== undefined ) {
  150. for ( var name in material.defines ) {
  151. array.push( name );
  152. array.push( material.defines[ name ] );
  153. }
  154. }
  155. for ( var i = 0; i < parameterNames.length; i ++ ) {
  156. array.push( parameters[ parameterNames[ i ] ] );
  157. }
  158. return array.join();
  159. };
  160. this.acquireProgram = function ( material, parameters, code ) {
  161. var program;
  162. // Check if code has been already compiled
  163. for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
  164. var programInfo = programs[ p ];
  165. if ( programInfo.code === code ) {
  166. program = programInfo;
  167. ++ program.usedTimes;
  168. break;
  169. }
  170. }
  171. if ( program === undefined ) {
  172. program = new WebGLProgram( renderer, code, material, parameters );
  173. programs.push( program );
  174. }
  175. return program;
  176. };
  177. this.releaseProgram = function( program ) {
  178. if ( -- program.usedTimes === 0 ) {
  179. // Remove from unordered set
  180. var i = programs.indexOf( program );
  181. programs[ i ] = programs[ programs.length - 1 ];
  182. programs.pop();
  183. // Free WebGL resources
  184. program.destroy();
  185. }
  186. };
  187. // Exposed for resource monitoring & error feedback via renderer.info:
  188. this.programs = programs;
  189. }
  190. export { WebGLPrograms };