WebGLPrograms.js 8.0 KB

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