Source

gb_emulator / gb_emulator / src / gb_video_d3d11.cpp

Full commit
  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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*  Copyright © 2011 Chris Spencer <spencercw@gmail.com>

    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, either version 3 of the License, or
    (at your option) any later version.

    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 for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <gb_emulator/gb_video_d3d11.h>

#include <stdexcept>
#include <string>

#include <boost/lexical_cast.hpp>
#include <boost/scoped_array.hpp>

#include <Cg/cgD3D11.h>

#include <D3D11.h>

#include <gb_emulator/gb.h>
#include <gb_emulator/gb_input.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

namespace fs = boost::filesystem;
using boost::lexical_cast;
using boost::scoped_array;
using boost::shared_ptr;
using std::logic_error;
using std::runtime_error;
using std::string;

typedef HRESULT (WINAPI *D3D11CreateDevice_t)(IDXGIAdapter *pAdapter, D3D_DRIVER_TYPE DriverType,
	HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL *pFeatureLevels, UINT FeatureLevels,
	UINT SDKVersion, ID3D11Device **ppDevice, D3D_FEATURE_LEVEL *pFeatureLevel,
	ID3D11DeviceContext **ppImmediateContext);
typedef HRESULT (WINAPI *D3D11CreateDeviceAndSwapChain_t)(IDXGIAdapter *pAdapter,
	D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags,
	const D3D_FEATURE_LEVEL *pFeatureLevels, UINT FeatureLevels, UINT SDKVersion,
	const DXGI_SWAP_CHAIN_DESC *pSwapChainDesc, IDXGISwapChain **ppSwapChain,
	ID3D11Device **ppDevice, D3D_FEATURE_LEVEL *pFeatureLevel,
	ID3D11DeviceContext **ppImmediateContext);

struct Vertex
{
	// Position
	FLOAT x;
	FLOAT y;
	FLOAT z;

	// Texture coordinates
	FLOAT u;
	FLOAT v;
};

static const D3D_FEATURE_LEVEL SUPPORTED_FEATURE_LEVELS[] = {
	D3D_FEATURE_LEVEL_11_0,
	D3D_FEATURE_LEVEL_10_1,
	D3D_FEATURE_LEVEL_10_0 };
static GbVideoD3D11 *gD3D11;

GbVideoD3D11::GbVideoD3D11(Gb &gb, GbConfig::Renderer renderer, const fs::path &installDir):
GbVideo(gb),
installDir_(installDir),
gbcBound_(true)
{
	// Load the Direct3D 11 library if available
	d3d11_.reset(LoadLibraryW(L"d3d11.dll"), FreeLibrary);

	// Check the rendering method
	if (renderer != GbConfig::RENDER_DEFAULT && renderer != GbConfig::RENDER_SOFTWARE &&
		renderer != GbConfig::RENDER_HARDWARE)
	{
		throw logic_error("unsupported rendering method");
	}
	renderer_ = renderer == GbConfig::RENDER_DEFAULT ? GbConfig::RENDER_SOFTWARE : renderer;
}

bool GbVideoD3D11::isAvailable()
{
	if (!d3d11_)
	{
		return false;
	}

	// Direct3D 11 is installed, but not necessarily supported by the hardware; try opening the
	// device
	D3D11CreateDevice_t D3D11CreateDevice_ = reinterpret_cast<D3D11CreateDevice_t>(
		GetProcAddress(d3d11_.get(), "D3D11CreateDevice"));
	if (!D3D11CreateDevice_)
	{
		return false;
	}

	ATL::CComPtr<ID3D11Device> device;
	ATL::CComPtr<ID3D11DeviceContext> deviceContext;
	D3D_FEATURE_LEVEL featureLevel;
	HRESULT hr = D3D11CreateDevice_(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
		D3D11_CREATE_DEVICE_SINGLETHREADED, SUPPORTED_FEATURE_LEVELS,
		ARRAY_SIZE(SUPPORTED_FEATURE_LEVELS), D3D11_SDK_VERSION, &device, &featureLevel, &deviceContext);
	return SUCCEEDED(hr);
}

