summaryrefslogtreecommitdiff
path: root/Source/Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp
blob: cfb74df22591783f4c5776781299f8d0ae39c139 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include "Common.h"
#include "StringUtil.h"

#include "D3DBase.h"
#include "D3DUtil.h"
#include "Render.h"
#include "PixelShaderCache.h"
#include "VertexShaderCache.h"

namespace DX9
{

namespace D3D
{
CD3DFont font;

#define MAX_NUM_VERTICES 50*6
struct FONT2DVERTEX {
	float x,y,z;
	float rhw;
	u32 color;
	float tu, tv;
};

#define D3DFVF_FONT2DVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
#define D3DFVF_FONT3DVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL|D3DFVF_TEX1)

inline FONT2DVERTEX InitFont2DVertex(float x, float y, u32 color, float tu, float tv)
{
	FONT2DVERTEX v;   v.x=x; v.y=y; v.z=0; v.rhw=1.0f;  v.color = color;   v.tu = tu;   v.tv = tv;
	return v;
}

CD3DFont::CD3DFont()
{
	m_pTexture			   = NULL;
	m_pVB				   = NULL;
}

enum {m_dwTexWidth = 512, m_dwTexHeight = 512};

int CD3DFont::Init()
{
	// Create vertex buffer for the letters
	HRESULT hr;
	if (FAILED(hr = dev->CreateVertexBuffer(MAX_NUM_VERTICES*sizeof(FONT2DVERTEX),
		D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &m_pVB, NULL)))
	{
		return hr;
	}
	m_fTextScale  = 1.0f; // Draw fonts into texture without scaling

	// Prepare to create a bitmap
	unsigned int* pBitmapBits;
	BITMAPINFO bmi;
	ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
	bmi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth       =  (int)m_dwTexWidth;
	bmi.bmiHeader.biHeight      = -(int)m_dwTexHeight;
	bmi.bmiHeader.biPlanes      = 1;
	bmi.bmiHeader.biCompression = BI_RGB;
	bmi.bmiHeader.biBitCount    = 32;

	// Create a DC and a bitmap for the font
	HDC hDC = CreateCompatibleDC(NULL);
	HBITMAP hbmBitmap = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);
	SetMapMode(hDC, MM_TEXT);

	// Create a font.  By specifying ANTIALIASED_QUALITY, we might get an
	// antialiased font, but this is not guaranteed.
	// We definitely don't want to get it cleartype'd, anyway.
	int m_dwFontHeight = 24;
	int nHeight = -MulDiv(m_dwFontHeight, int(GetDeviceCaps(hDC, LOGPIXELSY) * m_fTextScale), 72);
	int dwBold = FW_NORMAL; ///FW_BOLD
	HFONT hFont = CreateFont(nHeight, 0, 0, 0, dwBold, 0,
		FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
		VARIABLE_PITCH, _T("Tahoma"));
	if (NULL == hFont)
		return E_FAIL;

	HGDIOBJ hOldbmBitmap = SelectObject(hDC, hbmBitmap);
	HGDIOBJ hOldFont = SelectObject(hDC, hFont);

	// Set text properties
	SetTextColor(hDC, 0xFFFFFF);
	SetBkColor  (hDC, 0);
	SetTextAlign(hDC, TA_TOP);

	// Loop through all printable characters and output them to the bitmap
	// Meanwhile, keep track of the corresponding tex coords for each character.
	int x = 0, y = 0;
	char str[2] = "\0";
	for (int c = 0; c < 127 - 32; c++)
	{
		str[0] = c + 32;
		SIZE size;
		GetTextExtentPoint32A(hDC, str, 1, &size);
		if ((int)(x+size.cx+1) > m_dwTexWidth)
		{
			x  = 0;
			y += size.cy + 1;
		}

		ExtTextOutA(hDC, x+1, y+0, ETO_OPAQUE | ETO_CLIPPED, NULL, str, 1, NULL);
		m_fTexCoords[c][0] = ((float)(x+0))/m_dwTexWidth;
		m_fTexCoords[c][1] = ((float)(y+0))/m_dwTexHeight;
		m_fTexCoords[c][2] = ((float)(x+0+size.cx))/m_dwTexWidth;
		m_fTexCoords[c][3] = ((float)(y+0+size.cy))/m_dwTexHeight;

		x += size.cx + 3;  // 3 to work around annoying ij conflict (part of the j ends up with the i)
	}

	// Create a new texture for the font
	// possible optimization: store the converted data in a buffer and fill the texture on creation.
	//							That way, we can use a static texture
	hr = dev->CreateTexture(m_dwTexWidth, m_dwTexHeight, 1, D3DUSAGE_DYNAMIC,
							D3DFMT_A4R4G4B4, D3DPOOL_DEFAULT, &m_pTexture, NULL);
	if (FAILED(hr))
	{
		PanicAlert("Failed to create font texture");
		return hr;
	}

