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 1 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.
In this first tutorial, we will go through the elements necessary to create a minimal Direct3D 11 application. Every Direct3D 11 application must have these elements to function properly. The elements include setting up a window and a device object, and then displaying a colour on the window.
The source files, libraries etc. may be downloaded from here.
The following is the code to create a device and a swap chain:
DIM sd{} = DXGI_SWAP_CHAIN_DESC{} sd.BufferCount% = 1 sd.BufferDesc.Width% = Width% sd.BufferDesc.Height% = Height% sd.BufferDesc.Format% = DXGI_FORMAT_R8G8B8A8_UNORM sd.BufferDesc.RefreshRate.Numerator% = 60 sd.BufferDesc.RefreshRate.Denominator% = 1 sd.BufferUsage% = DXGI_USAGE_RENDER_TARGET_OUTPUT sd.OutputWindow% = @hwnd% sd.SampleDesc.Count% = 1 sd.SampleDesc.Quality% = 0 sd.Windowed% = 1 SYS `D3D11CreateDeviceAndSwapChain`, NULL, D3D_DRIVER_TYPE_HARDWARE, \ \ NULL, 0, NULL, 0, D3D11_SDK_VERSION, sd{}, ^pSwapChain%, \ \ ^pd3dDevice%, NULL, ^pImmediateContext% TO hr% IF hr% <> S_OK ERROR 100, "CreateDeviceAndSwapChain failed: "+STR$~hr% !(^IDXGISwapChain{}+4) = !pSwapChain% !(^ID3D11Device{}+4) = !pd3dDevice% !(^ID3D11DeviceContext{}+4) = !pImmediateContext%
The code to create and set the render target view is as follows:
REM Create a render target view: SYS IDXGISwapChain.GetBuffer%, pSwapChain%, 0, IID_ID3D11Texture2D, \ \ ^pBackBuffer% TO hr% IF hr% <> S_OK ERROR 100, "IDXGISwapChain::GetBuffer failed: "+STR$~hr% !(^ID3D11Texture2D{}+4) = !pBackBuffer% SYS ID3D11Device.CreateRenderTargetView%, pd3dDevice%, pBackBuffer%, NULL, \ \ ^pRenderTargetView% TO hr% IF hr% <> S_OK ERROR 100, "ID3D11Device::CreateRenderTargetView failed: "+STR$~hr% SYS ID3D11Texture2D.Release%, pBackBuffer% SYS ID3D11DeviceContext.OMSetRenderTargets%, pImmediateContext%, \ \ 1, ^pRenderTargetView%, NULL
The last thing we need to set up before Direct3D 11 can render is initialize the viewport:
DIM vp{} = D3D11_VIEWPORT{} : REM All members are floats vp.Width% = FN_f4(Width%) vp.Height% = FN_f4(Height%) vp.MinDepth% = 0 vp.MaxDepth% = FN_f4(1.0) vp.TopLeftX% = 0 vp.TopLeftY% = 0 SYS ID3D11DeviceContext.RSSetViewports%, pImmediateContext%, 1, vp{}
In this tutorial, we will render the simplest scene possible, which is to fill the screen with a single colour:
REM Clear the back buffer: DIM ClearColor%(3) ClearColor%() = 0, FN_f4(0.125), FN_f4(0.6), FN_f4(1.0) : REM RGBA SYS ID3D11DeviceContext.ClearRenderTargetView%, pImmediateContext%, \ \ pRenderTargetView%, ^ClearColor%(0) REM Present the information rendered to the back buffer: SYS IDXGISwapChain.Present%, pSwapChain%, 0, 0