WebGLPrograms.js 11.9 KB
Newer Older
1
import { BackSide, DoubleSide, CubeUVRefractionMapping, CubeUVReflectionMapping, LinearEncoding, ObjectSpaceNormalMap, TangentSpaceNormalMap, NoToneMapping } from '../../constants.js';
B
bentok 已提交
2
import { WebGLProgram } from './WebGLProgram.js';
3 4
import { ShaderLib } from '../shaders/ShaderLib.js';
import { UniformsUtils } from '../shaders/UniformsUtils.js';
R
Rich Harris 已提交
5

M
Mugen87 已提交
6
function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingStates, clipping ) {
G
gero3 已提交
7

M
Mugen87 已提交
8
	const programs = [];
G
gero3 已提交
9

M
Mugen87 已提交
10 11 12 13 14
	const isWebGL2 = capabilities.isWebGL2;
	const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
	const floatVertexTextures = capabilities.floatVertexTextures;
	const maxVertexUniforms = capabilities.maxVertexUniforms;
	const vertexTextures = capabilities.vertexTextures;
15

M
Mugen87 已提交
16 17 18
	let precision = capabilities.precision;

	const shaderIDs = {
G
gero3 已提交
19
		MeshDepthMaterial: 'depth',
W
WestLangley 已提交
20
		MeshDistanceMaterial: 'distanceRGBA',
G
gero3 已提交
21 22 23 24
		MeshNormalMaterial: 'normal',
		MeshBasicMaterial: 'basic',
		MeshLambertMaterial: 'lambert',
		MeshPhongMaterial: 'phong',
25
		MeshToonMaterial: 'toon',
26
		MeshStandardMaterial: 'physical',
W
WestLangley 已提交
27
		MeshPhysicalMaterial: 'physical',
W
WestLangley 已提交
28
		MeshMatcapMaterial: 'matcap',
G
gero3 已提交
29 30
		LineBasicMaterial: 'basic',
		LineDashedMaterial: 'dashed',
31
		PointsMaterial: 'points',
32 33
		ShadowMaterial: 'shadow',
		SpriteMaterial: 'sprite'
G
gero3 已提交
34
	};
G
gero3 已提交
35

M
Mugen87 已提交
36
	const parameterNames = [
G
Garrett Johnson 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
		'precision', 'isWebGL2', 'supportsVertexTextures', 'outputEncoding', 'instancing', 'instancingColor',
		'map', 'mapEncoding', 'matcap', 'matcapEncoding', 'envMap', 'envMapMode', 'envMapEncoding', 'envMapCubeUV',
		'lightMap', 'lightMapEncoding', 'aoMap', 'emissiveMap', 'emissiveMapEncoding', 'bumpMap', 'normalMap', 'objectSpaceNormalMap', 'tangentSpaceNormalMap', 'clearcoatMap', 'clearcoatRoughnessMap', 'clearcoatNormalMap', 'displacementMap', 'specularMap',
		'roughnessMap', 'metalnessMap', 'gradientMap',
		'alphaMap', 'combine', 'vertexColors', 'vertexTangents', 'vertexUvs', 'uvsVertexOnly', 'fog', 'useFog', 'fogExp2',
		'flatShading', 'sizeAttenuation', 'logarithmicDepthBuffer', 'skinning',
		'maxBones', 'useVertexTexture', 'morphTargets', 'morphNormals',
		'maxMorphTargets', 'maxMorphNormals', 'premultipliedAlpha',
		'numDirLights', 'numPointLights', 'numSpotLights', 'numHemiLights', 'numRectAreaLights',
		'numDirLightShadows', 'numPointLightShadows', 'numSpotLightShadows',
		'shadowMapEnabled', 'shadowMapType', 'toneMapping', 'physicallyCorrectLights',
		'alphaTest', 'doubleSided', 'flipSided', 'numClippingPlanes', 'numClipIntersection', 'depthPacking', 'dithering',
		'sheen', 'transmissionMap'
M
Mr.doob 已提交
50
	];
G
gero3 已提交
51

M
Mugen87 已提交
52
	function getMaxBones( object ) {
G
gero3 已提交
53

M
Mugen87 已提交
54 55
		const skeleton = object.skeleton;
		const bones = skeleton.bones;
56

57
		if ( floatVertexTextures ) {
58

G
gero3 已提交
59 60 61 62 63 64 65 66 67 68 69
			return 1024;

		} else {

			// default for when object is not specified
			// ( for example when prebuilding shader to be used with multiple objects )
			//
			//  - leave some extra space for other uniforms
			//  - limit here is ANGLE's 254 max uniform vectors
			//    (up to 54 should be safe)

M
Mugen87 已提交
70 71
			const nVertexUniforms = maxVertexUniforms;
			const nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
G
gero3 已提交
72

M
Mugen87 已提交
73
			const maxBones = Math.min( nVertexMatrices, bones.length );
G
gero3 已提交
74

75
			if ( maxBones < bones.length ) {
G
gero3 已提交
76

77 78
				console.warn( 'THREE.WebGLRenderer: Skeleton has ' + bones.length + ' bones. This GPU supports ' + maxBones + '.' );
				return 0;
G
gero3 已提交
79 80 81 82 83 84 85 86

			}

			return maxBones;

		}

	}
G
gero3 已提交
87

88
	function getTextureEncodingFromMap( map ) {
89

M
Mugen87 已提交
90
		let encoding;
M
Mr.doob 已提交
91 92

		if ( ! map ) {
M
Mr.doob 已提交
93

R
Rich Harris 已提交
94
			encoding = LinearEncoding;
M
Mr.doob 已提交
95

96
		} else if ( map.isTexture ) {
M
Mr.doob 已提交
97

98
			encoding = map.encoding;
M
Mr.doob 已提交
99

100
		} else if ( map.isWebGLRenderTarget ) {
M
Mr.doob 已提交
101

G
Garrett Johnson 已提交
102
			console.warn( 'THREE.WebGLPrograms.getTextureEncodingFromMap: don\'t use render targets as textures. Use their .texture property instead.' );
103
			encoding = map.texture.encoding;
M
Mr.doob 已提交
104

105 106 107
		}

		return encoding;
M
Mr.doob 已提交
108

109 110
	}

M
Mugen87 已提交
111
	function getParameters( material, lights, shadows, scene, object ) {
M
Mr.doob 已提交
112

M
Mugen87 已提交
113 114
		const fog = scene.fog;
		const environment = material.isMeshStandardMaterial ? scene.environment : null;
M
Mr.doob 已提交
115

116
		const envMap = cubemaps.get( material.envMap || environment );
M
Mr.doob 已提交
117

M
Mugen87 已提交
118
		const shaderID = shaderIDs[ material.type ];
M
Mr.doob 已提交
119

G
gero3 已提交
120
		// heuristics to create shader parameters according to lights in the scene
G
gero3 已提交
121 122
		// (not to blow over maxLights budget)

M
Mugen87 已提交
123
		const maxBones = object.isSkinnedMesh ? getMaxBones( object ) : 0;
G
gero3 已提交
124 125 126

		if ( material.precision !== null ) {

G
gero3 已提交
127
			precision = capabilities.getMaxPrecision( material.precision );
G
gero3 已提交
128 129 130

			if ( precision !== material.precision ) {

131
				console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
G
gero3 已提交
132 133 134 135

			}

		}
M
Mr.doob 已提交
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		let vertexShader, fragmentShader;

		if ( shaderID ) {

			const shader = ShaderLib[ shaderID ];

			vertexShader = shader.vertexShader;
			fragmentShader = shader.fragmentShader;

		} else {

			vertexShader = material.vertexShader;
			fragmentShader = material.fragmentShader;

		}
152

M
Mugen87 已提交
153
		const currentRenderTarget = renderer.getRenderTarget();
154

M
Mugen87 已提交
155
		const parameters = {
G
gero3 已提交
156

157
			isWebGL2: isWebGL2,
M
Mr.doob 已提交
158

G
gero3 已提交
159
			shaderID: shaderID,
160
			shaderName: material.type,
161

162 163
			vertexShader: vertexShader,
			fragmentShader: fragmentShader,
164 165
			defines: material.defines,

M
Mr.doob 已提交
166 167
			isRawShaderMaterial: material.isRawShaderMaterial === true,
			glslVersion: material.glslVersion,
G
gero3 已提交
168 169

			precision: precision,
M
Mr.doob 已提交
170 171

			instancing: object.isInstancedMesh === true,
172
			instancingColor: object.isInstancedMesh === true && object.instanceColor !== null,
M
Mr.doob 已提交
173

174
			supportsVertexTextures: vertexTextures,
175
			outputEncoding: ( currentRenderTarget !== null ) ? getTextureEncodingFromMap( currentRenderTarget.texture ) : renderer.outputEncoding,
G
gero3 已提交
176
			map: !! material.map,
M
Mugen87 已提交
177
			mapEncoding: getTextureEncodingFromMap( material.map ),
W
WestLangley 已提交
178
			matcap: !! material.matcap,
M
Mugen87 已提交
179
			matcapEncoding: getTextureEncodingFromMap( material.matcap ),
M
Mr.doob 已提交
180 181 182 183
			envMap: !! envMap,
			envMapMode: envMap && envMap.mapping,
			envMapEncoding: getTextureEncodingFromMap( envMap ),
			envMapCubeUV: ( !! envMap ) && ( ( envMap.mapping === CubeUVReflectionMapping ) || ( envMap.mapping === CubeUVRefractionMapping ) ),
G
gero3 已提交
184
			lightMap: !! material.lightMap,
M
Mugen87 已提交
185
			lightMapEncoding: getTextureEncodingFromMap( material.lightMap ),
G
gero3 已提交
186 187
			aoMap: !! material.aoMap,
			emissiveMap: !! material.emissiveMap,
M
Mugen87 已提交
188
			emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap ),
G
gero3 已提交
189 190
			bumpMap: !! material.bumpMap,
			normalMap: !! material.normalMap,
W
WestLangley 已提交
191
			objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap,
192
			tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap,
193 194
			clearcoatMap: !! material.clearcoatMap,
			clearcoatRoughnessMap: !! material.clearcoatRoughnessMap,
195
			clearcoatNormalMap: !! material.clearcoatNormalMap,
196
			displacementMap: !! material.displacementMap,
W
WestLangley 已提交
197 198
			roughnessMap: !! material.roughnessMap,
			metalnessMap: !! material.metalnessMap,
G
gero3 已提交
199 200 201
			specularMap: !! material.specularMap,
			alphaMap: !! material.alphaMap,

T
Takahiro 已提交
202
			gradientMap: !! material.gradientMap,
T
Takahiro 已提交
203

204
			sheen: !! material.sheen,
205

206 207
			transmissionMap: !! material.transmissionMap,

G
gero3 已提交
208 209
			combine: material.combine,

210
			vertexTangents: ( material.normalMap && material.vertexTangents ),
G
gero3 已提交
211
			vertexColors: material.vertexColors,
212 213
			vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatMap || !! material.clearcoatRoughnessMap || !! material.clearcoatNormalMap || !! material.displacementMap || !! material.transmissionMap,
			uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap || !! material.transmissionMap ) && !! material.displacementMap,
G
gero3 已提交
214

M
Mr.doob 已提交
215
			fog: !! fog,
G
gero3 已提交
216
			useFog: material.fog,
E
Elias Hasle 已提交
217
			fogExp2: ( fog && fog.isFogExp2 ),
G
gero3 已提交
218

219
			flatShading: material.flatShading,
G
gero3 已提交
220 221

			sizeAttenuation: material.sizeAttenuation,
222
			logarithmicDepthBuffer: logarithmicDepthBuffer,
G
gero3 已提交
223

M
Mr.doob 已提交
224
			skinning: material.skinning && maxBones > 0,
G
gero3 已提交
225
			maxBones: maxBones,
226
			useVertexTexture: floatVertexTextures,
G
gero3 已提交
227 228 229

			morphTargets: material.morphTargets,
			morphNormals: material.morphNormals,
M
Mr.doob 已提交
230 231
			maxMorphTargets: renderer.maxMorphTargets,
			maxMorphNormals: renderer.maxMorphNormals,
G
gero3 已提交
232

233 234 235
			numDirLights: lights.directional.length,
			numPointLights: lights.point.length,
			numSpotLights: lights.spot.length,
236
			numRectAreaLights: lights.rectArea.length,
237
			numHemiLights: lights.hemi.length,
238

239 240 241 242
			numDirLightShadows: lights.directionalShadowMap.length,
			numPointLightShadows: lights.pointShadowMap.length,
			numSpotLightShadows: lights.spotShadowMap.length,

M
Mugen87 已提交
243 244
			numClippingPlanes: clipping.numPlanes,
			numClipIntersection: clipping.numIntersection,
T
tschw 已提交
245

246
			dithering: material.dithering,
247

248
			shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0,
M
Mr.doob 已提交
249
			shadowMapType: renderer.shadowMap.type,
G
gero3 已提交
250

W
WestLangley 已提交
251
			toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping,
252
			physicallyCorrectLights: renderer.physicallyCorrectLights,
B
Ben Houston 已提交
253

254 255
			premultipliedAlpha: material.premultipliedAlpha,

G
gero3 已提交
256
			alphaTest: material.alphaTest,
R
Rich Harris 已提交
257 258
			doubleSided: material.side === DoubleSide,
			flipSided: material.side === BackSide,
259

260 261 262 263 264
			depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false,

			index0AttributeName: material.index0AttributeName,

			extensionDerivatives: material.extensions && material.extensions.derivatives,
265
			extensionFragDepth: material.extensions && material.extensions.fragDepth,
M
Michael Herzog 已提交
266
			extensionDrawBuffers: material.extensions && material.extensions.drawBuffers,
267 268
			extensionShaderTextureLOD: material.extensions && material.extensions.shaderTextureLOD,

269 270 271
			rendererExtensionFragDepth: isWebGL2 || extensions.has( 'EXT_frag_depth' ),
			rendererExtensionDrawBuffers: isWebGL2 || extensions.has( 'WEBGL_draw_buffers' ),
			rendererExtensionShaderTextureLod: isWebGL2 || extensions.has( 'EXT_shader_texture_lod' ),
272

M
Mr.doob 已提交
273
			customProgramCacheKey: material.customProgramCacheKey()
G
gero3 已提交
274 275

		};
