Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1118461 of WebGLShader

  • Revision slug: Web/API/WebGLShader
  • Revision title: WebGLShader
  • Revision id: 1118461
  • Created:
  • Creator: zalomea
  • Is current revision? No
  • Comment Coding error in the example. (+) operator left.

Revision Content

{{APIRef("WebGL")}}

The WebGLShader is part of the WebGL API and can either be a vertex or a fragment shader. A {{domxref("WebGLProgram")}} requires both types of shaders.

Description

To create a WebGLShader use {{domxref("WebGLRenderingContext.createShader")}}, then hook up the GLSL source code using {{domxref("WebGLRenderingContext.shaderSource()")}}, and finally invoke {{domxref("WebGLRenderingContext.compileShader()")}} to finish and compile the shader. At this point the WebGLShader is still not in a usable form and must still be attached to a {{domxref("WebGLProgram")}}.

function createShader (gl, sourceCode, type) {
  // Compiles either a shader of type gl.VERTEX_SHADER or gl.FRAGMENT_SHADER
  var shader = gl.createShader( type );
  gl.shaderSource( shader, sourceCode );
  gl.compileShader( shader );

  if ( !gl.getShaderParameter(shader, gl.COMPILE_STATUS) ) {
    var info = gl.getShaderInfoLog( shader );
    throw "Could not compile WebGL program. \n\n" + info;
  }
}

See {{domxref("WebGLProgram")}} for information on attaching the shaders.

Examples

Creating a vertex shader

Note that there are many other strategies for writing and accessing shader source code strings. These example are for illustration purposes only.

var vertexShaderSource =
  "attribute vec4 position;\n"+
  "void main() {\n"+
  "  gl_Position = position;\n"+
  "}\n";

//Use the createShader function from the example above
var vertexShader = createShader(gl, vertexShaderSource, gl.VERTEX_SHADER)

Creating a fragment shader

var fragmentShaderSource =
  "void main() {\n"+
  "  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"+
  "}\n";

//Use the createShader function from the example above
var fragmentShader = createShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER)

Specifications

Specification Status Comment
{{SpecName('WebGL', "#5.8", "WebGLShader")}} {{Spec2('WebGL')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("9")}} {{CompatGeckoDesktop("2.0")}} {{CompatIE("11")}} {{CompatOpera("12")}} {{CompatSafari("5.1")}}
Available in workers {{CompatNo}} {{CompatGeckoDesktop(44)}} [1] {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} 25 {{CompatVersionUnknown}} {{CompatUnknown}} 12 8.1
Available in workers {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(44)}} [1] {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] This feature is behind a feature preference setting. In about:config, set gfx.offscreencanvas.enabled to true.

See also

  • {{domxref("WebGLProgram")}}
  • {{domxref("WebGLRenderingContext.attachShader()")}}
  • {{domxref("WebGLRenderingContext.bindAttribLocation()")}}
  • {{domxref("WebGLRenderingContext.compileShader()")}}
  • {{domxref("WebGLRenderingContext.createProgram()")}}
  • {{domxref("WebGLRenderingContext.createShader()")}}
  • {{domxref("WebGLRenderingContext.deleteProgram()")}}
  • {{domxref("WebGLRenderingContext.deleteShader()")}}
  • {{domxref("WebGLRenderingContext.detachShader()")}}
  • {{domxref("WebGLRenderingContext.getAttachedShaders()")}}
  • {{domxref("WebGLRenderingContext.getProgramParameter()")}}
  • {{domxref("WebGLRenderingContext.getProgramInfoLog()")}}
  • {{domxref("WebGLRenderingContext.getShaderParameter()")}}
  • {{domxref("WebGLRenderingContext.getShaderPrecisionFormat()")}}
  • {{domxref("WebGLRenderingContext.getShaderInfoLog()")}}
  • {{domxref("WebGLRenderingContext.getShaderSource()")}}
  • {{domxref("WebGLRenderingContext.isProgram()")}}
  • {{domxref("WebGLRenderingContext.isShader()")}}
  • {{domxref("WebGLRenderingContext.linkProgram()")}}
  • {{domxref("WebGLRenderingContext.shaderSource()")}}
  • {{domxref("WebGLRenderingContext.useProgram()")}}
  • {{domxref("WebGLRenderingContext.validateProgram()")}}

