3
#include "GPU/Directx9/helper/global.h"
4
#include "GPU/Directx9/helper/dx_fbo.h"
5
#include "thin3d/d3dx9_loader.h"
6
#include "Common/CommonFuncs.h"
10
LPDIRECT3DDEVICE9 pD3Ddevice = NULL;
11
LPDIRECT3DDEVICE9EX pD3DdeviceEx = NULL;
12
LPDIRECT3D9 pD3D = NULL;
14
void DXSetViewport(float x, float y, float w, float h, float minZ, float maxZ) {
22
pD3Ddevice->SetViewport(&vp);
25
static const char * vscode =
27
" float4 ObjPos : POSITION;\n"
28
" float2 Uv : TEXCOORD0;\n"
31
" float4 ProjPos : POSITION;\n"
32
" float2 Uv : TEXCOORD0;\n"
34
"VS_OUT main( VS_IN In ) {\n"
36
" Out.ProjPos = In.ObjPos;\n"
41
//--------------------------------------------------------------------------------------
43
//--------------------------------------------------------------------------------------
44
static const char * pscode =
45
"sampler s: register(s0);\n"
47
" float2 Uv : TEXCOORD0;\n"
49
"float4 main( PS_IN In ) : COLOR {\n"
50
" float4 c = tex2D(s, In.Uv);\n"
54
IDirect3DVertexDeclaration9* pFramebufferVertexDecl = NULL;
56
static const D3DVERTEXELEMENT9 VertexElements[] = {
57
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
58
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
62
IDirect3DVertexDeclaration9* pSoftVertexDecl = NULL;
64
static const D3DVERTEXELEMENT9 SoftTransVertexElements[] = {
65
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
66
{ 0, 16, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
67
{ 0, 28, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
68
{ 0, 32, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1 },
72
LPDIRECT3DVERTEXSHADER9 pFramebufferVertexShader = NULL; // Vertex Shader
73
LPDIRECT3DPIXELSHADER9 pFramebufferPixelShader = NULL; // Pixel Shader
75
bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
76
ID3DXBuffer *pShaderCode = nullptr;
77
ID3DXBuffer *pErrorMsg = nullptr;
79
// Compile pixel shader.
80
HRESULT hr = dyn_D3DXCompileShader(code,
92
errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
94
} else if (FAILED(hr)) {
95
errorMessage = GetStringErrorMsg(hr);
100
if (FAILED(hr) || !pShaderCode) {
102
pShaderCode->Release();
106
// Create pixel shader.
107
pD3Ddevice->CreatePixelShader( (DWORD*)pShaderCode->GetBufferPointer(),
110
pShaderCode->Release();
115
bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
116
ID3DXBuffer *pShaderCode = nullptr;
117
ID3DXBuffer *pErrorMsg = nullptr;
119
// Compile pixel shader.
120
HRESULT hr = dyn_D3DXCompileShader(code,
132
errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
133
pErrorMsg->Release();
134
} else if (FAILED(hr)) {
135
errorMessage = GetStringErrorMsg(hr);
140
if (FAILED(hr) || !pShaderCode) {
142
pShaderCode->Release();
146
// Create pixel shader.
147
pD3Ddevice->CreateVertexShader( (DWORD*)pShaderCode->GetBufferPointer(),
150
pShaderCode->Release();
155
bool CompileShaders(std::string &errorMsg) {
156
if (!CompileVertexShader(vscode, &pFramebufferVertexShader, NULL, errorMsg)) {
157
OutputDebugStringA(errorMsg.c_str());
161
if (!CompilePixelShader(pscode, &pFramebufferPixelShader, NULL, errorMsg)) {
162
OutputDebugStringA(errorMsg.c_str());
163
if (pFramebufferVertexShader) {
164
pFramebufferVertexShader->Release();
169
pD3Ddevice->CreateVertexDeclaration(VertexElements, &pFramebufferVertexDecl);
170
pD3Ddevice->SetVertexDeclaration(pFramebufferVertexDecl);
171
pD3Ddevice->CreateVertexDeclaration(SoftTransVertexElements, &pSoftVertexDecl);
176
void DestroyShaders() {
177
if (pFramebufferVertexShader) {
178
pFramebufferVertexShader->Release();
180
if (pFramebufferPixelShader) {
181
pFramebufferPixelShader->Release();
183
if (pFramebufferVertexDecl) {
184
pFramebufferVertexDecl->Release();
186
if (pSoftVertexDecl) {
187
pSoftVertexDecl->Release();
191
// Only used by Headless! TODO: Remove
192
void DirectxInit(HWND window) {
193
pD3D = Direct3DCreate9( D3D_SDK_VERSION );
195
// Set up the structure used to create the D3DDevice. Most parameters are
196
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
197
// window, and then set the SwapEffect to "discard", which is the most
198
// efficient method of presenting the back buffer to the display. And
199
// we request a back buffer format that matches the current desktop display
201
D3DPRESENT_PARAMETERS d3dpp;
202
ZeroMemory(&d3dpp, sizeof(d3dpp));
204
d3dpp.Windowed = TRUE;
205
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
206
d3dpp.MultiSampleQuality = 0;
207
d3dpp.BackBufferCount = 1;
208
d3dpp.EnableAutoDepthStencil = TRUE;
209
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
210
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
211
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
212
//d3dpp.PresentationInterval = (useVsync == true)?D3DPRESENT_INTERVAL_ONE:D3DPRESENT_INTERVAL_IMMEDIATE;
213
//d3dpp.RingBufferParameters = d3dr;
215
HRESULT hr = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
216
D3DCREATE_HARDWARE_VERTEXPROCESSING,
217
&d3dpp, &pD3Ddevice);
223
pD3Ddevice->SetRingBufferParameters( &d3dr );
226
std::string errorMessage;
227
CompileShaders(errorMessage);