276

G
gero3 已提交
277
		return parameters;
G
gero3 已提交
278

M
Mr.doob 已提交
279
	}
G
gero3 已提交
280

M
Mr.doob 已提交
281
	function getProgramCacheKey( parameters ) {
G
gero3 已提交
282

M
Mugen87 已提交
283
		const array = [];
G
gero3 已提交
284 285 286

		if ( parameters.shaderID ) {

M
Mr.doob 已提交
287
			array.push( parameters.shaderID );
G
gero3 已提交
288 289 290

		} else {

291 292
			array.push( parameters.fragmentShader );
			array.push( parameters.vertexShader );
G
gero3 已提交
293 294 295

		}

296
		if ( parameters.defines !== undefined ) {
G
gero3 已提交
297

M
Mugen87 已提交
298
			for ( const name in parameters.defines ) {
G
gero3 已提交
299

M
Mr.doob 已提交
300
				array.push( name );
301
				array.push( parameters.defines[ name ] );
G
gero3 已提交
302 303 304 305 306

			}

		}

307
		if ( parameters.isRawShaderMaterial === false ) {
G
gero3 已提交
308

M
Mugen87 已提交
309
			for ( let i = 0; i < parameterNames.length; i ++ ) {
E
Emmett Lalish 已提交
310 311 312 313

				array.push( parameters[ parameterNames[ i ] ] );

			}
G
gero3 已提交
314

E
Emmett Lalish 已提交
315 316
			array.push( renderer.outputEncoding );
			array.push( renderer.gammaFactor );
M
Mr.doob 已提交
317

E
Emmett Lalish 已提交
318
		}
319

320
		array.push( parameters.customProgramCacheKey );
W
WestLangley 已提交
321

M
Mr.doob 已提交
322
		return array.join();
G
gero3 已提交
323

M
Mr.doob 已提交
324
	}