void GbVideoD3D11::initialise()
{
	if (!d3d11_)
	{
		throw runtime_error("Direct3D 11 not available");
	}
	D3D11CreateDeviceAndSwapChain_t D3D11CreateDeviceAndSwapChain_ =
		reinterpret_cast<D3D11CreateDeviceAndSwapChain_t>(GetProcAddress(d3d11_.get(),
		"D3D11CreateDeviceAndSwapChain"));
	if (!D3D11CreateDeviceAndSwapChain_)
	{
		throw runtime_error("Failed to locate D3D11CreateDeviceAndSwapChain in d3d11.dll");
	}

	// Save a reference to this for use in the window procedure
	gD3D11 = this;

	if (renderer_ == GbConfig::RENDER_SOFTWARE)
	{
		unscaledPixelBuffer_.reset(new uint32_t[WIDTH * HEIGHT]);
		scaledPixelBuffer_.reset(new uint32_t[SCALED_WIDTH * SCALED_HEIGHT]);
	}

	// Register the window class
	WNDCLASSEX windowClass;
	memset(&windowClass, 0, sizeof(windowClass));
	windowClass.cbSize = sizeof(windowClass);
	windowClass.style = 0;
	windowClass.lpfnWndProc = &GbVideoD3D11::windowProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = GetModuleHandle(NULL);
	windowClass.hIcon = NULL;
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = NULL;
	windowClass.lpszMenuName = MAKEINTRESOURCEW(101);
	windowClass.lpszClassName = L"WindowClass";
	windowClass.hIconSm = NULL;

	if (!RegisterClassEx(&windowClass))
	{
		throw runtime_error("failed to register window class: " +
			lexical_cast<string>(GetLastError()));
	}

	RECT windowRect = { 0, 0, SCALED_WIDTH, SCALED_HEIGHT };
	if (!AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE))
	{
		throw runtime_error("failed to set the main window size: " +
			lexical_cast<string>(GetLastError()));
	}

	// Create the main window
	window_ = CreateWindowEx(NULL, L"WindowClass", L"Game Boy Emulator",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
		windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, NULL, NULL,
		windowClass.hInstance, NULL);
	if (!window_)
	{
		throw runtime_error("failed to create the main window: " +
			lexical_cast<string>(GetLastError()));
	}

	// Initialise Direct3D
	// Create the device and swap chain
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	memset(&swapChainDesc, 0, sizeof(swapChainDesc));
	swapChainDesc.BufferDesc.Width = SCALED_WIDTH;
	swapChainDesc.BufferDesc.Height = SCALED_HEIGHT;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.BufferCount = 2;
	swapChainDesc.OutputWindow = window_;
	swapChainDesc.Windowed = TRUE;
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	swapChainDesc.Flags = 0;

	D3D_FEATURE_LEVEL featureLevel;
	HRESULT hr = D3D11CreateDeviceAndSwapChain_(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
		D3D11_CREATE_DEVICE_SINGLETHREADED, SUPPORTED_FEATURE_LEVELS,
		ARRAY_SIZE(SUPPORTED_FEATURE_LEVELS), D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
		&device_, &featureLevel, &deviceContext_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to initialise Direct3D: " + lexical_cast<string>(hr));
	}

	// Set the render target
	createRenderTarget();

	// Set the viewport
	D3D11_VIEWPORT viewport;
	memset(&viewport, 0, sizeof(viewport));
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = SCALED_WIDTH;
	viewport.Height = SCALED_HEIGHT;
	viewport.MinDepth = 0;
	viewport.MaxDepth = 0;

	deviceContext_->RSSetViewports(1, &viewport);

	// Perform remaining initialisation
	initPipeline();
	initGraphics();
}

GbVideoD3D11::~GbVideoD3D11()
{
}

