~ppsspp/ppsspp/ppsspp-sdl-1.2.2

« back to all changes in this revision

Viewing changes to GPU/Directx9/helper/global.cpp

  • Committer: Sérgio Benjamim
  • Date: 2016-04-25 03:32:37 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20160425033237-776ndjdvs3r5xuzd
1.2.2 source (for SDL).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
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"
 
7
 
 
8
namespace DX9 {
 
9
 
 
10
LPDIRECT3DDEVICE9 pD3Ddevice = NULL;
 
11
LPDIRECT3DDEVICE9EX pD3DdeviceEx = NULL;
 
12
LPDIRECT3D9 pD3D = NULL;
 
13
 
 
14
void DXSetViewport(float x, float y, float w, float h, float minZ, float maxZ) {
 
15
        D3DVIEWPORT9 vp;
 
16
        vp.X = (DWORD)x;
 
17
        vp.Y = (DWORD)y;
 
18
        vp.Width = (DWORD)w;
 
19
        vp.Height = (DWORD)h;
 
20
        vp.MinZ = minZ;
 
21
        vp.MaxZ = maxZ;
 
22
        pD3Ddevice->SetViewport(&vp);
 
23
}
 
24
 
 
25
static const char * vscode =
 
26
  "struct VS_IN {\n"
 
27
  "  float4 ObjPos   : POSITION;\n"
 
28
  "  float2 Uv    : TEXCOORD0;\n"
 
29
  "};"
 
30
  "struct VS_OUT {\n"
 
31
  "  float4 ProjPos  : POSITION;\n"
 
32
  "  float2 Uv    : TEXCOORD0;\n"
 
33
  "};\n"
 
34
  "VS_OUT main( VS_IN In ) {\n"
 
35
  "  VS_OUT Out;\n"
 
36
  "  Out.ProjPos = In.ObjPos;\n"
 
37
  "  Out.Uv = In.Uv;\n"
 
38
  "  return Out;\n"
 
39
  "}\n";
 
40
 
 
41
//--------------------------------------------------------------------------------------
 
42
// Pixel shader
 
43
//--------------------------------------------------------------------------------------
 
44
static const char * pscode =
 
45
  "sampler s: register(s0);\n"
 
46
  "struct PS_IN {\n"
 
47
  "  float2 Uv : TEXCOORD0;\n"
 
48
  "};\n"
 
49
  "float4 main( PS_IN In ) : COLOR {\n"
 
50
  "  float4 c =  tex2D(s, In.Uv);\n"
 
51
  "  return c;\n"
 
52
  "}\n";
 
53
 
 
54
IDirect3DVertexDeclaration9* pFramebufferVertexDecl = NULL;
 
55
 
 
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 },
 
59
        D3DDECL_END()
 
60
};
 
61
 
 
62
IDirect3DVertexDeclaration9* pSoftVertexDecl = NULL;
 
63
 
 
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 },
 
69
        D3DDECL_END()
 
70
};
 
71
 
 
72
LPDIRECT3DVERTEXSHADER9      pFramebufferVertexShader = NULL; // Vertex Shader
 
73
LPDIRECT3DPIXELSHADER9       pFramebufferPixelShader = NULL;  // Pixel Shader
 
74
 
 
75
bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
 
76
        ID3DXBuffer *pShaderCode = nullptr;
 
77
        ID3DXBuffer *pErrorMsg = nullptr;
 
78
 
 
79
        // Compile pixel shader.
 
80
        HRESULT hr = dyn_D3DXCompileShader(code,
 
81
                (UINT)strlen(code),
 
82
                nullptr,
 
83
                nullptr,
 
84
                "main",
 
85
                "ps_2_0",
 
86
                0,
 
87
                &pShaderCode,
 
88
                &pErrorMsg,
 
89
                pShaderTable);
 
90
 
 
91
        if (pErrorMsg) {
 
92
                errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
 
93
                pErrorMsg->Release();
 
94
        } else if (FAILED(hr)) {
 
95
                errorMessage = GetStringErrorMsg(hr);
 
96
        } else {
 
97
                errorMessage = "";
 
98
        }
 
99
 
 
100
        if (FAILED(hr) || !pShaderCode) {
 
101
                if (pShaderCode)
 
102
                        pShaderCode->Release();
 
103
                return false;
 
104
        }
 
105
 
 
106
        // Create pixel shader.
 
