Skip to content

Commit

Permalink
Added control over fragment shader precision
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcozzi committed Dec 3, 2012
1 parent a4d4491 commit c17516a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
42 changes: 31 additions & 11 deletions Source/Renderer/Context.js
Expand Up @@ -168,7 +168,7 @@ define([
* @exception {RuntimeError} The browser supports WebGL, but initialization failed.
* @exception {DeveloperError} canvas is required.
*/
var Context = function(canvas, options) {
var Context = function(canvas, webglOptions, options) {
if (!window.WebGLRenderingContext) {
throw new RuntimeError('The browser does not support WebGL. Visit http://get.webgl.org.');
}
Expand All @@ -179,22 +179,42 @@ define([

this._canvas = canvas;

options = defaultValue(options, {});
var glOptions = {
alpha : defaultValue(options.alpha, false), // WebGL default is true
depth : defaultValue(options.depth, true),
stencil : defaultValue(options.stencil, true), // WebGL default is false
antialias : defaultValue(options.antialias, true),
premultipliedAlpha : defaultValue(options.premultipliedAlpha, true), // Doesn't matter since alpha is false
preserveDrawingBuffer : defaultValue(options.preserveDrawingBuffer, false)
// TODO: Expose webglOptions and options through getters?
webglOptions = defaultValue(webglOptions, {});
webglOptions = {
alpha : defaultValue(webglOptions.alpha, false), // WebGL default is true
depth : defaultValue(webglOptions.depth, true),
stencil : defaultValue(webglOptions.stencil, true), // WebGL default is false
antialias : defaultValue(webglOptions.antialias, true),
premultipliedAlpha : defaultValue(webglOptions.premultipliedAlpha, true), // Doesn't matter since alpha is false
preserveDrawingBuffer : defaultValue(webglOptions.preserveDrawingBuffer, false)
};

this._originalGLContext = canvas.getContext('webgl', glOptions) || canvas.getContext('experimental-webgl', glOptions);
this._originalGLContext = canvas.getContext('webgl', webglOptions) || canvas.getContext('experimental-webgl', webglOptions);

if (!this._originalGLContext) {
throw new RuntimeError('The browser supports WebGL, but initialization failed.');
}

options = defaultValue(options, {});
var fragmentShaderFloatPrecision = defaultValue(options.fragmentShaderFloatPrecision, 'highp');
if ((fragmentShaderFloatPrecision !== 'highp') &&
(fragmentShaderFloatPrecision !== 'mediump') &&
(fragmentShaderFloatPrecision !== 'lowp')) {
throw new DeveloperError('options.fragmentShaderFloatPrecision must be highp, mediump, or lowp.');
}

if (fragmentShaderFloatPrecision === 'highp') {
this._fragmentShaderFloatPrecision =
'#ifdef GL_FRAGMENT_PRECISION_HIGH \n' +
' precision highp float; \n' +
'#else \n' +
' precision mediump float; \n' +
'#endif \n\n';
} else {
this._fragmentShaderFloatPrecision = 'precision mediump float; \n';
}

this._id = createGuid();

// Validation and logging disabled by default for speed.
Expand Down Expand Up @@ -1111,7 +1131,7 @@ define([
* sp = context.createShaderProgram(vs, fs, attributes); *
*/
Context.prototype.createShaderProgram = function(vertexShaderSource, fragmentShaderSource, attributeLocations) {
return new ShaderProgram(this._gl, this._logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations);
return new ShaderProgram(this._gl, this._logShaderCompilation, this._fragmentShaderFloatPrecision, vertexShaderSource, fragmentShaderSource, attributeLocations);
};

function createBuffer(gl, bufferTarget, typedArrayOrSizeInBytes, usage) {
Expand Down
17 changes: 4 additions & 13 deletions Source/Renderer/ShaderProgram.js
Expand Up @@ -1704,8 +1704,8 @@ define([
* @see Context#createShaderProgram
* @see Context#getShaderCache
*/
var ShaderProgram = function(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations) {
var program = createAndLinkProgram(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations);
var ShaderProgram = function(gl, logShaderCompilation, fragmentShaderFloatPrecision, vertexShaderSource, fragmentShaderSource, attributeLocations) {
var program = createAndLinkProgram(gl, logShaderCompilation, fragmentShaderFloatPrecision, vertexShaderSource, fragmentShaderSource, attributeLocations);
var numberOfVertexAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
var uniforms = findUniforms(gl, program);
var partitionedUniforms = partitionUniforms(uniforms.allUniforms);
Expand Down Expand Up @@ -1794,15 +1794,6 @@ define([
return modifiedSource;
}

function getFragmentShaderPrecision() {
// TODO: Performance?
return '#ifdef GL_FRAGMENT_PRECISION_HIGH \n' +
' precision highp float; \n' +
'#else \n' +
' precision mediump float; \n' +
'#endif \n\n';
}

function getBuiltinConstants() {
var constants = {
/**
Expand Down Expand Up @@ -2039,15 +2030,15 @@ define([
return definitions;
};

function createAndLinkProgram(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations) {
function createAndLinkProgram(gl, logShaderCompilation, fragmentShaderFloatPrecision, vertexShaderSource, fragmentShaderSource, attributeLocations) {
var vsSourceVersioned = extractShaderVersion(vertexShaderSource);
var fsSourceVersioned = extractShaderVersion(fragmentShaderSource);

var vsSource = vsSourceVersioned.versionDirective +
getShaderDefinitions() +
commentOutAutomaticUniforms(vsSourceVersioned.modifiedSource);
var fsSource = fsSourceVersioned.versionDirective +
getFragmentShaderPrecision() +
fragmentShaderFloatPrecision +
getShaderDefinitions() +
commentOutAutomaticUniforms(fsSourceVersioned.modifiedSource);

Expand Down

1 comment on commit c17516a

@pjcozzi
Copy link
Contributor Author

@pjcozzi pjcozzi commented on c17516a Dec 3, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.