WebGLPrograms.js 8.8 KB

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