Revision Source

<div>{{APIRef("WebGL")}}</div>

<p>The <strong>WebGLShader</strong> is part of the <a href="/en-US/docs/Web/API/WebGL_API">WebGL API</a> and can either be a vertex or a fragment shader. A {{domxref("WebGLProgram")}} requires both types of shaders.</p>

<h2 id="Description">Description</h2>

<p>To create a <strong>WebGLShader</strong> use {{domxref("WebGLRenderingContext.createShader")}}, then hook up the GLSL source code using&nbsp;{{domxref("WebGLRenderingContext.shaderSource()")}}, and finally invoke {{domxref("WebGLRenderingContext.compileShader()")}} to finish and compile the shader. At this point the <strong>WebGLShader</strong>&nbsp;is still not in a usable form and must still be attached to a&nbsp;{{domxref("WebGLProgram")}}.</p>

<pre class="brush: js">
function createShader (gl, sourceCode, type) {
  // Compiles either a shader of type gl.VERTEX_SHADER or gl.FRAGMENT_SHADER
  var shader = gl.createShader( type );
  gl.shaderSource( shader, sourceCode );
  gl.compileShader( shader );

  if ( !gl.getShaderParameter(shader, gl.COMPILE_STATUS) ) {
    var info = gl.getShaderInfoLog( shader );
    throw "Could not compile WebGL program. \n\n" + info;
  }
}
</pre>

<p>See {{domxref("WebGLProgram")}}&nbsp;for information on attaching the shaders.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Creating_a_vertex_shader">Creating a vertex shader</h3>

<p>Note that there are many other strategies for writing and accessing shader source code strings.&nbsp;These example are for illustration purposes only.</p>

<pre class="brush: js">
var vertexShaderSource =
  "attribute vec4 position;\n"+
  "void main() {\n"+
  "  gl_Position = position;\n"+
  "}\n";

//Use the createShader function from the example above
var vertexShader = createShader(gl, vertexShaderSource, gl.VERTEX_SHADER)
</pre>

<h3 id="Creating_a_fragment_shader">Creating a fragment shader</h3>

<pre class="brush: js">
var fragmentShaderSource =
  "void main() {\n"+
  "  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"+
  "}\n";

//Use the createShader function from the example above
var fragmentShader = createShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER)
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('WebGL', "#5.8", "WebGLShader")}}</td>
   <td>{{Spec2('WebGL')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("9")}}</td>
   <td>{{CompatGeckoDesktop("2.0")}}</td>
   <td>{{CompatIE("11")}}</td>
   <td>{{CompatOpera("12")}}</td>
   <td>{{CompatSafari("5.1")}}</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(44)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>25</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>12</td>
   <td>8.1</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(44)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] This feature is behind a feature preference setting. In about:config, set <code>gfx.offscreencanvas.enabled</code> to <code>true</code>.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("WebGLProgram")}}</li>
 <li>{{domxref("WebGLRenderingContext.attachShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.bindAttribLocation()")}}</li>
 <li>{{domxref("WebGLRenderingContext.compileShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.createProgram()")}}</li>
 <li>{{domxref("WebGLRenderingContext.createShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.deleteProgram()")}}</li>
 <li>{{domxref("WebGLRenderingContext.deleteShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.detachShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getAttachedShaders()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getProgramParameter()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getProgramInfoLog()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getShaderParameter()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getShaderPrecisionFormat()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getShaderInfoLog()")}}</li>
 <li>{{domxref("WebGLRenderingContext.getShaderSource()")}}</li>
 <li>{{domxref("WebGLRenderingContext.isProgram()")}}</li>
 <li>{{domxref("WebGLRenderingContext.isShader()")}}</li>
 <li>{{domxref("WebGLRenderingContext.linkProgram()")}}</li>
 <li>{{domxref("WebGLRenderingContext.shaderSource()")}}</li>
 <li>{{domxref("WebGLRenderingContext.useProgram()")}}</li>
 <li>{{domxref("WebGLRenderingContext.validateProgram()")}}</li>
</ul>
Revert to this revision