G
gero3 已提交
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	function getUniforms( material ) {

		const shaderID = shaderIDs[ material.type ];
		let uniforms;

		if ( shaderID ) {

			const shader = ShaderLib[ shaderID ];
			uniforms = UniformsUtils.clone( shader.uniforms );

		} else {

			uniforms = material.uniforms;

		}

		return uniforms;

	}

M
Mr.doob 已提交
346
	function acquireProgram( parameters, cacheKey ) {
G
gero3 已提交
347

M
Mugen87 已提交
348
		let program;
G
gero3 已提交
349 350

		// Check if code has been already compiled
M
Mugen87 已提交
351
		for ( let p = 0, pl = programs.length; p < pl; p ++ ) {
G
gero3 已提交
352

M
Mugen87 已提交
353
			const preexistingProgram = programs[ p ];
G
gero3 已提交
354

355
			if ( preexistingProgram.cacheKey === cacheKey ) {
G
gero3 已提交
356

357
				program = preexistingProgram;
358
				++ program.usedTimes;
G
gero3 已提交
359 360 361 362 363 364 365 366

				break;

			}

		}

		if ( program === undefined ) {
G
gero3 已提交
367

368
			program = new WebGLProgram( renderer, cacheKey, parameters, bindingStates );
G
gero3 已提交
369
			programs.push( program );
G
gero3 已提交
370

G
gero3 已提交
371
		}
G
gero3 已提交
372

373
		return program;
G
gero3 已提交
374

M
Mr.doob 已提交
375
	}
376

M
Mr.doob 已提交
377
	function releaseProgram( program ) {
378 379 380 381

		if ( -- program.usedTimes === 0 ) {

			// Remove from unordered set
M
Mugen87 已提交
382
			const i = programs.indexOf( program );
383 384 385 386 387 388 389 390
			programs[ i ] = programs[ programs.length - 1 ];
			programs.pop();

			// Free WebGL resources
			program.destroy();

		}

M
Mr.doob 已提交
391
	}
G
gero3 已提交
392

M
Mr.doob 已提交
393 394 395
	return {
		getParameters: getParameters,
		getProgramCacheKey: getProgramCacheKey,
396
		getUniforms: getUniforms,
M
Mr.doob 已提交
397 398 399 400 401
		acquireProgram: acquireProgram,
		releaseProgram: releaseProgram,
		// Exposed for resource monitoring & error feedback via renderer.info:
		programs: programs
	};
402

M
Mr.doob 已提交
403
}
R
Rich Harris 已提交
404 405


406
export { WebGLPrograms };