# BBC BASIC Programmers' Reference

## Tutorial 3 - Shaders and Effect System

by Richard Russell, August 2015

Note that the code in this tutorial requires Windows 8.1 or Windows 10

This tutorial is closely based on Microsoft's Direct 3D 11 Tutorial 3 but with the code translated from C++ to BBC BASIC for Windows. You should refer to the original for a detailed explanation of the code. ## Summary

In the previous tutorial, we set up a vertex buffer and passed one triangle to the GPU. Now, we will actually step through the graphics pipeline and look at how each stage works. The concept of shaders and the effect system will be explained.

Note that this tutorial shares the same source code as the previous one, but will emphasize a different section.

## The Graphics Pipeline

In the previous tutorial, we set up the vertex buffer, and then we associated a vertex layout with a vertex shader. Now, we will explain what a shader is and how it works. To fully understand the individual shaders, we will take a step back and look at the whole graphical pipeline.

In Direct3D 11, shaders reside in different stages of the graphics pipeline. They are short programs that, executed by the GPU, take certain input data, process that data, and then output the result to the next stage of the pipeline. Direct3D 11 supports three basic types of shaders: vertex shader, geometry shader, and pixel shader.

Vertex shaders are short programs that are executed by the GPU on vertices. Think of vertex shaders as C functions that take each vertex as input, process the input, and then output the modified vertex. In the Direct3D 11 tutorials, we will write our shaders in High-Level Shading Language (HLSL). Recall that our vertex data has a 3D position element, and the vertex shader will do no processing on the input at all. The resulting vertex shader looks like the following:

```      float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
return Pos;
}```

A pixel shader's primary purpose is to compute the colour that each pixel should have. The shader takes certain input about the pixel being coloured, computes the pixel's colour, then outputs that colour back to the pipeline. The vertex shader we created above outputs a float4 with the semantics SV_POSITION. This will be the input of our pixel shader. Since pixel shaders output color values, the output of our pixel shader will be a float4. We give the output the semantics SV_TARGET to signify outputting to the render target format. The pixel shader looks like the following:

```      float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
}```

In the application code, we will need to create a vertex shader object and a pixel shader object. The code is demonstrated below:

```        REM Compile the vertex shader:
hr% = FN_CompileShaderFromFile(@dir\$+"Tutorial02.fx", "VS", "vs_4_0", ^pVSBlob%)
IF hr% ERROR 100, "The FX file could not be opened or " + \
\                 "the vertex shader could not be compiled"
!(^ID3D10Blob{}+4) = !pVSBlob%

SYS ID3D10Blob.GetBufferPointer%, pVSBlob% TO bp%
SYS ID3D10Blob.GetBufferSize%, pVSBlob% TO bs%
IF hr% <> 0 OR pVertexShader% = 0 THEN
SYS ID3D10Blob.Release%, pVSBlob%
ENDIF

hr% = FN_CompileShaderFromFile(@dir\$+"Tutorial02.fx", "PS", "ps_4_0", ^pPSBlob%)
IF hr% ERROR 100, "The FX file could not be opened or " + \
\                 "the pixel shader could not be compiled"
!(^ID3D10Blob{}+4) = !pPSBlob%

SYS ID3D10Blob.GetBufferPointer%, pPSBlob% TO bp%
SYS ID3D10Blob.GetBufferSize%, pPSBlob% TO bs%
IF hr% <> 0 OR pPixelShader% = 0 THEN
SYS ID3D10Blob.Release%, pVSBlob% 