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 826243 of WebGLProgram

  • Revision slug: Web/API/WebGLProgram
  • Revision title: WebGLProgram
  • Revision id: 826243
  • Created:
  • Creator: gregtatum
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("WebGL")}} {{draft}}
The WebGLProgram is part of the WebGL API and is a combination of two compiled {{domxref("WebGLShader")}}s consisting of a vertex shader and a fragment shader (both written in GLSL). These are then linked into a usable program.

var program = gl.createProgram();

//Attach pre-existing shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);

gl.linkProgram(program);

if ( !gl.getProgramParameter( program, gl.LINK_STATUS) ) {
  var info = gl.getProgramInfoLog(program);
  throw "Could not compile WebGL program. \n\n" + info;
}

See {{domxref("WebGLShader")}} for information on creating the vertexShader and fragmentShader in the above example.

Examples

Using the program

The steps to actually do some work with the program involve telling the GPU to use the program, bind the appropriate data and configuration options, and finally draw something to the screen.

// Use the program
gl.useProgram(program);

// Bind existing attribute data
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.enableVertexAttribArray(attributeLocation);
gl.vertexAttribPointer(attributeLocation, 3, gl.FLOAT, false, 0, 0);

// Draw a single triangle
gl.drawArrays(gl.TRIANGLES, 0, 3);

Deleting the program

If there is an error compiling the program or you wish to delete an existing program, then it is as simple as running {{domxref("WebGLRenderingContext.deleteProgram()")}}. This frees the memory of the compiled program.

gl.deleteProgram(program);

Specifications

Specification Status Comment
{{SpecName('WebGL', "#5.6", "WebGLProgram")}} {{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")}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} 25 {{CompatVersionUnknown}} {{CompatUnknown}} 12 8.1

See also

  • {{domxref("WebGLShader")}}
  • {{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

<p><span style="line-height:19.0909080505371px">{{APIRef("WebGL")}} {{draft}}</span><br />
 The&nbsp;<strong>WebGLProgram</strong>&nbsp;is part of the <a href="/en-US/docs/Web/API/WebGL_API">WebGL API</a>&nbsp;and is a combination of two&nbsp;compiled {{domxref("WebGLShader")}}s&nbsp;consisting of a&nbsp;vertex shader and a fragment shader (both&nbsp;written in GLSL). These are&nbsp;then linked&nbsp;into a usable program.</p>

<pre class="brush: js">
var program = gl.createProgram();

//Attach pre-existing shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);

gl.linkProgram(program);

if ( !gl.getProgramParameter( program, gl.LINK_STATUS) ) {
  var info = gl.getProgramInfoLog(program);
  throw "Could not compile WebGL program. \n\n" + info;
}
</pre>

<p>See <span style="line-height:19.0909080505371px">{{domxref("WebGLShader")}}</span>&nbsp;for information on creating the <code>vertexShader</code> and <code>fragmentShader</code> in the above example.</p>

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

<h3 id="Using_the_program">Using the program</h3>

<p>The steps to actually do some work with the program involve <span style="line-height:19.0909080505371px">telling the GPU to use the program,</span>&nbsp;bind&nbsp;the appropriate data and configuration options, and&nbsp;finally draw&nbsp;something to the screen.</p>

<pre class="brush: js">
// Use the program
gl.useProgram(program);

// Bind existing attribute data
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.enableVertexAttribArray(attributeLocation);
gl.vertexAttribPointer(attributeLocation, 3, gl.FLOAT, false, 0, 0);

// Draw a single triangle
gl.drawArrays(gl.TRIANGLES, 0, 3);
</pre>

<h3 id="Deleting_the_program">Deleting the program</h3>

<p>If there is an error compiling the program or you wish to delete an existing program, then it is as simple as running {{domxref("WebGLRenderingContext.deleteProgram()")}}. This frees the memory of the compiled program.</p>

<pre class="brush: js">
gl.deleteProgram(program);
</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.6", "WebGLProgram")}}</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>
 </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>
 </tbody>
</table>
</div>

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

<ul>
 <li>{{domxref("WebGLShader")}}</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