1 | import { PRECISION } from '@pixi/constants';
|
2 | import { ProgramCache, isMobile } from '@pixi/utils';
|
3 | import defaultFragment from './defaultProgram.mjs';
|
4 | import defaultVertex from './defaultProgram2.mjs';
|
5 | import './utils/index.mjs';
|
6 | import { setPrecision } from './utils/setPrecision.mjs';
|
7 | import { getMaxFragmentPrecision } from './utils/getMaxFragmentPrecision.mjs';
|
8 |
|
9 | let UID = 0;
|
10 | const nameCache = {};
|
11 | const _Program = class {
|
12 | constructor(vertexSrc, fragmentSrc, name = "pixi-shader", extra = {}) {
|
13 | this.extra = {};
|
14 | this.id = UID++;
|
15 | this.vertexSrc = vertexSrc || _Program.defaultVertexSrc;
|
16 | this.fragmentSrc = fragmentSrc || _Program.defaultFragmentSrc;
|
17 | this.vertexSrc = this.vertexSrc.trim();
|
18 | this.fragmentSrc = this.fragmentSrc.trim();
|
19 | this.extra = extra;
|
20 | if (this.vertexSrc.substring(0, 8) !== "#version") {
|
21 | name = name.replace(/\s+/g, "-");
|
22 | if (nameCache[name]) {
|
23 | nameCache[name]++;
|
24 | name += `-${nameCache[name]}`;
|
25 | } else {
|
26 | nameCache[name] = 1;
|
27 | }
|
28 | this.vertexSrc = `#define SHADER_NAME ${name}
|
29 | ${this.vertexSrc}`;
|
30 | this.fragmentSrc = `#define SHADER_NAME ${name}
|
31 | ${this.fragmentSrc}`;
|
32 | this.vertexSrc = setPrecision(this.vertexSrc, _Program.defaultVertexPrecision, PRECISION.HIGH);
|
33 | this.fragmentSrc = setPrecision(this.fragmentSrc, _Program.defaultFragmentPrecision, getMaxFragmentPrecision());
|
34 | }
|
35 | this.glPrograms = {};
|
36 | this.syncUniforms = null;
|
37 | }
|
38 | static get defaultVertexSrc() {
|
39 | return defaultVertex;
|
40 | }
|
41 | static get defaultFragmentSrc() {
|
42 | return defaultFragment;
|
43 | }
|
44 | static from(vertexSrc, fragmentSrc, name) {
|
45 | const key = vertexSrc + fragmentSrc;
|
46 | let program = ProgramCache[key];
|
47 | if (!program) {
|
48 | ProgramCache[key] = program = new _Program(vertexSrc, fragmentSrc, name);
|
49 | }
|
50 | return program;
|
51 | }
|
52 | };
|
53 | let Program = _Program;
|
54 | Program.defaultVertexPrecision = PRECISION.HIGH;
|
55 | Program.defaultFragmentPrecision = isMobile.apple.device ? PRECISION.HIGH : PRECISION.MEDIUM;
|
56 |
|
57 | export { Program };
|
58 |
|