	// Lock the surface and write the alpha values for the set pixels
	D3DLOCKED_RECT d3dlr;
	m_pTexture->LockRect(0, &d3dlr, 0, D3DLOCK_DISCARD);
	int bAlpha; // 4-bit measure of pixel intensity

	for (y = 0; y < m_dwTexHeight; y++)
	{
		u16 *pDst16 = (u16*)((u8 *)d3dlr.pBits + y * d3dlr.Pitch);
		for (x = 0; x < m_dwTexWidth; x++)
		{
			bAlpha = ((pBitmapBits[m_dwTexWidth * y + x] & 0xff) >> 4);
			pDst16[x] = (bAlpha << 12) | 0x0fff;
		}
	}

	// Done updating texture, so clean up used objects
	m_pTexture->UnlockRect(0);

	SelectObject(hDC, hOldbmBitmap);
	DeleteObject(hbmBitmap);

	SelectObject(hDC, hOldFont);
	DeleteObject(hFont);

	return S_OK;
}

int CD3DFont::Shutdown()
{
	m_pVB->Release();
	m_pVB = NULL;
	m_pTexture->Release();
	m_pTexture = NULL;
	return S_OK;
}


const int RS[6][2] =
{
	{D3DRS_ALPHABLENDENABLE, TRUE},
	{D3DRS_SRCBLEND,		 D3DBLEND_SRCALPHA},
	{D3DRS_DESTBLEND,		 D3DBLEND_INVSRCALPHA},
	{D3DRS_CULLMODE,		 D3DCULL_NONE},
	{D3DRS_ZENABLE,			 FALSE},
	{D3DRS_FOGENABLE,		 FALSE},
};
const int TS[6][2] = 
{
	{D3DTSS_COLOROP,   D3DTOP_MODULATE},
	{D3DTSS_COLORARG1, D3DTA_TEXTURE},
	{D3DTSS_COLORARG2, D3DTA_DIFFUSE },
	{D3DTSS_ALPHAOP,   D3DTOP_MODULATE },
	{D3DTSS_ALPHAARG1, D3DTA_TEXTURE },
	{D3DTSS_ALPHAARG2, D3DTA_DIFFUSE },
};

void RestoreShaders()
{
	D3D::SetTexture(0, 0);
	D3D::RefreshVertexDeclaration();
	D3D::RefreshPixelShader();
	D3D::RefreshVertexShader();
}

void RestoreRenderStates()
{
	RestoreShaders();
	for (int i = 0; i < 6; i++)
	{
		D3D::RefreshRenderState((_D3DRENDERSTATETYPE)RS[i][0]);
		D3D::RefreshTextureStageState(0, (_D3DTEXTURESTAGESTATETYPE)int(TS[i][0]));
	}
}

void CD3DFont::SetRenderStates()
{
	D3D::SetTexture(0, m_pTexture);

	dev->SetPixelShader(0);
	dev->SetVertexShader(0);
	
	dev->SetFVF(D3DFVF_FONT2DVERTEX);

	for (int i = 0; i < 6; i++)
	{
		D3D::ChangeRenderState((_D3DRENDERSTATETYPE)RS[i][0], RS[i][1]);
		D3D::ChangeTextureStageState(0, (_D3DTEXTURESTAGESTATETYPE)int(TS[i][0]), TS[i][1]);
	}
}


int CD3DFont::DrawTextScaled(float x, float y, float fXScale, float fYScale, float spacing, u32 dwColor, const char* strText)
{
	if (!m_pVB)
		return 0;

	SetRenderStates();
	dev->SetStreamSource(0, m_pVB, 0, sizeof(FONT2DVERTEX));

	float vpWidth = 1;
	float vpHeight = 1;

	float sx = x*vpWidth-0.5f;
	float sy = y*vpHeight-0.5f;

	float fStartX = sx;

	float invLineHeight = 1.0f / ((m_fTexCoords[0][3] - m_fTexCoords[0][1]) * m_dwTexHeight);
	// Fill vertex buffer
	FONT2DVERTEX* pVertices;
	int dwNumTriangles = 0L;
	m_pVB->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);

	const char *oldstrText=strText;
	//First, let's measure the text
	float tw=0;
	float mx=0;
	float maxx=0;

	while (*strText)
	{
		char c = *strText++;

		if (c == ('\n'))
			mx = 0;
		if (c < (' '))
			continue;

		float tx1 = m_fTexCoords[c-32][0];
		float tx2 = m_fTexCoords[c-32][2];

		float w = (tx2-tx1)*m_dwTexWidth;
		w *= (fXScale*vpHeight)*invLineHeight;
		mx += w + spacing*fXScale*vpWidth;
		if (mx > maxx) maxx = mx;
	}

