2
0

WebGLPrograms.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { BackSide, DoubleSide, CubeUVRefractionMapping, CubeUVReflectionMapping, 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: 'toon',
  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", "numMultiviewViews",
  33. "map", "mapEncoding", "matcap", "matcapEncoding", "envMap", "envMapMode", "envMapEncoding", "envMapCubeUV",
  34. "lightMap", "lightMapEncoding", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "objectSpaceNormalMap", "tangentSpaceNormalMap", "clearcoatNormalMap", "displacementMap", "specularMap",
  35. "roughnessMap", "metalnessMap", "gradientMap",
  36. "alphaMap", "combine", "vertexColors", "vertexTangents", "vertexUvs", "uvsVertexOnly", "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 ) {
  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. return encoding;
  79. }
  80. this.getParameters = function ( material, lights, shadows, scene, nClipPlanes, nClipIntersection, object ) {
  81. var fog = scene.fog;
  82. var environment = material.isMeshStandardMaterial ? scene.environment : null;
  83. var envMap = material.envMap || environment;
  84. var shaderID = shaderIDs[ material.type ];
  85. // heuristics to create shader parameters according to lights in the scene
  86. // (not to blow over maxLights budget)
  87. var maxBones = object.isSkinnedMesh ? allocateBones( object ) : 0;
  88. if ( material.precision !== null ) {
  89. precision = capabilities.getMaxPrecision( material.precision );
  90. if ( precision !== material.precision ) {
  91. console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
  92. }
  93. }
  94. var currentRenderTarget = renderer.getRenderTarget();
  95. var numMultiviewViews = currentRenderTarget && currentRenderTarget.isWebGLMultiviewRenderTarget ? currentRenderTarget.numViews : 0;
  96. var parameters = {
  97. isWebGL2: isWebGL2,
  98. shaderID: shaderID,
  99. precision: precision,
  100. instancing: object.isInstancedMesh === true,
  101. supportsVertexTextures: vertexTextures,
  102. numMultiviewViews: numMultiviewViews,
  103. outputEncoding: ( currentRenderTarget !== null ) ? getTextureEncodingFromMap( currentRenderTarget.texture ) : renderer.outputEncoding,
  104. map: !! material.map,
  105. mapEncoding: getTextureEncodingFromMap( material.map ),
  106. matcap: !! material.matcap,
  107. matcapEncoding: getTextureEncodingFromMap( material.matcap ),
  108. envMap: !! envMap,
  109. envMapMode: envMap && envMap.mapping,
  110. envMapEncoding: getTextureEncodingFromMap( envMap ),
  111. envMapCubeUV: ( !! envMap ) && ( ( envMap.mapping === CubeUVReflectionMapping ) || ( envMap.mapping === CubeUVRefractionMapping ) ),
  112. lightMap: !! material.lightMap,
  113. lightMapEncoding: getTextureEncodingFromMap( material.lightMap ),
  114. aoMap: !! material.aoMap,
  115. emissiveMap: !! material.emissiveMap,
  116. emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap ),
  117. bumpMap: !! material.bumpMap,
  118. normalMap: !! material.normalMap,
  119. objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap,
  120. tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap,
  121. clearcoatNormalMap: !! material.clearcoatNormalMap,
  122. displacementMap: !! material.displacementMap,
  123. roughnessMap: !! material.roughnessMap,
  124. metalnessMap: !! material.metalnessMap,
  125. specularMap: !! material.specularMap,
  126. alphaMap: !! material.alphaMap,
  127. gradientMap: !! material.gradientMap,
  128. sheen: !! material.sheen,
  129. combine: material.combine,
  130. vertexTangents: ( material.normalMap && material.vertexTangents ),
  131. vertexColors: material.vertexColors,
  132. vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap || !! material.displacementMap,
  133. uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap ) && !! material.displacementMap,
  134. fog: !! fog,
  135. useFog: material.fog,
  136. fogExp2: ( fog && fog.isFogExp2 ),
  137. flatShading: material.flatShading,
  138. sizeAttenuation: material.sizeAttenuation,
  139. logarithmicDepthBuffer: logarithmicDepthBuffer,
  140. skinning: material.skinning && maxBones > 0,
  141. maxBones: maxBones,
  142. useVertexTexture: floatVertexTextures,
  143. morphTargets: material.morphTargets,
  144. morphNormals: material.morphNormals,
  145. maxMorphTargets: renderer.maxMorphTargets,
  146. maxMorphNormals: renderer.maxMorphNormals,
  147. numDirLights: lights.directional.length,
  148. numPointLights: lights.point.length,
  149. numSpotLights: lights.spot.length,
  150. numRectAreaLights: lights.rectArea.length,
  151. numHemiLights: lights.hemi.length,
  152. numDirLightShadows: lights.directionalShadowMap.length,
  153. numPointLightShadows: lights.pointShadowMap.length,
  154. numSpotLightShadows: lights.spotShadowMap.length,
  155. numClippingPlanes: nClipPlanes,
  156. numClipIntersection: nClipIntersection,
  157. dithering: material.dithering,
  158. shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0,
  159. shadowMapType: renderer.shadowMap.type,
  160. toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping,
  161. physicallyCorrectLights: renderer.physicallyCorrectLights,
  162. premultipliedAlpha: material.premultipliedAlpha,
  163. alphaTest: material.alphaTest,
  164. doubleSided: material.side === DoubleSide,
  165. flipSided: material.side === BackSide,
  166. depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false
  167. };
  168. return parameters;
  169. };
  170. this.getProgramCacheKey = function ( material, parameters ) {
  171. var array = [];
  172. if ( parameters.shaderID ) {
  173. array.push( parameters.shaderID );
  174. } else {
  175. array.push( material.fragmentShader );
  176. array.push( material.vertexShader );
  177. }
  178. if ( material.defines !== undefined ) {
  179. for ( var name in material.defines ) {
  180. array.push( name );
  181. array.push( material.defines[ name ] );
  182. }
  183. }
  184. if ( material.isRawShaderMaterial === undefined ) {
  185. for ( var i = 0; i < parameterNames.length; i ++ ) {
  186. array.push( parameters[ parameterNames[ i ] ] );
  187. }
  188. array.push( renderer.outputEncoding );
  189. array.push( renderer.gammaFactor );
  190. }
  191. array.push( material.onBeforeCompile.toString() );
  192. return array.join();
  193. };
  194. this.acquireProgram = function ( material, shader, parameters, cacheKey ) {
  195. var program;
  196. // Check if code has been already compiled
  197. for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
  198. var preexistingProgram = programs[ p ];
  199. if ( preexistingProgram.cacheKey === cacheKey ) {
  200. program = preexistingProgram;
  201. ++ program.usedTimes;
  202. break;
  203. }
  204. }
  205. if ( program === undefined ) {
  206. program = new WebGLProgram( renderer, extensions, cacheKey, material, shader, parameters );
  207. programs.push( program );
  208. }
  209. return program;
  210. };
  211. this.releaseProgram = function ( program ) {
  212. if ( -- program.usedTimes === 0 ) {
  213. // Remove from unordered set
  214. var i = programs.indexOf( program );
  215. programs[ i ] = programs[ programs.length - 1 ];
  216. programs.pop();
  217. // Free WebGL resources
  218. program.destroy();
  219. }
  220. };
  221. // Exposed for resource monitoring & error feedback via renderer.info:
  222. this.programs = programs;
  223. }
  224. export { WebGLPrograms };