- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
import { setPrecision,
getMaxFragmentPrecision } from './utils';
import { ProgramCache } from '@pixi/utils';
import defaultFragment from './defaultProgram.frag';
import defaultVertex from './defaultProgram.vert';
import { settings } from '@pixi/settings';
import { PRECISION } from '@pixi/constants';
import type { GLProgram } from './GLProgram';
let UID = 0;
const nameCache: { [key: string]: number } = {};
export interface IAttributeData
{
type: string;
size: number;
location: number;
name: string;
}
export interface IUniformData
{
index: number;
type: string;
size: number;
isArray: boolean;
value: any;
name: string;
}
/**
* Helper class to create a shader program.
* @memberof PIXI
*/
export class Program
{
public id: number;
/** Source code for the vertex shader. */
public vertexSrc: string;
/** Source code for the fragment shader. */
public fragmentSrc: string;
nameCache: any;
glPrograms: { [ key: number ]: GLProgram};
syncUniforms: any;
/** Assigned when a program is first bound to the shader system. */
attributeData: { [key: string]: IAttributeData};
/** Assigned when a program is first bound to the shader system. */
uniformData: {[key: string]: IUniformData};
/**
* @param vertexSrc - The source of the vertex shader.
* @param fragmentSrc - The source of the fragment shader.
* @param name - Name for shader
*/
constructor(vertexSrc?: string, fragmentSrc?: string, name = 'pixi-shader')
{
this.id = UID++;
this.vertexSrc = vertexSrc || Program.defaultVertexSrc;
this.fragmentSrc = fragmentSrc || Program.defaultFragmentSrc;
this.vertexSrc = this.vertexSrc.trim();
this.fragmentSrc = this.fragmentSrc.trim();
if (this.vertexSrc.substring(0, 8) !== '#version')
{
name = name.replace(/\s+/g, '-');
if (nameCache[name])
{
nameCache[name]++;
name += `-${nameCache[name]}`;
}
else
{
nameCache[name] = 1;
}
this.vertexSrc = `#define SHADER_NAME ${name}\n${this.vertexSrc}`;
this.fragmentSrc = `#define SHADER_NAME ${name}\n${this.fragmentSrc}`;
this.vertexSrc = setPrecision(this.vertexSrc, settings.PRECISION_VERTEX, PRECISION.HIGH);
this.fragmentSrc = setPrecision(this.fragmentSrc, settings.PRECISION_FRAGMENT, getMaxFragmentPrecision());
}
// currently this does not extract structs only default types
// this is where we store shader references..
this.glPrograms = {};
this.syncUniforms = null;
}
/**
* The default vertex shader source.
* @constant
*/
static get defaultVertexSrc(): string
{
return defaultVertex;
}
/**
* The default fragment shader source.
* @constant
*/
static get defaultFragmentSrc(): string
{
return defaultFragment;
}
/**
* A short hand function to create a program based of a vertex and fragment shader.
*
* This method will also check to see if there is a cached program.
* @param vertexSrc - The source of the vertex shader.
* @param fragmentSrc - The source of the fragment shader.
* @param name - Name for shader
* @returns A shiny new PixiJS shader program!
*/
static from(vertexSrc?: string, fragmentSrc?: string, name?: string): Program
{
const key = vertexSrc + fragmentSrc;
let program = ProgramCache[key];
if (!program)
{
ProgramCache[key] = program = new Program(vertexSrc, fragmentSrc, name);
}
return program;
}
}