-
Notifications
You must be signed in to change notification settings - Fork 5
/
xxGraphicD3D9PS.cpp
277 lines (240 loc) · 10.5 KB
/
xxGraphicD3D9PS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//==============================================================================
// xxGraphic : Direct3D 9.0 Programmable Shader Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#include "xxSystem.h"
#include "dxsdk/d3d9.h"
#include "dxsdk/d3dcommon.h"
#include "internal/xxGraphicInternalD3D.h"
#include "xxGraphicD3DAsm.h"
#include "xxGraphicD3D9PS.h"
//==============================================================================
// Instance
//==============================================================================
uint64_t xxCreateInstanceD3D9PS()
{
uint64_t instance = xxCreateInstanceD3D9();
if (instance == 0)
return 0;
xxRegisterFunctionSingle(xxCreateInstance, xxCreateInstanceD3D9PS);
xxRegisterFunctionSingle(xxGetInstanceName, xxGetInstanceNameD3D9PS);
xxRegisterFunctionSingle(xxCreateVertexAttribute, xxCreateVertexAttributeD3D9PS);
xxRegisterFunctionSingle(xxDestroyVertexAttribute, xxDestroyVertexAttributeD3D9PS);
xxRegisterFunctionSingle(xxCreateVertexShader, xxCreateVertexShaderD3D9PS);
xxRegisterFunctionSingle(xxCreateFragmentShader, xxCreateFragmentShaderD3D9PS);
xxRegisterFunctionSingle(xxDestroyShader, xxDestroyShaderD3D9PS);
xxRegisterFunctionSingle(xxCreatePipeline, xxCreatePipelineD3D9PS);
xxRegisterFunctionSingle(xxSetVertexBuffers, xxSetVertexBuffersD3D9PS);
xxRegisterFunctionSingle(xxSetVertexConstantBuffer, xxSetVertexConstantBufferD3D9PS);
xxRegisterFunctionSingle(xxSetFragmentConstantBuffer, xxSetFragmentConstantBufferD3D9PS);
return instance;
}
//==============================================================================
// Vertex Attribute
//==============================================================================
uint64_t xxCreateVertexAttributeD3D9PS(uint64_t device, int count, int* attribute)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(device);
if (d3dDevice == nullptr)
return 0;
D3DVERTEXATTRIBUTE9PS* d3dVertexAttribute = xxAlloc(D3DVERTEXATTRIBUTE9PS);
if (d3dVertexAttribute == nullptr)
return 0;
D3DVERTEXELEMENT9 vertexElements[32];
int normalIndex = 0;
int textureIndex = 0;
int stride = 0;
for (int i = 0; i < count; ++i)
{
int stream = (*attribute++);
int offset = (*attribute++);
int element = (*attribute++);
int size = (*attribute++);
stride += size;
D3DVERTEXELEMENT9& vertexElement = vertexElements[i];
vertexElement.Stream = stream;
vertexElement.Offset = offset;
vertexElement.Type = D3DDECLTYPE_FLOAT1;
vertexElement.Method = D3DDECLMETHOD_DEFAULT;
vertexElement.Usage;
vertexElement.UsageIndex = 0;
if (element == 'POS3' && size == sizeof(float) * 3)
{
vertexElement.Type = D3DDECLTYPE_FLOAT3;
vertexElement.Usage = D3DDECLUSAGE_POSITION;
continue;
}
if (element == 'BON3' && size == sizeof(float) * 3)
{
vertexElement.Type = D3DDECLTYPE_FLOAT3;
vertexElement.Usage = D3DDECLUSAGE_BLENDWEIGHT;
continue;
}
if (element == 'BON4' && size == sizeof(char) * 4)
{
vertexElement.Type = D3DDECLTYPE_UBYTE4;
vertexElement.Usage = D3DDECLUSAGE_BLENDINDICES;
continue;
}
if (element == 'NOR3' && size == sizeof(float) * 3)
{
vertexElement.Type = D3DDECLTYPE_FLOAT3;
switch (normalIndex)
{
case 0: vertexElement.Usage = D3DDECLUSAGE_NORMAL; break;
case 1: vertexElement.Usage = D3DDECLUSAGE_TANGENT; break;
case 2: vertexElement.Usage = D3DDECLUSAGE_BINORMAL; break;
}
normalIndex++;
continue;
}
if (element == 'COL4' && size == sizeof(char) * 4)
{
vertexElement.Type = D3DDECLTYPE_D3DCOLOR;
vertexElement.Usage = D3DDECLUSAGE_COLOR;
continue;
}
if (element == 'TEX2' && size == sizeof(float) * 2)
{
vertexElement.Type = D3DDECLTYPE_FLOAT2;
vertexElement.Usage = D3DDECLUSAGE_TEXCOORD;
vertexElement.UsageIndex = textureIndex;
textureIndex++;
continue;
}
}
D3DVERTEXELEMENT9 end = D3DDECL_END();
vertexElements[count] = end;
LPDIRECT3DVERTEXDECLARATION9 vertexDeclaration = nullptr;
HRESULT hResult = d3dDevice->CreateVertexDeclaration(vertexElements, &vertexDeclaration);
if (hResult != S_OK)
{
xxFree(d3dVertexAttribute);
return 0;
}
d3dVertexAttribute->vertexDeclaration = vertexDeclaration;
d3dVertexAttribute->stride = stride;
return reinterpret_cast<uint64_t>(d3dVertexAttribute);
}
//------------------------------------------------------------------------------
void xxDestroyVertexAttributeD3D9PS(uint64_t vertexAttribute)
{
D3DVERTEXATTRIBUTE9PS* d3dVertexAttribute = reinterpret_cast<D3DVERTEXATTRIBUTE9PS*>(vertexAttribute);
if (d3dVertexAttribute == nullptr)
return;
d3dVertexAttribute->vertexDeclaration->Release();
xxFree(d3dVertexAttribute);
}
//==============================================================================
// Shader
//==============================================================================
uint64_t xxCreateVertexShaderD3D9PS(uint64_t device, char const* shader, uint64_t vertexAttribute)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(device);
if (d3dDevice == nullptr)
return 0;
LPDIRECT3DVERTEXSHADER9 d3dShader = nullptr;
if (strcmp(shader, "default") == 0)
{
HRESULT hResult = d3dDevice->CreateVertexShader(vertexShaderCode11, &d3dShader);
if (hResult != S_OK)
return 0;
return reinterpret_cast<uint64_t>(d3dShader);
}
else
{
static char const* const macro[] = { "SHADER_HLSL", "9",
"SHADER_VERTEX", "1",
nullptr, nullptr };
ID3D10Blob* blob = D3DCompileShader(shader, macro, "Main", "vs_3_0");
if (blob == nullptr)
return 0;
HRESULT hResult = d3dDevice->CreateVertexShader((DWORD*)blob->GetBufferPointer(), &d3dShader);
blob->Release();
if (hResult != S_OK)
return 0;
return reinterpret_cast<uint64_t>(d3dShader);
}
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateFragmentShaderD3D9PS(uint64_t device, char const* shader)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(device);
if (d3dDevice == nullptr)
return 0;
LPDIRECT3DPIXELSHADER9 d3dShader = nullptr;
if (strcmp(shader, "default") == 0)
{
HRESULT hResult = d3dDevice->CreatePixelShader(pixelShaderCode10, &d3dShader);
if (hResult != S_OK)
return 0;
return reinterpret_cast<uint64_t>(d3dShader);
}
else
{
static char const* const macro[] = { "SHADER_HLSL", "9",
"SHADER_FRAGMENT", "1",
nullptr, nullptr };
ID3D10Blob* blob = D3DCompileShader(shader, macro, "Main", "ps_3_0");
if (blob == nullptr)
return 0;
HRESULT hResult = d3dDevice->CreatePixelShader((DWORD*)blob->GetBufferPointer(), &d3dShader);
blob->Release();
if (hResult != S_OK)
return 0;
return reinterpret_cast<uint64_t>(d3dShader);
}
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyShaderD3D9PS(uint64_t device, uint64_t shader)
{
LPUNKNOWN d3dShader = reinterpret_cast<LPUNKNOWN>(shader);
SafeRelease(d3dShader);
}
//==============================================================================
// Pipeline
//==============================================================================
uint64_t xxCreatePipelineD3D9PS(uint64_t device, uint64_t renderPass, uint64_t blendState, uint64_t depthStencilState, uint64_t rasterizerState, uint64_t vertexAttribute, uint64_t meshShader, uint64_t vertexShader, uint64_t fragmentShader)
{
uint64_t pipeline = xxCreatePipelineD3D9(device, renderPass, blendState, depthStencilState, rasterizerState, 0, meshShader, vertexShader, fragmentShader);
if (pipeline == 0)
return pipeline;
D3DVERTEXATTRIBUTE9PS* d3dVertexAttribute = reinterpret_cast<D3DVERTEXATTRIBUTE9PS*>(vertexAttribute);
if (d3dVertexAttribute == nullptr)
return pipeline;
LPDIRECT3DVERTEXDECLARATION9* d3dVertexDeclaration = reinterpret_cast<LPDIRECT3DVERTEXDECLARATION9*>(pipeline);
(*d3dVertexDeclaration) = d3dVertexAttribute->vertexDeclaration;
return pipeline;
}
//==============================================================================
// Command
//==============================================================================
void xxSetVertexBuffersD3D9PS(uint64_t commandEncoder, int count, const uint64_t* buffers, uint64_t vertexAttribute)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(commandEncoder);
D3DVERTEXATTRIBUTE9PS* d3dVertexAttribute = reinterpret_cast<D3DVERTEXATTRIBUTE9PS*>(vertexAttribute);
for (int i = 0; i < count; ++i)
{
LPDIRECT3DVERTEXBUFFER9 d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER9>(getResourceData(buffers[i]));
d3dDevice->SetStreamSource(i, d3dVertexBuffer, 0, d3dVertexAttribute->stride);
}
}
//------------------------------------------------------------------------------
void xxSetVertexConstantBufferD3D9PS(uint64_t commandEncoder, uint64_t buffer, int size)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(commandEncoder);
const float* d3dBuffer = reinterpret_cast<float*>(buffer);
d3dDevice->SetVertexShaderConstantF(0, d3dBuffer, size / sizeof(float) / 4);
}
//------------------------------------------------------------------------------
void xxSetFragmentConstantBufferD3D9PS(uint64_t commandEncoder, uint64_t buffer, int size)
{
LPDIRECT3DDEVICE9 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE9>(commandEncoder);
const float* d3dBuffer = reinterpret_cast<float*>(buffer);
d3dDevice->SetPixelShaderConstantF(0, d3dBuffer, size / sizeof(float) / 4);
}
//==============================================================================