	float offset = -maxx/2;
	strText = oldstrText;

	float wScale = (fXScale*vpHeight)*invLineHeight;
	float hScale = (fYScale*vpHeight)*invLineHeight;

	// Let's draw it
	while (*strText)
	{
		char c = *strText++;

		if (c == ('\n'))
		{
			sx  = fStartX;
			sy += fYScale*vpHeight;
		}
		if (c < (' '))
			continue;

		c -= 32;
		float tx1 = m_fTexCoords[c][0];
		float ty1 = m_fTexCoords[c][1];
		float tx2 = m_fTexCoords[c][2];
		float ty2 = m_fTexCoords[c][3];

		float w = (tx2-tx1)*m_dwTexWidth;
		float h = (ty2-ty1)*m_dwTexHeight;

		w *= wScale;
		h *= hScale;

		FONT2DVERTEX v[6];
		v[0] = InitFont2DVertex(sx,   sy+h, dwColor, tx1, ty2);
		v[1] = InitFont2DVertex(sx,   sy,   dwColor, tx1, ty1);
		v[2] = InitFont2DVertex(sx+w, sy+h, dwColor, tx2, ty2);
		v[3] = InitFont2DVertex(sx+w, sy,   dwColor, tx2, ty1);
		v[4] = v[2];
		v[5] = v[1];

		memcpy(pVertices, v, 6*sizeof(FONT2DVERTEX));

		pVertices+=6;
		dwNumTriangles += 2;

		if (dwNumTriangles * 3 > (MAX_NUM_VERTICES - 6))
		{
			// Unlock, render, and relock the vertex buffer
			m_pVB->Unlock();
			dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, dwNumTriangles);
			m_pVB->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
			dwNumTriangles = 0;
		}
		sx += w + spacing*fXScale*vpWidth;
	}

	// Unlock and render the vertex buffer
	m_pVB->Unlock();
	if (dwNumTriangles > 0)
		dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, dwNumTriangles);
	RestoreRenderStates();
	return S_OK;
}

/* Explanation of texture copying via drawShadedTexQuad and drawShadedTexSubQuad:
	From MSDN: "When rendering 2D output using pre-transformed vertices,
				care must be taken to ensure that each texel area correctly corresponds
				to a single pixel area, otherwise texture distortion can occur."
	=> We need to subtract 0.5 from the vertex positions to properly map texels to pixels.
	HOWEVER, the MSDN article talks about D3DFVF_XYZRHW vertices, which bypass the programmable pipeline.
	Since we're using D3DFVF_XYZW and the programmable pipeline though, the vertex positions
	are normalized to [-1;+1], i.e. we need to scale the -0.5 offset by the texture dimensions.
	For example see a texture with a width of 640 pixels:
	"Expected" coordinate range when using D3DFVF_XYZRHW: [0;640]
	Normalizing this coordinate range for D3DFVF_XYZW: [0;640]->[-320;320]->[-1;1]
		i.e. we're subtracting width/2 and dividing by width/2
	BUT: The actual range when using D3DFVF_XYZRHW needs to be [-0.5;639.5] because of the need for exact texel->pixel mapping.
	We can still apply the same normalizing procedure though:
		[-0.5;639.5]->[-320-0.5;320-0.5]->[-1-0.5/320;1-0.5/320]

	So generally speaking the correct coordinate range is [-1-0.5/(w/2);1-0.5/(w/2)]
	which can be simplified to [-1-1/w;1-1/w].

	Note that while for D3DFVF_XYZRHW the y coordinate of the bottom of the screen is positive,
		it's negative for D3DFVF_XYZW. This is why we need to _add_ 1/h for the second position component instead of subtracting it.

	For a detailed explanation of this read the MSDN article "Directly Mapping Texels to Pixels (Direct3D 9)".
*/
void drawShadedTexQuad(IDirect3DTexture9 *texture,
					   const RECT *rSource,
					   int SourceWidth,
					   int SourceHeight,
					   int DestWidth,
					   int DestHeight,
					   IDirect3DPixelShader9 *PShader,
					   IDirect3DVertexShader9 *Vshader,
					   float Gamma)
{
	float sw = 1.0f /(float) SourceWidth;
	float sh = 1.0f /(float) SourceHeight;
	float dw = 1.0f /(float) DestWidth;
	float dh = 1.0f /(float) DestHeight;
	float u1=((float)rSource->left) * sw;
	float u2=((float)rSource->right) * sw;
	float v1=((float)rSource->top) * sh;
	float v2=((float)rSource->bottom) * sh;
	float g = 1.0f/Gamma;

	struct Q2DVertex { float x,y,z,rhw,u,v,w,h,G; } coords[4] = {
		{-1.0f - dw,-1.0f + dh, 0.0f,1.0f, u1, v2, sw, sh, g},
		{-1.0f - dw, 1.0f + dh, 0.0f,1.0f, u1, v1, sw, sh, g},
		{ 1.0f - dw,-1.0f + dh, 0.0f,1.0f, u2, v2, sw, sh, g},
		{ 1.0f - dw, 1.0f + dh, 0.0f,1.0f, u2, v1, sw, sh, g}
	};
	dev->SetVertexShader(Vshader);
	dev->SetPixelShader(PShader);
	D3D::SetTexture(0, texture);
	dev->SetFVF(D3DFVF_XYZW | D3DFVF_TEX3 |  D3DFVF_TEXCOORDSIZE1(2));
	dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, coords, sizeof(Q2DVertex));
	RestoreShaders();
}