107
        pD3Ddevice->CreatePixelShader( (DWORD*)pShaderCode->GetBufferPointer(), 
 
108
                pShader );
 
109
 
 
110
        pShaderCode->Release();
 
111
 
 
112
        return true;
 
113
}
 
114
 
 
115
bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
 
116
        ID3DXBuffer *pShaderCode = nullptr;
 
117
        ID3DXBuffer *pErrorMsg = nullptr;
 
118
 
 
119
        // Compile pixel shader.
 
120
        HRESULT hr = dyn_D3DXCompileShader(code,
 
121
                (UINT)strlen(code),
 
122
                nullptr,
 
123
                nullptr,
 
124
                "main",
 
125
                "vs_2_0",
 
126
                0,
 
127
                &pShaderCode,
 
128
                &pErrorMsg,
 
129
                pShaderTable);
 
130
 
 
131
        if (pErrorMsg) {
 
132
                errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
 
133
                pErrorMsg->Release();
 
134
        } else if (FAILED(hr)) {
 
135
                errorMessage = GetStringErrorMsg(hr);
 
136
        } else {
 
137
                errorMessage = "";
 
138
        }
 
139
 
 
140
        if (FAILED(hr) || !pShaderCode) {
 
141
                if (pShaderCode)
 
142
                        pShaderCode->Release();
 
143
                return false;
 
144
        }
 
145
 
 
146
        // Create pixel shader.
 
147
        pD3Ddevice->CreateVertexShader( (DWORD*)pShaderCode->GetBufferPointer(), 
 
148
                pShader );
 
149
 
 
150
        pShaderCode->Release();
 
151
 
 
152
        return true;
 
153
}
 
154
 
 
155
bool CompileShaders(std::string &errorMsg) {
 
156
        if (!CompileVertexShader(vscode, &pFramebufferVertexShader, NULL, errorMsg)) {
 
157
                OutputDebugStringA(errorMsg.c_str());
 
158
                return false;
 
159
        }
 
160
 
 
161
        if (!CompilePixelShader(pscode, &pFramebufferPixelShader, NULL, errorMsg)) {
 
162
                OutputDebugStringA(errorMsg.c_str());
 
163
                if (pFramebufferVertexShader) {
 
164
                        pFramebufferVertexShader->Release();
 
165
                }
 
166
                return false;
 
167
        }
 
168
 
 
169
        pD3Ddevice->CreateVertexDeclaration(VertexElements, &pFramebufferVertexDecl);
 
170
        pD3Ddevice->SetVertexDeclaration(pFramebufferVertexDecl);
 
171
        pD3Ddevice->CreateVertexDeclaration(SoftTransVertexElements, &pSoftVertexDecl);
 
172
 
 
173
        return true;
 
174
}
 
175
 
 
176
void DestroyShaders() {
 
177
        if (pFramebufferVertexShader) {
 
178
                pFramebufferVertexShader->Release();
 
179
        }
 
180
        if (pFramebufferPixelShader) {
 
181
                pFramebufferPixelShader->Release();
 
182
        }
 
183
        if (pFramebufferVertexDecl) {
 
184
                pFramebufferVertexDecl->Release();
 
185
        }
 
186
        if (pSoftVertexDecl) {
 
187
                pSoftVertexDecl->Release();
 
188
        }
 
189
}
 
190
 
 
191
// Only used by Headless! TODO: Remove
 
192
void DirectxInit(HWND window) {
 
193
        pD3D = Direct3DCreate9( D3D_SDK_VERSION );
 
194
 
 
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 
 
200
        // format.
 
201
        D3DPRESENT_PARAMETERS d3dpp;
 
202
        ZeroMemory(&d3dpp, sizeof(d3dpp));
 
203
        // TODO?
 
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;
 
214
 
 
215
        HRESULT hr = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
 
216
                                      D3DCREATE_HARDWARE_VERTEXPROCESSING,
 
217
                                      &d3dpp, &pD3Ddevice);
 
218
        if (hr != D3D_OK) {
 
219
                // TODO
 
220
        }
 
221
 
 
222
#ifdef _XBOX
 
223
        pD3Ddevice->SetRingBufferParameters( &d3dr );
 
224
#endif
 
225
 
 
226
        std::string errorMessage;
 
227
        CompileShaders(errorMessage);
 
228
 
 
229
        fbo_init(pD3D);
 
230
}
 
231
 
 
232
};