LRESULT CALLBACK GbVideoD3D11::windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_SIZE:
		if (gD3D11->deviceContext_ && gD3D11->swapChain_)
		{
			// Resize the back buffer. To do this we need to delete the render target, resize the
			// buffer then re-create the render target
			gD3D11->deviceContext_->OMSetRenderTargets(0, NULL, NULL);
			gD3D11->renderTarget_ = NULL;

			uint32_t size = static_cast<uint32_t>(lParam);
			unsigned width = size & 0xffff;
			unsigned height = size >> 16;
			gD3D11->swapChain_->ResizeBuffers(1, width, height, DXGI_FORMAT_B8G8R8A8_UNORM, 0);

			gD3D11->createRenderTarget();

			// Adjust the viewport
			D3D11_VIEWPORT viewport;
			memset(&viewport, 0, sizeof(viewport));
			viewport.TopLeftX =
				static_cast<float>(static_cast<int>(width  / 2 - SCALED_WIDTH  / 2));
			viewport.TopLeftY =
				static_cast<float>(static_cast<int>(height / 2 - SCALED_HEIGHT / 2));
			viewport.Width = SCALED_WIDTH;
			viewport.Height = SCALED_HEIGHT;
			viewport.MinDepth = 0;
			viewport.MaxDepth = 0;

			gD3D11->deviceContext_->RSSetViewports(1, &viewport);
		}
		break;

	case WM_COMMAND:
		switch (wParam & 0xffff)
		{
		case 57601:  // Open
			gD3D11->gb_.input_->openRomDialog();
			break;

		case 57603:  // Save
			gD3D11->gb_.save();
			break;

		case 40004:  // Load
			gD3D11->gb_.load();
			break;

		case 40001:  // Exit
			exit(0);
			break;
		}
		break;

	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}

	return 0;
}

void GbVideoD3D11::createRenderTarget()
{
	CComPtr<ID3D11Texture2D> backBuffer;
	HRESULT hr = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D),
		reinterpret_cast<void **>(&backBuffer));
	if (FAILED(hr))
	{
		throw runtime_error("failed to get video buffer: " + lexical_cast<string>(hr));
	}

	D3D11_TEXTURE2D_DESC desc;
	backBuffer->GetDesc(&desc);

	hr = device_->CreateRenderTargetView(backBuffer, NULL, &renderTarget_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to create video render-target view: " +
			lexical_cast<string>(hr));
	}
	deviceContext_->OMSetRenderTargets(1, &renderTarget_.p, NULL);
}