void drawShadedTexSubQuad(IDirect3DTexture9 *texture,
							const MathUtil::Rectangle<float> *rSource,
							int SourceWidth,
							int SourceHeight,
							const MathUtil::Rectangle<float> *rDest,
							int DestWidth,
							int DestHeight,
							IDirect3DPixelShader9 *PShader,
							IDirect3DVertexShader9 *Vshader,
							float Gamma)
{
	float sw = 1.0f /(float) SourceWidth;
	float sh = 1.0f /(float) SourceHeight;
	float dw = 1.0f /(float) DestWidth;
	float dh = 1.0f /(float) DestHeight;
	float u1= rSource->left * sw;
	float u2= rSource->right * sw;
	float v1= rSource->top * sh;
	float v2= rSource->bottom * sh;
	float g = 1.0f/Gamma;

	struct Q2DVertex { float x,y,z,rhw,u,v,w,h,G; } coords[4] = {
		{ rDest->left  - dw , rDest->top    + dh, 1.0f,1.0f, u1, v2, sw, sh, g},
		{ rDest->left  - dw , rDest->bottom + dh, 1.0f,1.0f, u1, v1, sw, sh, g},
		{ rDest->right - dw , rDest->top    + dh, 1.0f,1.0f, u2, v2, sw, sh, g},
		{ rDest->right - dw , rDest->bottom + dh, 1.0f,1.0f, u2, v1, sw, sh, g}
	};
	dev->SetVertexShader(Vshader);
	dev->SetPixelShader(PShader);
	D3D::SetTexture(0, texture);
	dev->SetFVF(D3DFVF_XYZW | D3DFVF_TEX3 |  D3DFVF_TEXCOORDSIZE1(2));
	dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, coords, sizeof(Q2DVertex));
	RestoreShaders();
}

// Fills a certain area of the current render target with the specified color
// Z buffer disabled; destination coordinates normalized to (-1;1)
void drawColorQuad(int DestWidth, int DestHeight, u32 Color, float x1, float y1, float x2, float y2)
{
	float dw = 1.f / (float)DestWidth;
	float dh = 1.f / (float)DestHeight;
	struct CQVertex { float x, y, z, rhw; u32 col; } coords[4] = {
		{ x1-dw, y2+dh, 0.f, 1.f, Color },
		{ x2-dw, y2+dh, 0.f, 1.f, Color },
		{ x1-dw, y1+dh, 0.f, 1.f, Color },
		{ x2-dw, y1+dh, 0.f, 1.f, Color },
	};
	dev->SetVertexShader(VertexShaderCache::GetClearVertexShader());
	dev->SetPixelShader(PixelShaderCache::GetClearProgram());
	dev->SetFVF(D3DFVF_XYZW | D3DFVF_DIFFUSE);
	dev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coords, sizeof(CQVertex));
	RestoreShaders();
}

void drawClearQuad(int DestWidth, int DestHeight, u32 Color, float z, IDirect3DPixelShader9 *PShader, IDirect3DVertexShader9 *Vshader)
{
	float dw = 1.f / (float)DestWidth;
	float dh = 1.f / (float)DestHeight;
	struct Q2DVertex { float x,y,z,rhw;u32 color;} coords[4] = {
		{-1.0f-dw,  1.0f+dh, z, 1.0f, Color},
		{ 1.0f-dw,  1.0f+dh, z, 1.0f, Color},
		{ 1.0f-dw, -1.0f+dh, z, 1.0f, Color},
		{-1.0f-dw, -1.0f+dh, z, 1.0f, Color}
	};
	dev->SetVertexShader(Vshader);
	dev->SetPixelShader(PShader);
	dev->SetFVF(D3DFVF_XYZW | D3DFVF_DIFFUSE);
	dev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coords, sizeof(Q2DVertex));
	RestoreShaders();
}


}  // namespace

}  // namespace DX9