unit DXSprite;
interface
uses
Windows,
SysUtils,
Classes,
Messages,
D3D11_JSB,
D3DX11_JSB,
D3D10_JSB,
D3DX10_JSB,
DXGI_JSB,
D3DCommon_JSB,
DXTypes_JSB,
D3DCompiler_JSB;
const
FXFile = '
C:\Tutorial03.fx';
type TD3DXVECTOR3 =
record
X:Single;
Y:Single;
Z:Single;
end;
type SimpleVertex =
record
Pos :TD3DXVECTOR3 ;
end;
type TDX11Sprite =
class
private
g_pd3dDevice : ID3D11Device;
g_pImmediateContext : ID3D11DeviceContext;
g_pVertexShader : ID3D11VertexShader;
g_pPixelShader : ID3D11PixelShader;
g_pVertexLayout : ID3D11InputLayout;
g_pVertexBuffer : ID3D11Buffer;
public
constructor Create(Device:ID3D11Device);
destructor Destroy;
override;
procedure Render;
end;
function DXVECTOR3(v1,v2,v3:Single):TD3DXVECTOR3;
implementation
constructor TDX11Sprite.Create(Device:ID3D11Device);
var
pVSBlob : ID3DBlob;
pPSBlob : ID3DBlob;
eM : ID3DBlob;
layout : D3D11_INPUT_ELEMENT_DESC;
vertices :
Array[0..2]
of SimpleVertex;
bd : D3D11_BUFFER_DESC;
InitData : D3D11_SUBRESOURCE_DATA;
stride,offset : Integer;
begin
g_pd3dDevice := Device;
g_pd3dDevice.GetImmediateContext(g_pImmediateContext);
g_pVertexShader :=nil;
g_pPixelShader :=nil;
g_pVertexLayout :=nil;
g_pVertexBuffer :=nil;
pVSBlob :=
nil;
D3DX11CompileFromFile(PChar(FXFile),
nil,
nil,PChar('
VS'),PChar('
vs_4_0'),
D3DCOMPILE_ENABLE_STRICTNESS, 0,
nil,pVSBlob,eM,
nil);
g_pd3dDevice.CreateVertexShader(pVSBlob.GetBufferPointer,pVSBlob.GetBufferSize,
nil,g_pVertexShader);
layout.SemanticName := '
POSITION';
layout.SemanticIndex := 0;
layout.Format := DXGI_FORMAT_R32G32B32_FLOAT;
layout.InputSlot := 0;
layout.AlignedByteOffset := 0;
layout.InputSlotClass := D3D11_INPUT_PER_VERTEX_DATA;
layout.InstanceDataStepRate := 0;
g_pd3dDevice.CreateInputLayout(@layout,1, pVSBlob.GetBufferPointer,
pVSBlob.GetBufferSize,g_pVertexLayout);
pVSBlob :=
nil;
g_pImmediateContext.IASetInputLayout(g_pVertexLayout);
D3DX11CompileFromFile(PChar(FXFile),
nil,
nil,PChar('
PS'),PChar('
ps_4_0'),
D3DCOMPILE_ENABLE_STRICTNESS, 0,
nil,pPSBlob,eM,
nil);
g_pd3dDevice.CreatePixelShader(pPSBlob.GetBufferPointer,pPSBlob.GetBufferSize,
nil,g_pPixelShader);
pPSBlob :=
nil;
vertices[0].Pos := DXVECTOR3(0.0,0.5,0.5);
vertices[1].Pos := DXVECTOR3(0.0,-0.5,0.5);
vertices[2].Pos := DXVECTOR3(-0.0,-0.5,0.5);
ZeroMemory(@bd,sizeof(bd));
bd.Usage := D3D11_USAGE_DEFAULT;
bd.ByteWidth := sizeof(SimpleVertex)*3;
bd.BindFlags := D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags := 0;
ZeroMemory(@InitData,sizeof(InitData));
InitData.pSysMem := @vertices;
g_pd3dDevice.CreateBuffer(bd,@InitData,g_pVertexBuffer);
stride := sizeof(SimpleVertex );
offset := 0;
g_pImmediateContext.IASetVertexBuffers(0,1,@g_pVertexBuffer,@stride,@offset);
g_pImmediateContext.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
end;
destructor TDX11Sprite.Destroy;
begin
if not(g_pVertexShader=nil)
then g_pVertexShader :=
nil;
if not(g_pPixelShader=nil)
then g_pPixelShader :=
nil;
if not(g_pVertexLayout=nil)
then g_pVertexLayout :=
nil;
if not(g_pVertexBuffer=nil)
then g_pVertexBuffer :=
nil;
if not(g_pImmediateContext=nil)
then g_pImmediateContext :=
nil;
if not(g_pd3dDevice=nil)
then g_pd3dDevice :=
nil;
end;
procedure TDX11Sprite.Render;
begin
g_pImmediateContext.VSSetShader(g_pVertexShader,
nil,0);
g_pImmediateContext.PSSetShader(g_pPixelShader,
nil,0);
g_pImmediateContext.Draw(3,0);
end;
function DXVECTOR3(v1,v2,v3:Single):TD3DXVECTOR3;
begin
with result
do begin
x := v1;
y := v2;
z := v3;
end;
end;
end.