void GbVideoD3D11::initPipeline()
{
	// Initialise Cg
	cgContext_.reset(cgCreateContext(), cgDestroyContext);
	if (!cgContext_)
	{
		throw runtime_error(string("failed to create Cg context: ") + cgGetLastErrorString(NULL));
	}

	// Compile the shaders
	fs::path vertexShaderPath = installDir_ / L"shaders/vertex_shader.cg";
	fs::path pixelShaderPath  = installDir_ / L"shaders/pixel_shader.cg";
	vertexShaderPath.make_preferred();
	pixelShaderPath.make_preferred();
	
	string vertexShaderPathStr = vertexShaderPath.string();
	string pixelShaderPathStr  = pixelShaderPath.string();

	vertexShader_.reset(cgCreateProgramFromFile(cgContext_.get(), CG_SOURCE,
		vertexShaderPathStr.c_str(), CG_PROFILE_VS_4_0, NULL, NULL), cgDestroyProgram);
	if (!vertexShader_)
	{
		throw runtime_error(string("failed to compile vertex shader: ") +
			cgGetLastErrorString(NULL));
	}

	// Load the appropriate pixel shaders depending on the rendering mode
	if (renderer_ == GbConfig::RENDER_HARDWARE)
	{
		dmgPixelShader_.reset(cgCreateProgramFromFile(cgContext_.get(), CG_SOURCE,
			pixelShaderPathStr.c_str(), CG_PROFILE_PS_4_0, "dmgDecode", NULL), cgDestroyProgram);
		if (!dmgPixelShader_)
 		{
			throw runtime_error(string("failed to compile DMG pixel shader: ") +
				cgGetLastErrorString(NULL));
		}

		gbcPixelShader_.reset(cgCreateProgramFromFile(cgContext_.get(), CG_SOURCE,
			pixelShaderPathStr.c_str(), CG_PROFILE_PS_4_0, "gbcDecode", NULL), cgDestroyProgram);
		if (!gbcPixelShader_)
		{
			throw runtime_error(string("failed to compile GBC pixel shader: ") +
 				cgGetLastErrorString(NULL));
 		}
	}
	else
	{
		basicPixelShader_.reset(cgCreateProgramFromFile(cgContext_.get(), CG_SOURCE,
			pixelShaderPathStr.c_str(), CG_PROFILE_PS_4_0, "passThrough", NULL), cgDestroyProgram);
		if (!basicPixelShader_)
		{
			throw runtime_error(string("failed to compile pixel shader: ") +
				cgGetLastErrorString(NULL));
		}
	}

	// Load the shaders into Direct3D
	HRESULT hr = cgD3D11SetDevice(cgContext_.get(), device_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to set Direct3D device within Cg: " + lexical_cast<string>(hr));
	}

	hr = cgD3D11LoadProgram(vertexShader_.get(), 0);
	if (FAILED(hr))
	{
		throw runtime_error("failed to load vertex shader: " + lexical_cast<string>(hr));
	}

	if (renderer_ == GbConfig::RENDER_HARDWARE)
	{
		hr = cgD3D11LoadProgram(dmgPixelShader_.get(), 0);
 		if (FAILED(hr))
 		{
			throw runtime_error("failed to load DMG pixel shader: " + lexical_cast<string>(hr));
 		}
 
		hr = cgD3D11LoadProgram(gbcPixelShader_.get(), 0);
		if (FAILED(hr))
		{
			throw runtime_error("failed to load GBC pixel shader: " + lexical_cast<string>(hr));
		}
	}
	else
	{
		hr = cgD3D11LoadProgram(basicPixelShader_.get(), 0);
		if (FAILED(hr))
		{
			throw runtime_error("failed to load pixel shader: " + lexical_cast<string>(hr));
		}
	}

	// Get the vertex shader blob which is required for the input layout descriptor
	ID3D10Blob *vertexShaderBlob = cgD3D11GetCompiledProgram(vertexShader_.get());
	if (!vertexShaderBlob)
	{
		throw runtime_error("failed to retrieve compiled vertex shader: " +
			lexical_cast<string>(cgD3D11GetLastError()));
	}

	// Create the input layout descriptor
	D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] = {
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT,
		  D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,    0, D3D11_APPEND_ALIGNED_ELEMENT,
		  D3D11_INPUT_PER_VERTEX_DATA, 0 } };

	hr = device_->CreateInputLayout(inputLayoutDesc, 2, vertexShaderBlob->GetBufferPointer(),
		vertexShaderBlob->GetBufferSize(), &layout_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to create input layout: " + lexical_cast<string>(hr));
	}

	deviceContext_->IASetInputLayout(layout_);

	// Create the texture sampler
	D3D11_SAMPLER_DESC samplerDesc;
	memset(&samplerDesc, 0, sizeof(samplerDesc));
	samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
	samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
	samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
	samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
	samplerDesc.MipLODBias = 0.0f;
	samplerDesc.MaxAnisotropy = 1;
	samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
	samplerDesc.BorderColor[0] = 0.0f;
	samplerDesc.BorderColor[1] = 0.0f;
	samplerDesc.BorderColor[2] = 0.0f;
	samplerDesc.BorderColor[3] = 0.0f;
	samplerDesc.MinLOD = 0.0f;
	samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

	hr = device_->CreateSamplerState(&samplerDesc, &sampler_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to create sampler state: " + lexical_cast<string>(hr));
	}
}

