WebGLPrograms.js 9.5 KB

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