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

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

Revision Content

{{APIRef("WebGL")}} {{draft}}
The WebGLProgram 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.

Using the program

The steps to actually do some work with the program involve telling the GPU to use the program, binding the appropriate data and configuration options, and then finally drawing 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, and invalidates the name associated with the program.

gl.deleteProgram(program);

Specifications

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

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 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 vertexShader and fragmentShader in the above example.</p>

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

<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;binding the appropriate data and configuration options, and then finally drawing 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>

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

<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, and invalidates the name associated with the 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="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