void GbVideoD3D11::initGraphics()
{
	// The triangle list we want to draw
	static const Vertex vertices1024[] = {
		{  1.0f, -1.0f, 0.0f, SCALED_WIDTH / 1024.0f, SCALED_HEIGHT / 1024.0f },    // Bottom right
		{ -1.0f, -1.0f, 0.0f, 0.0f,                   SCALED_HEIGHT / 1024.0f },    // Bottom left
		{ -1.0f,  1.0f, 0.0f, 0.0f,                   0.0f },                       // Top left
		{  1.0f,  1.0f, 0.0f, SCALED_WIDTH / 1024.0f, 0.0f },                       // Top right
		{  1.0f, -1.0f, 0.0f, SCALED_WIDTH / 1024.0f, SCALED_HEIGHT / 1024.0f } };  // Bottom right

	static const Vertex vertices256[] = {
		{  1.0f, -1.0f, 0.0f, WIDTH / 256.0f, HEIGHT / 256.0f },    // Bottom right
		{ -1.0f, -1.0f, 0.0f, 0.0f,           HEIGHT / 256.0f },    // Bottom left
		{ -1.0f,  1.0f, 0.0f, 0.0f,           0.0f },               // Top left
		{  1.0f,  1.0f, 0.0f, WIDTH / 256.0f, 0.0f },               // Top right
		{  1.0f, -1.0f, 0.0f, WIDTH / 256.0f, HEIGHT / 256.0f } };  // Bottom right

	const Vertex *vertices = renderer_ == GbConfig::RENDER_HARDWARE ? vertices256 : vertices1024;

	// Create the vertex buffer
	D3D11_BUFFER_DESC vertexBufferDesc;
	memset(&vertexBufferDesc, 0, sizeof(vertexBufferDesc));
	vertexBufferDesc.ByteWidth = sizeof(vertices256);
	vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	vertexBufferDesc.MiscFlags = 0;
	vertexBufferDesc.StructureByteStride = 0;

	HRESULT hr = device_->CreateBuffer(&vertexBufferDesc, NULL, &vertexBuffer_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to create vertex buffer: " + lexical_cast<string>(hr));
	}

	// Fill the vertex buffer
	D3D11_MAPPED_SUBRESOURCE resource;
	hr = deviceContext_->Map(vertexBuffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
	if (FAILED(hr))
	{
		throw runtime_error("failed to map vertex buffer: " + lexical_cast<string>(hr));
	}

	memcpy(resource.pData, vertices, sizeof(vertices256));
	deviceContext_->Unmap(vertexBuffer_, 0);

	// Create the texture to render the game on to
	D3D11_TEXTURE2D_DESC textureDescriptor;
	memset(&textureDescriptor, 0, sizeof(textureDescriptor));
	textureDescriptor.Width = textureDescriptor.Height =
		renderer_ == GbConfig::RENDER_HARDWARE ? 256 : 1024;
	textureDescriptor.MipLevels = 1;
	textureDescriptor.ArraySize = 1;
	textureDescriptor.Format = renderer_ == GbConfig::RENDER_HARDWARE ?
		DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_B8G8R8A8_UNORM;
	textureDescriptor.SampleDesc.Count = 1;
	textureDescriptor.SampleDesc.Quality = 0;
	textureDescriptor.Usage = D3D11_USAGE_DYNAMIC;
	textureDescriptor.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	textureDescriptor.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	textureDescriptor.MiscFlags = 0;
	
	hr = device_->CreateTexture2D(&textureDescriptor, NULL, &texture_);
	if (FAILED(hr))
	{
		throw runtime_error("failed to create texture: " + lexical_cast<string>(hr));
	}

	// Bind the texture and sampler to the shader
	if (renderer_ == GbConfig::RENDER_HARDWARE)
	{
		shared_ptr<_CGparameter> dmgSamplerParam(cgGetNamedParameter(dmgPixelShader_.get(), "sampler"),
 			cgDestroyParameter);
		if (!dmgSamplerParam)
 		{
			throw runtime_error(string("failed to get DMG pixel shader sampler parameter: ") +
 				cgGetLastErrorString(NULL));
 		}
 
		shared_ptr<_CGparameter> gbcSamplerParam(cgGetNamedParameter(gbcPixelShader_.get(), "sampler"),
			cgDestroyParameter);
		if (!dmgSamplerParam)
		{
			throw runtime_error(string("failed to get GBC pixel shader sampler parameter: ") +
				cgGetLastErrorString(NULL));
		}

		cgD3D11SetTextureSamplerStateParameter(dmgSamplerParam.get(), texture_, sampler_);
		cgD3D11SetTextureSamplerStateParameter(gbcSamplerParam.get(), texture_, sampler_);
	}
	else
	{
		shared_ptr<_CGparameter> samplerParam(cgGetNamedParameter(basicPixelShader_.get(),
			"sampler"), cgDestroyParameter);
		if (!samplerParam)
		{
			throw runtime_error(string("failed to get pixel shader sampler parameter: ") +
				cgGetLastErrorString(NULL));
		}

		cgD3D11SetTextureSamplerStateParameter(samplerParam.get(), texture_, sampler_);
	}

	// Bind the shaders
	hr = cgD3D11BindProgram(vertexShader_.get());
	if (FAILED(hr))
	{
		throw runtime_error("failed to bind vertex shader: " + lexical_cast<string>(hr));
	}

	hr = cgD3D11BindProgram(renderer_ == GbConfig::RENDER_HARDWARE ?
		gbcPixelShader_.get() : basicPixelShader_.get());
	if (FAILED(hr))
	{
		throw runtime_error("failed to bind pixel shader: " + lexical_cast<string>(hr));
	}
}

void GbVideoD3D11::draw(uint16_t *pixelBuffer, bool gbc)
{
	if (renderer_ == GbConfig::RENDER_HARDWARE)
	{
		// Switch pixel shaders if necessary. A DMG -> GBC switch can't happen so don't bother
		// checking for it
		if (!gbc && gbcBound_)
		{
			HRESULT hr = cgD3D11BindProgram(dmgPixelShader_.get());
			if (FAILED(hr))
			{
				throw runtime_error("failed to bind DMG pixel shader: " + lexical_cast<string>(hr));
			}
		}
	}
	else
	{
		// Perform post-processing in software
		postProcess(pixelBuffer, gbc, unscaledPixelBuffer_.get(), scaledPixelBuffer_.get());
	}

	// Fill the texture to display
	D3D11_MAPPED_SUBRESOURCE resource;
	HRESULT hr = deviceContext_->Map(texture_, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
	if (FAILED(hr))
	{
		throw runtime_error("Failed to map texture: " + lexical_cast<string>(hr));
	}

	// Copy the data into the texture. We have to do it line-by-line because the width of the pixel
	// buffer does not match that of the texture.
	if (renderer_ == GbConfig::RENDER_HARDWARE)
	{
		uint16_t *buf = reinterpret_cast<uint16_t *>(resource.pData);
		for (unsigned y = 0; y != HEIGHT; ++y)
		{
			memcpy(&buf[y * 256], &pixelBuffer[y * WIDTH], WIDTH * sizeof(uint16_t));
		}
	}
	else
	{
		uint32_t *buf = reinterpret_cast<uint32_t *>(resource.pData);
		for (unsigned y = 0; y != SCALED_HEIGHT; ++y)
		{
			memcpy(&buf[y * 1024], &scaledPixelBuffer_[y * SCALED_WIDTH],
				SCALED_WIDTH * sizeof(uint32_t));
		}
	}
	deviceContext_->Unmap(texture_, 0);

	// Select the vertex buffer
	static const UINT stride = sizeof(Vertex);
	static const UINT offset = 0;
	deviceContext_->IASetVertexBuffers(0, 1, &vertexBuffer_.p, &stride, &offset);

	// Draw the vertices to the back buffer
	deviceContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	deviceContext_->Draw(5, 0);

	// Switch the buffers. Only vsync if we only have one frame to render; this stops missed vsyncs
	// from screwing up the emulator timing.
	UINT vsync = InterlockedCompareExchange(&gb_.frames_, 1, 1) == 1;
	hr = swapChain_->Present(vsync, 0);
	if (FAILED(hr))
	{
		throw runtime_error("Failed to swap display buffers: " + lexical_cast<string>(hr));
	}
}