blob: 4b8cb5a6198c1ba6435a3764adb40483d27899fd [file] [log] [blame]
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
8
9#include "common/debug.h"
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000014#include "libGLESv2/Program.h"
15#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000016#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000017#include "libGLESv2/renderer/Renderer11.h"
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +000018#include "libGLESv2/renderer/RenderTarget11.h"
daniel@transgaming.com65e65372012-11-28 19:33:50 +000019#include "libGLESv2/renderer/renderer11_utils.h"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000020#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000021#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000022#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000023#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000024#include "libGLESv2/renderer/IndexBuffer11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000025#include "libGLESv2/renderer/VertexDataManager.h"
26#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000027#include "libGLESv2/renderer/TextureStorage11.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000028
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000029#include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h"
30#include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h"
31#include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h"
32#include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h"
33#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h"
34
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000035#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
36#include "libGLESv2/renderer/shaders/compiled/clear11ps.h"
37
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000038#include <sstream>
39
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000040namespace rx
41{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000042static const DXGI_FORMAT RenderTargetFormats[] =
43 {
44 DXGI_FORMAT_R8G8B8A8_UNORM
45 };
46
47static const DXGI_FORMAT DepthStencilFormats[] =
48 {
49 DXGI_FORMAT_D24_UNORM_S8_UINT
50 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000051
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000052enum
53{
54 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
55};
56
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000057Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
58{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000059 mVertexDataManager = NULL;
60 mIndexDataManager = NULL;
61
daniel@transgaming.comc5114302012-12-20 21:11:36 +000062 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000063 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000064
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000065 mCopyResourcesInitialized = false;
66 mCopyVB = NULL;
67 mCopySampler = NULL;
68 mCopyIL = NULL;
69 mCopyVS = NULL;
70 mCopyRGBAPS = NULL;
71 mCopyRGBPS = NULL;
72 mCopyLumPS = NULL;
73 mCopyLumAlphaPS = NULL;
74
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000075 mClearResourcesInitialized = false;
76 mClearVB = NULL;
77 mClearIL = NULL;
78 mClearVS = NULL;
79 mClearPS = NULL;
80 mClearScissorRS = NULL;
81 mClearNoScissorRS = NULL;
82
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000083 mD3d11Module = NULL;
84 mDxgiModule = NULL;
85
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000086 mDeviceLost = false;
87
daniel@transgaming.com25072f62012-11-28 19:31:32 +000088 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000089 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000090 mDxgiAdapter = NULL;
91 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000092
93 mDriverConstantBufferVS = NULL;
94 mDriverConstantBufferPS = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000095}
96
97Renderer11::~Renderer11()
98{
99 releaseDeviceResources();
100
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000101 if (mDxgiFactory)
102 {
103 mDxgiFactory->Release();
104 mDxgiFactory = NULL;
105 }
106
107 if (mDxgiAdapter)
108 {
109 mDxgiAdapter->Release();
110 mDxgiAdapter = NULL;
111 }
112
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000113 if (mDeviceContext)
114 {
daniel@transgaming.comd5df4e82013-01-11 04:11:21 +0000115 mDeviceContext->ClearState();
116 mDeviceContext->Flush();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000117 mDeviceContext->Release();
118 mDeviceContext = NULL;
119 }
120
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000121 if (mDevice)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000122 {
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000123 mDevice->Release();
124 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000125 }
126
127 if (mD3d11Module)
128 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000129 FreeLibrary(mD3d11Module);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000130 mD3d11Module = NULL;
131 }
132
133 if (mDxgiModule)
134 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000135 FreeLibrary(mDxgiModule);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000136 mDxgiModule = NULL;
137 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000138}
139
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000140Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
141{
142 ASSERT(dynamic_cast<rx::Renderer11*>(renderer) != NULL);
143 return static_cast<rx::Renderer11*>(renderer);
144}
145
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000146EGLint Renderer11::initialize()
147{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000148 if (!initializeCompiler())
149 {
150 return EGL_NOT_INITIALIZED;
151 }
152
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000153 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
154 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000155
156 if (mD3d11Module == NULL || mDxgiModule == NULL)
157 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000158 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000159 return EGL_NOT_INITIALIZED;
160 }
161
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000162 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000163
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000164 if (D3D11CreateDevice == NULL)
165 {
166 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
167 return EGL_NOT_INITIALIZED;
168 }
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000169
170 D3D_FEATURE_LEVEL featureLevel[] =
171 {
172 D3D_FEATURE_LEVEL_11_0,
173 D3D_FEATURE_LEVEL_10_1,
174 D3D_FEATURE_LEVEL_10_0,
175 };
176
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000177 HRESULT result = D3D11CreateDevice(NULL,
178 D3D_DRIVER_TYPE_HARDWARE,
179 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000180 #if defined(_DEBUG)
181 D3D11_CREATE_DEVICE_DEBUG,
182 #else
183 0,
184 #endif
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000185 featureLevel,
186 sizeof(featureLevel)/sizeof(featureLevel[0]),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000187 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000188 &mDevice,
189 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000190 &mDeviceContext);
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000191
192 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000193 {
194 ERR("Could not create D3D11 device - aborting!\n");
195 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
196 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000197
198 IDXGIDevice *dxgiDevice = NULL;
199 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
200
201 if (FAILED(result))
202 {
203 ERR("Could not query DXGI device - aborting!\n");
204 return EGL_NOT_INITIALIZED;
205 }
206
207 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
208
209 if (FAILED(result))
210 {
211 ERR("Could not retrieve DXGI adapter - aborting!\n");
212 return EGL_NOT_INITIALIZED;
213 }
214
215 dxgiDevice->Release();
216
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000217 mDxgiAdapter->GetDesc(&mAdapterDescription);
218 memset(mDescription, 0, sizeof(mDescription));
219 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
220
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000221 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
222
223 if (!mDxgiFactory || FAILED(result))
224 {
225 ERR("Could not create DXGI factory - aborting!\n");
226 return EGL_NOT_INITIALIZED;
227 }
228
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000229 initializeDevice();
230
231 return EGL_SUCCESS;
232}
233
234// do any one-time device initialization
235// NOTE: this is also needed after a device lost/reset
236// to reset the scene status and ensure the default states are reset.
237void Renderer11::initializeDevice()
238{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000239 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000240 mInputLayoutCache.initialize(mDevice, mDeviceContext);
241
242 ASSERT(!mVertexDataManager && !mIndexDataManager);
243 mVertexDataManager = new VertexDataManager(this);
244 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000245
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000246 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000247}
248
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000249int Renderer11::generateConfigs(ConfigDesc **configDescList)
250{
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000251 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
252 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000253 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
254 int numConfigs = 0;
255
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000256 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000257 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000258 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000259 {
260 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
261
262 UINT formatSupport = 0;
263 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
264
265 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
266 {
267 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
268
269 UINT formatSupport = 0;
270 HRESULT result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
271
272 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL))
273 {
274 ConfigDesc newConfig;
275 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
276 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
277 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
278 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
279
280 (*configDescList)[numConfigs++] = newConfig;
281 }
282 }
283 }
284 }
285
286 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000287}
288
289void Renderer11::deleteConfigs(ConfigDesc *configDescList)
290{
291 delete [] (configDescList);
292}
293
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000294void Renderer11::sync(bool block)
295{
296 // TODO
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000297 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000298}
299
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000300SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
301{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000302 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000303}
304
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000305void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
306{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000307 if (type == gl::SAMPLER_PIXEL)
308 {
309 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
310 {
311 ERR("Pixel shader sampler index %i is not valid.", index);
312 return;
313 }
314
315 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
316 {
317 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
318
319 if (!dxSamplerState)
320 {
321 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
322 "sampler state for pixel shaders at slot %i.", index);
323 }
324
325 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
326
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000327 mCurPixelSamplerStates[index] = samplerState;
328 }
329
330 mForceSetPixelSamplerStates[index] = false;
331 }
332 else if (type == gl::SAMPLER_VERTEX)
333 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000334 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000335 {
336 ERR("Vertex shader sampler index %i is not valid.", index);
337 return;
338 }
339
340 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
341 {
342 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
343
344 if (!dxSamplerState)
345 {
346 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
347 "sampler state for vertex shaders at slot %i.", index);
348 }
349
350 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
351
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000352 mCurVertexSamplerStates[index] = samplerState;
353 }
354
355 mForceSetVertexSamplerStates[index] = false;
356 }
357 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000358}
359
360void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
361{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000362 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000363 unsigned int serial = 0;
364 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000365
366 if (texture)
367 {
368 TextureStorageInterface *texStorage = texture->getNativeTexture();
369 if (texStorage)
370 {
371 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
372 textureSRV = storage11->getSRV();
373 }
374
375 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
376 // missing the shader resource view
377 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000378
379 serial = texture->getTextureSerial();
380 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000381 }
382
383 if (type == gl::SAMPLER_PIXEL)
384 {
385 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
386 {
387 ERR("Pixel shader sampler index %i is not valid.", index);
388 return;
389 }
390
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000391 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
392 {
393 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
394 }
395
396 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000397 }
398 else if (type == gl::SAMPLER_VERTEX)
399 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000400 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000401 {
402 ERR("Vertex shader sampler index %i is not valid.", index);
403 return;
404 }
405
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000406 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
407 {
408 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
409 }
410
411 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000412 }
413 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000414}
415
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000416void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000417{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000418 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000419 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000420 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
421 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000422 if (!dxRasterState)
423 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000424 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000425 "rasterizer state.");
426 }
427
428 mDeviceContext->RSSetState(dxRasterState);
429
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000430 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000431 }
432
433 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000434}
435
436void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
437 unsigned int sampleMask)
438{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000439 if (mForceSetBlendState ||
440 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
441 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
442 sampleMask != mCurSampleMask)
443 {
444 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
445 if (!dxBlendState)
446 {
447 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
448 "blend state.");
449 }
450
451 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
452 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
453
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000454 mCurBlendState = blendState;
455 mCurBlendColor = blendColor;
456 mCurSampleMask = sampleMask;
457 }
458
459 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000460}
461
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000462void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000463 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000464{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000465 if (mForceSetDepthStencilState ||
466 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
467 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
468 {
469 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
470 stencilRef != stencilBackRef ||
471 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
472 {
473 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
474 "invalid under WebGL.");
475 return error(GL_INVALID_OPERATION);
476 }
477
478 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
479 if (!dxDepthStencilState)
480 {
481 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
482 "setting the default depth stencil state.");
483 }
484
485 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
486
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000487 mCurDepthStencilState = depthStencilState;
488 mCurStencilRef = stencilRef;
489 mCurStencilBackRef = stencilBackRef;
490 }
491
492 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000493}
494
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000495void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000496{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000497 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
498 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000499 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000500 if (enabled)
501 {
502 D3D11_RECT rect;
503 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
504 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
505 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
506 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000507
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000508 mDeviceContext->RSSetScissorRects(1, &rect);
509 }
510
511 if (enabled != mScissorEnabled)
512 {
513 mForceSetRasterState = true;
514 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000515
516 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000517 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000518 }
519
520 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000521}
522
daniel@transgaming.com12985182012-12-20 20:56:31 +0000523bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000524 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000525{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000526 gl::Rectangle actualViewport = viewport;
527 float actualZNear = gl::clamp01(zNear);
528 float actualZFar = gl::clamp01(zFar);
529 if (ignoreViewport)
530 {
531 actualViewport.x = 0;
532 actualViewport.y = 0;
533 actualViewport.width = mRenderTargetDesc.width;
534 actualViewport.height = mRenderTargetDesc.height;
535 actualZNear = 0.0f;
536 actualZFar = 1.0f;
537 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000538
539 D3D11_VIEWPORT dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000540 dxViewport.TopLeftX = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
541 dxViewport.TopLeftY = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
542 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.TopLeftX));
543 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.TopLeftY));
544 dxViewport.MinDepth = actualZNear;
545 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000546
547 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
548 {
549 return false; // Nothing to render
550 }
551
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000552 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
553 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000554
daniel@transgaming.com53670042012-11-28 20:55:51 +0000555 if (viewportChanged)
556 {
557 mDeviceContext->RSSetViewports(1, &dxViewport);
558
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000559 mCurViewport = actualViewport;
560 mCurNear = actualZNear;
561 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000562 }
563
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000564 if (currentProgram && viewportChanged)
daniel@transgaming.com53670042012-11-28 20:55:51 +0000565 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000566 mVertexConstants.halfPixelSize[0] = 0.0f;
567 mVertexConstants.halfPixelSize[1] = 0.0f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000568
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000569 mPixelConstants.coord[0] = actualViewport.width * 0.5f;
570 mPixelConstants.coord[1] = actualViewport.height * 0.5f;
571 mPixelConstants.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
572 mPixelConstants.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000573
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000574 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
575 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
576 mPixelConstants.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000577
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000578 mVertexConstants.depthRange[0] = actualZNear;
579 mVertexConstants.depthRange[1] = actualZFar;
580 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000581
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000582 mPixelConstants.depthRange[0] = actualZNear;
583 mPixelConstants.depthRange[1] = actualZFar;
584 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000585 }
586
587 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000588 return true;
589}
590
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000591bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
592{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000593 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000594
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000595 switch (mode)
596 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000597 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
598 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000599 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000600 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
601 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
602 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000603 // emulate fans via rewriting index buffer
604 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000605 default:
606 return error(GL_INVALID_ENUM, false);
607 }
608
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000609 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000610
611 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000612}
613
614bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000615{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000616 // Get the color render buffer and serial
617 gl::Renderbuffer *renderbufferObject = NULL;
618 unsigned int renderTargetSerial = 0;
619 if (framebuffer->getColorbufferType() != GL_NONE)
620 {
621 renderbufferObject = framebuffer->getColorbuffer();
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000622
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000623 if (!renderbufferObject)
624 {
625 ERR("render target pointer unexpectedly null.");
daniel@transgaming.come9c71b42012-11-28 21:02:23 +0000626 return false;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000627 }
628
629 renderTargetSerial = renderbufferObject->getSerial();
630 }
631
632 // Get the depth stencil render buffer and serials
633 gl::Renderbuffer *depthStencil = NULL;
634 unsigned int depthbufferSerial = 0;
635 unsigned int stencilbufferSerial = 0;
636 if (framebuffer->getDepthbufferType() != GL_NONE)
637 {
638 depthStencil = framebuffer->getDepthbuffer();
639 if (!depthStencil)
640 {
641 ERR("Depth stencil pointer unexpectedly null.");
642 return false;
643 }
644
645 depthbufferSerial = depthStencil->getSerial();
646 }
647 else if (framebuffer->getStencilbufferType() != GL_NONE)
648 {
649 depthStencil = framebuffer->getStencilbuffer();
650 if (!depthStencil)
651 {
652 ERR("Depth stencil pointer unexpectedly null.");
653 return false;
654 }
655
656 stencilbufferSerial = depthStencil->getSerial();
657 }
658
659 // Extract the render target dimensions and view
660 unsigned int renderTargetWidth = 0;
661 unsigned int renderTargetHeight = 0;
662 GLenum renderTargetFormat = 0;
663 ID3D11RenderTargetView* framebufferRTV = NULL;
664 if (renderbufferObject)
665 {
666 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
667 if (!renderTarget)
668 {
669 ERR("render target pointer unexpectedly null.");
670 return false;
671 }
672
673 framebufferRTV = renderTarget->getRenderTargetView();
674 if (!framebufferRTV)
675 {
676 ERR("render target view pointer unexpectedly null.");
677 return false;
678 }
679
680 renderTargetWidth = renderbufferObject->getWidth();
681 renderTargetHeight = renderbufferObject->getHeight();
682 renderTargetFormat = renderbufferObject->getActualFormat();
683 }
684
685 // Extract the depth stencil sizes and view
686 unsigned int depthSize = 0;
687 unsigned int stencilSize = 0;
688 ID3D11DepthStencilView* framebufferDSV = NULL;
689 if (depthStencil)
690 {
691 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
692 if (!depthStencilRenderTarget)
693 {
694 ERR("render target pointer unexpectedly null.");
695 if (framebufferRTV)
696 {
697 framebufferRTV->Release();
698 }
699 return false;
700 }
701
702 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
703 if (!framebufferDSV)
704 {
705 ERR("depth stencil view pointer unexpectedly null.");
706 if (framebufferRTV)
707 {
708 framebufferRTV->Release();
709 }
710 return false;
711 }
712
713 // If there is no render buffer, the width, height and format values come from
714 // the depth stencil
715 if (!renderbufferObject)
716 {
717 renderTargetWidth = depthStencil->getWidth();
718 renderTargetHeight = depthStencil->getHeight();
719 renderTargetFormat = depthStencil->getActualFormat();
720 }
721
722 depthSize = depthStencil->getDepthSize();
723 stencilSize = depthStencil->getStencilSize();
724 }
725
726 // Apply the render target and depth stencil
727 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
728 renderTargetSerial != mAppliedRenderTargetSerial ||
729 depthbufferSerial != mAppliedDepthbufferSerial ||
730 stencilbufferSerial != mAppliedStencilbufferSerial)
731 {
732 mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV);
733
734 mRenderTargetDesc.width = renderTargetWidth;
735 mRenderTargetDesc.height = renderTargetHeight;
736 mRenderTargetDesc.format = renderTargetFormat;
737 mForceSetViewport = true; // TODO: It may not be required to clamp the viewport in D3D11
738 mForceSetScissor = true; // TODO: It may not be required to clamp the scissor in D3D11
739
740 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
741 {
742 mCurDepthSize = depthSize;
743 mForceSetRasterState = true;
744 }
745
746 mCurStencilSize = stencilSize;
747
748 mAppliedRenderTargetSerial = renderTargetSerial;
749 mAppliedDepthbufferSerial = depthbufferSerial;
750 mAppliedStencilbufferSerial = stencilbufferSerial;
751 mRenderTargetDescInitialized = true;
752 mDepthStencilInitialized = true;
753 }
754
755 if (framebufferRTV)
756 {
757 framebufferRTV->Release();
758 }
759 if (framebufferDSV)
760 {
761 framebufferDSV->Release();
762 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000763
764 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000765}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000766
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000767GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000768{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000769 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
770 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
771 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000772 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000773 return err;
774 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000775
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000776 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000777}
778
daniel@transgaming.com31240482012-11-28 21:06:41 +0000779GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000780{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000781 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000782
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000783 if (err == GL_NO_ERROR)
784 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000785 if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000786 {
787 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
788
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000789 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000790 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000791 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000792 }
793 }
794
795 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000796}
797
798void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
799{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000800 if (mode == GL_LINE_LOOP)
801 {
802 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
803 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000804 else if (mode == GL_TRIANGLE_FAN)
805 {
806 drawTriangleFan(count, GL_NONE, NULL, 0, NULL);
807 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000808 else if (instances > 0)
809 {
810 // TODO
811 UNIMPLEMENTED();
812 }
813 else
814 {
815 mDeviceContext->Draw(count, 0);
816 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000817}
818
daniel@transgaming.com31240482012-11-28 21:06:41 +0000819void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000820{
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000821 if (mode == GL_LINE_LOOP)
822 {
823 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
824 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000825 else if (mode == GL_TRIANGLE_FAN)
826 {
827 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
828 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000829 else
830 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +0000831 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000832 }
833}
834
835void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
836{
837 // Get the raw indices for an indexed draw
838 if (type != GL_NONE && elementArrayBuffer)
839 {
840 gl::Buffer *indexBuffer = elementArrayBuffer;
841 intptr_t offset = reinterpret_cast<intptr_t>(indices);
842 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
843 }
844
845 if (!mLineLoopIB)
846 {
847 mLineLoopIB = new StreamingIndexBufferInterface(this);
848 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
849 {
850 delete mLineLoopIB;
851 mLineLoopIB = NULL;
852
853 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
854 return error(GL_OUT_OF_MEMORY);
855 }
856 }
857
858 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
859 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
860 {
861 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
862 return error(GL_OUT_OF_MEMORY);
863 }
864
865 void* mappedMemory = NULL;
866 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
867 if (offset == -1 || mappedMemory == NULL)
868 {
869 ERR("Could not map index buffer for GL_LINE_LOOP.");
870 return error(GL_OUT_OF_MEMORY);
871 }
872
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000873 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000874 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000875
876 switch (type)
877 {
878 case GL_NONE: // Non-indexed draw
879 for (int i = 0; i < count; i++)
880 {
881 data[i] = i;
882 }
883 data[count] = 0;
884 break;
885 case GL_UNSIGNED_BYTE:
886 for (int i = 0; i < count; i++)
887 {
888 data[i] = static_cast<const GLubyte*>(indices)[i];
889 }
890 data[count] = static_cast<const GLubyte*>(indices)[0];
891 break;
892 case GL_UNSIGNED_SHORT:
893 for (int i = 0; i < count; i++)
894 {
895 data[i] = static_cast<const GLushort*>(indices)[i];
896 }
897 data[count] = static_cast<const GLushort*>(indices)[0];
898 break;
899 case GL_UNSIGNED_INT:
900 for (int i = 0; i < count; i++)
901 {
902 data[i] = static_cast<const GLuint*>(indices)[i];
903 }
904 data[count] = static_cast<const GLuint*>(indices)[0];
905 break;
906 default: UNREACHABLE();
907 }
908
909 if (!mLineLoopIB->unmapBuffer())
910 {
911 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
912 return error(GL_OUT_OF_MEMORY);
913 }
914
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000915 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000916 {
917 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
918
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000919 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000920 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000921 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000922 }
923
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000924 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000925}
926
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000927void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
928{
929 // Get the raw indices for an indexed draw
930 if (type != GL_NONE && elementArrayBuffer)
931 {
932 gl::Buffer *indexBuffer = elementArrayBuffer;
933 intptr_t offset = reinterpret_cast<intptr_t>(indices);
934 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
935 }
936
937 if (!mTriangleFanIB)
938 {
939 mTriangleFanIB = new StreamingIndexBufferInterface(this);
940 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
941 {
942 delete mTriangleFanIB;
943 mTriangleFanIB = NULL;
944
945 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
946 return error(GL_OUT_OF_MEMORY);
947 }
948 }
949
950 const int numTris = count - 2;
951 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
952 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
953 {
954 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
955 return error(GL_OUT_OF_MEMORY);
956 }
957
958 void* mappedMemory = NULL;
959 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
960 if (offset == -1 || mappedMemory == NULL)
961 {
962 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
963 return error(GL_OUT_OF_MEMORY);
964 }
965
966 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
967 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
968
969 switch (type)
970 {
971 case GL_NONE: // Non-indexed draw
972 for (int i = 0; i < numTris; i++)
973 {
974 data[i*3 + 0] = 0;
975 data[i*3 + 1] = i + 1;
976 data[i*3 + 2] = i + 2;
977 }
978 break;
979 case GL_UNSIGNED_BYTE:
980 for (int i = 0; i < numTris; i++)
981 {
982 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
983 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
984 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
985 }
986 break;
987 case GL_UNSIGNED_SHORT:
988 for (int i = 0; i < numTris; i++)
989 {
990 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
991 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
992 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
993 }
994 break;
995 case GL_UNSIGNED_INT:
996 for (int i = 0; i < numTris; i++)
997 {
998 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
999 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1000 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1001 }
1002 break;
1003 default: UNREACHABLE();
1004 }
1005
1006 if (!mTriangleFanIB->unmapBuffer())
1007 {
1008 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
1009 return error(GL_OUT_OF_MEMORY);
1010 }
1011
1012 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1013 {
1014 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1015
1016 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1017 mAppliedIBSerial = mTriangleFanIB->getSerial();
1018 mAppliedIBOffset = indexBufferOffset;
1019 }
1020
1021 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1022}
1023
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001024void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1025{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001026 unsigned int programBinarySerial = programBinary->getSerial();
1027 if (programBinarySerial != mAppliedProgramBinarySerial)
1028 {
1029 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1030 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001031
daniel@transgaming.come4991412012-12-20 20:55:34 +00001032 ID3D11VertexShader *vertexShader = NULL;
1033 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001034
daniel@transgaming.come4991412012-12-20 20:55:34 +00001035 ID3D11PixelShader *pixelShader = NULL;
1036 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001037
daniel@transgaming.come4991412012-12-20 20:55:34 +00001038 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1039 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1040 programBinary->dirtyAllUniforms();
1041
1042 mAppliedProgramBinarySerial = programBinarySerial;
1043 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001044}
1045
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001046void Renderer11::applyUniforms(const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001047{
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001048 D3D11_BUFFER_DESC constantBufferDescriptionVS = {0};
1049 constantBufferDescriptionVS.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1050 constantBufferDescriptionVS.Usage = D3D11_USAGE_DYNAMIC;
1051 constantBufferDescriptionVS.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1052 constantBufferDescriptionVS.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1053 constantBufferDescriptionVS.MiscFlags = 0;
1054 constantBufferDescriptionVS.StructureByteStride = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001055
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001056 ID3D11Buffer *constantBufferVS = NULL;
1057 HRESULT result = mDevice->CreateBuffer(&constantBufferDescriptionVS, NULL, &constantBufferVS);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001058 ASSERT(SUCCEEDED(result));
1059
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001060 D3D11_BUFFER_DESC constantBufferDescriptionPS = {0};
1061 constantBufferDescriptionPS.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1062 constantBufferDescriptionPS.Usage = D3D11_USAGE_DYNAMIC;
1063 constantBufferDescriptionPS.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1064 constantBufferDescriptionPS.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1065 constantBufferDescriptionPS.MiscFlags = 0;
1066 constantBufferDescriptionPS.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001067
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001068 ID3D11Buffer *constantBufferPS = NULL;
1069 result = mDevice->CreateBuffer(&constantBufferDescriptionPS, NULL, &constantBufferPS);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001070 ASSERT(SUCCEEDED(result));
1071
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001072 D3D11_MAPPED_SUBRESOURCE mapVS = {0};
1073 result = mDeviceContext->Map(constantBufferVS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001074 ASSERT(SUCCEEDED(result));
1075
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001076 D3D11_MAPPED_SUBRESOURCE mapPS = {0};
1077 result = mDeviceContext->Map(constantBufferPS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001078 ASSERT(SUCCEEDED(result));
1079
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001080 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001081 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001082 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001083
1084 switch (uniform->type)
1085 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001086 case GL_SAMPLER_2D:
1087 case GL_SAMPLER_CUBE:
1088 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001089 case GL_FLOAT:
1090 case GL_FLOAT_VEC2:
1091 case GL_FLOAT_VEC3:
1092 case GL_FLOAT_VEC4:
1093 case GL_FLOAT_MAT2:
1094 case GL_FLOAT_MAT3:
1095 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001096 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001097 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001098 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001099 float (*f)[4] = (float(*)[4])uniform->data;
1100
1101 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001102 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001103 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1104 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1105 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1106 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001107 }
1108 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001109 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001110 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001111 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001112 float (*f)[4] = (float(*)[4])uniform->data;
1113
1114 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001115 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001116 c[uniform->psRegisterIndex + i][0] = f[i][0];
1117 c[uniform->psRegisterIndex + i][1] = f[i][1];
1118 c[uniform->psRegisterIndex + i][2] = f[i][2];
1119 c[uniform->psRegisterIndex + i][3] = f[i][3];
1120 }
1121 }
1122 break;
1123 case GL_INT:
1124 case GL_INT_VEC2:
1125 case GL_INT_VEC3:
1126 case GL_INT_VEC4:
1127 if (uniform->vsRegisterIndex >= 0)
1128 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001129 int (*c)[4] = (int(*)[4])mapVS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001130 GLint *x = (GLint*)uniform->data;
1131 int count = gl::VariableColumnCount(uniform->type);
1132
1133 for (unsigned int i = 0; i < uniform->registerCount; i++)
1134 {
1135 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1136 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1137 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1138 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1139 }
1140 }
1141 if (uniform->psRegisterIndex >= 0)
1142 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001143 int (*c)[4] = (int(*)[4])mapPS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001144 GLint *x = (GLint*)uniform->data;
1145 int count = gl::VariableColumnCount(uniform->type);
1146
1147 for (unsigned int i = 0; i < uniform->registerCount; i++)
1148 {
1149 if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0];
1150 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1151 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1152 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1153 }
1154 }
1155 break;
1156 case GL_BOOL:
1157 case GL_BOOL_VEC2:
1158 case GL_BOOL_VEC3:
1159 case GL_BOOL_VEC4:
1160 if (uniform->vsRegisterIndex >= 0)
1161 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001162 int (*c)[4] = (int(*)[4])mapVS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001163 GLboolean *b = (GLboolean*)uniform->data;
1164 int count = gl::VariableColumnCount(uniform->type);
1165
1166 for (unsigned int i = 0; i < uniform->registerCount; i++)
1167 {
1168 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1169 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1170 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1171 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1172 }
1173 }
1174 if (uniform->psRegisterIndex >= 0)
1175 {
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001176 int (*c)[4] = (int(*)[4])mapPS.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001177 GLboolean *b = (GLboolean*)uniform->data;
1178 int count = gl::VariableColumnCount(uniform->type);
1179
1180 for (unsigned int i = 0; i < uniform->registerCount; i++)
1181 {
1182 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1183 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1184 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1185 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001186 }
1187 }
1188 break;
1189 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001190 UNREACHABLE();
1191 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001192 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001193
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001194 mDeviceContext->Unmap(constantBufferVS, 0);
1195 mDeviceContext->VSSetConstantBuffers(0, 1, &constantBufferVS);
1196 constantBufferVS->Release();
1197
1198 mDeviceContext->Unmap(constantBufferPS, 0);
1199 mDeviceContext->PSSetConstantBuffers(0, 1, &constantBufferPS);
1200 constantBufferPS->Release();
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001201
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001202 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001203 if (!mDriverConstantBufferVS)
1204 {
1205 D3D11_BUFFER_DESC constantBufferDescription = {0};
1206 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1207 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1208 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1209 constantBufferDescription.CPUAccessFlags = 0;
1210 constantBufferDescription.MiscFlags = 0;
1211 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001212
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001213 result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
1214 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001215
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001216 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1217 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001218
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001219 if (!mDriverConstantBufferPS)
1220 {
1221 D3D11_BUFFER_DESC constantBufferDescription = {0};
1222 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1223 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1224 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1225 constantBufferDescription.CPUAccessFlags = 0;
1226 constantBufferDescription.MiscFlags = 0;
1227 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001228
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001229 result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
1230 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001231
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001232 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1233 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001234
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001235 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1236 {
1237 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1238 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1239 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001240
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001241 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1242 {
1243 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1244 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1245 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001246}
1247
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001248void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001249{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001250 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1251 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1252 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1253 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001254
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001255 unsigned int stencilUnmasked = 0x0;
1256 if (frameBuffer->hasStencil())
1257 {
1258 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1259 stencilUnmasked = (0x1 << stencilSize) - 1;
1260 }
1261 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1262 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001263
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001264 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1265 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1266 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001267
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001268 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1269 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001270 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001271 }
1272 else
1273 {
1274 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1275 {
1276 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1277 if (renderbufferObject)
1278 {
1279 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1280 if (!renderTarget)
1281 {
1282 ERR("render target pointer unexpectedly null.");
1283 return;
1284 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001285
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001286 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1287 if (!framebufferRTV)
1288 {
1289 ERR("render target view pointer unexpectedly null.");
1290 return;
1291 }
1292
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001293 const float clearValues[4] = { clearParams.colorClearValue.red,
1294 clearParams.colorClearValue.green,
1295 clearParams.colorClearValue.blue,
1296 clearParams.colorClearValue.alpha };
1297 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001298
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001299 framebufferRTV->Release();
1300 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001301 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001302 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001303 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001304 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1305 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001306 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001307 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1308 if (!renderTarget)
1309 {
1310 ERR("render target pointer unexpectedly null.");
1311 return;
1312 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001313
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001314 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1315 if (!framebufferDSV)
1316 {
1317 ERR("depth stencil view pointer unexpectedly null.");
1318 return;
1319 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001320
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001321 UINT clearFlags = 0;
1322 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1323 {
1324 clearFlags |= D3D11_CLEAR_DEPTH;
1325 }
1326 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1327 {
1328 clearFlags |= D3D11_CLEAR_STENCIL;
1329 }
1330
1331 float depthClear = clearParams.depthClearValue;
1332 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1333
1334 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001335
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001336 framebufferDSV->Release();
1337 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001338 }
1339 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001340}
1341
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001342void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1343{
1344 HRESULT result;
1345
1346 if (!mClearResourcesInitialized)
1347 {
1348 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1349
1350 D3D11_BUFFER_DESC vbDesc;
1351 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1352 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1353 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1354 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1355 vbDesc.MiscFlags = 0;
1356 vbDesc.StructureByteStride = 0;
1357
1358 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1359 ASSERT(SUCCEEDED(result));
1360 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1361
1362 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1363 {
1364 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1365 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1366 };
1367
1368 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1369 ASSERT(SUCCEEDED(result));
1370 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1371
1372 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1373 ASSERT(SUCCEEDED(result));
1374 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1375
1376 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1377 ASSERT(SUCCEEDED(result));
1378 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1379
1380 D3D11_RASTERIZER_DESC rsScissorDesc;
1381 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1382 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1383 rsScissorDesc.FrontCounterClockwise = FALSE;
1384 rsScissorDesc.DepthBias = 0;
1385 rsScissorDesc.DepthBiasClamp = 0.0f;
1386 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1387 rsScissorDesc.DepthClipEnable = FALSE;
1388 rsScissorDesc.ScissorEnable = TRUE;
1389 rsScissorDesc.MultisampleEnable = FALSE;
1390 rsScissorDesc.AntialiasedLineEnable = FALSE;
1391
1392 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1393 ASSERT(SUCCEEDED(result));
1394 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1395
1396 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1397 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1398 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1399 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1400 rsNoScissorDesc.DepthBias = 0;
1401 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1402 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1403 rsNoScissorDesc.DepthClipEnable = FALSE;
1404 rsNoScissorDesc.ScissorEnable = FALSE;
1405 rsNoScissorDesc.MultisampleEnable = FALSE;
1406 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1407
1408 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1409 ASSERT(SUCCEEDED(result));
1410 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1411
1412 mClearResourcesInitialized = true;
1413 }
1414
1415 // Prepare the depth stencil state to write depth values if the depth should be cleared
1416 // and stencil values if the stencil should be cleared
1417 gl::DepthStencilState glDSState;
1418 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1419 glDSState.depthFunc = GL_ALWAYS;
1420 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1421 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1422 glDSState.stencilFunc = GL_ALWAYS;
1423 glDSState.stencilMask = 0;
1424 glDSState.stencilFail = GL_REPLACE;
1425 glDSState.stencilPassDepthFail = GL_REPLACE;
1426 glDSState.stencilPassDepthPass = GL_REPLACE;
1427 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1428 glDSState.stencilBackFunc = GL_ALWAYS;
1429 glDSState.stencilBackMask = 0;
1430 glDSState.stencilBackFail = GL_REPLACE;
1431 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1432 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1433 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1434
1435 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1436
1437 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1438
1439 // Prepare the blend state to use a write mask if the color buffer should be cleared
1440 gl::BlendState glBlendState;
1441 glBlendState.blend = false;
1442 glBlendState.sourceBlendRGB = GL_ONE;
1443 glBlendState.destBlendRGB = GL_ZERO;
1444 glBlendState.sourceBlendAlpha = GL_ONE;
1445 glBlendState.destBlendAlpha = GL_ZERO;
1446 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1447 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1448 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1449 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1450 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1451 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1452 glBlendState.sampleAlphaToCoverage = false;
1453 glBlendState.dither = false;
1454
1455 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1456 static const UINT sampleMask = 0xFFFFFFFF;
1457
1458 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1459
1460 // Set the vertices
1461 D3D11_MAPPED_SUBRESOURCE mappedResource;
1462 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1463 if (FAILED(result))
1464 {
1465 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1466 return;
1467 }
1468
1469 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1470
1471 float depthClear = gl::clamp01(clearParams.depthClearValue);
1472 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1473 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1474 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1475 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1476
1477 mDeviceContext->Unmap(mClearVB, 0);
1478
1479 // Apply state
1480 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1481 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1482 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1483
1484 // Apply shaders
1485 mDeviceContext->IASetInputLayout(mClearIL);
1486 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1487 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
1488
1489 // Apply vertex buffer
1490 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1491 static UINT startIdx = 0;
1492 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1493 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1494
1495 // Draw the clear quad
1496 mDeviceContext->Draw(4, 0);
1497
1498 // Clean up
1499 markAllStateDirty();
1500}
1501
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001502void Renderer11::markAllStateDirty()
1503{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001504 mAppliedRenderTargetSerial = 0;
1505 mAppliedDepthbufferSerial = 0;
1506 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001507 mDepthStencilInitialized = false;
1508 mRenderTargetDescInitialized = false;
1509
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001510 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001511 {
1512 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001513 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001514 }
1515 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1516 {
1517 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001518 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001519 }
1520
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001521 mForceSetBlendState = true;
1522 mForceSetRasterState = true;
1523 mForceSetDepthStencilState = true;
1524 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001525 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001526
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001527 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001528 mAppliedIBOffset = 0;
1529
daniel@transgaming.come4991412012-12-20 20:55:34 +00001530 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1532 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001533}
1534
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001535void Renderer11::releaseDeviceResources()
1536{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001537 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001538 mInputLayoutCache.clear();
1539
1540 delete mVertexDataManager;
1541 mVertexDataManager = NULL;
1542
1543 delete mIndexDataManager;
1544 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001545
1546 delete mLineLoopIB;
1547 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001548
1549 delete mTriangleFanIB;
1550 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001551
1552 if (mCopyVB)
1553 {
1554 mCopyVB->Release();
1555 mCopyVB = NULL;
1556 }
1557
1558 if (mCopySampler)
1559 {
1560 mCopySampler->Release();
1561 mCopySampler = NULL;
1562 }
1563
1564 if (mCopyIL)
1565 {
1566 mCopyIL->Release();
1567 mCopyIL = NULL;
1568 }
1569
1570 if (mCopyVS)
1571 {
1572 mCopyVS->Release();
1573 mCopyVS = NULL;
1574 }
1575
1576 if (mCopyRGBAPS)
1577 {
1578 mCopyRGBAPS->Release();
1579 mCopyRGBAPS = NULL;
1580 }
1581
1582 if (mCopyRGBPS)
1583 {
1584 mCopyRGBPS->Release();
1585 mCopyRGBPS = NULL;
1586 }
1587
1588 if (mCopyLumPS)
1589 {
1590 mCopyLumPS->Release();
1591 mCopyLumPS = NULL;
1592 }
1593
1594 if (mCopyLumAlphaPS)
1595 {
1596 mCopyLumAlphaPS->Release();
1597 mCopyLumAlphaPS = NULL;
1598 }
1599
1600 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001601
1602 if (mClearVB)
1603 {
1604 mClearVB->Release();
1605 mClearVB = NULL;
1606 }
1607
1608 if (mClearIL)
1609 {
1610 mClearIL->Release();
1611 mClearIL = NULL;
1612 }
1613
1614 if (mClearVS)
1615 {
1616 mClearVS->Release();
1617 mClearVS = NULL;
1618 }
1619
1620 if (mClearPS)
1621 {
1622 mClearPS->Release();
1623 mClearPS = NULL;
1624 }
1625
1626 if (mClearScissorRS)
1627 {
1628 mClearScissorRS->Release();
1629 mClearScissorRS = NULL;
1630 }
1631
1632 if (mClearNoScissorRS)
1633 {
1634 mClearNoScissorRS->Release();
1635 mClearNoScissorRS = NULL;
1636 }
1637
1638 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001639
1640 if (mDriverConstantBufferVS)
1641 {
1642 mDriverConstantBufferVS->Release();
1643 mDriverConstantBufferVS = NULL;
1644 }
1645
1646 if (mDriverConstantBufferPS)
1647 {
1648 mDriverConstantBufferPS->Release();
1649 mDriverConstantBufferPS = NULL;
1650 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001651}
1652
1653void Renderer11::markDeviceLost()
1654{
1655 mDeviceLost = true;
1656}
1657
1658bool Renderer11::isDeviceLost()
1659{
1660 return mDeviceLost;
1661}
1662
1663// set notify to true to broadcast a message to all contexts of the device loss
1664bool Renderer11::testDeviceLost(bool notify)
1665{
1666 bool isLost = false;
1667
1668 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001669 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001670
1671 if (isLost)
1672 {
1673 // ensure we note the device loss --
1674 // we'll probably get this done again by markDeviceLost
1675 // but best to remember it!
1676 // Note that we don't want to clear the device loss status here
1677 // -- this needs to be done by resetDevice
1678 mDeviceLost = true;
1679 if (notify)
1680 {
1681 mDisplay->notifyDeviceLost();
1682 }
1683 }
1684
1685 return isLost;
1686}
1687
1688bool Renderer11::testDeviceResettable()
1689{
1690 HRESULT status = D3D_OK;
1691
1692 // TODO
1693 UNIMPLEMENTED();
1694
1695 switch (status)
1696 {
1697 case D3DERR_DEVICENOTRESET:
1698 case D3DERR_DEVICEHUNG:
1699 return true;
1700 default:
1701 return false;
1702 }
1703}
1704
1705bool Renderer11::resetDevice()
1706{
1707 releaseDeviceResources();
1708
1709 // TODO
1710 UNIMPLEMENTED();
1711
1712 // reset device defaults
1713 initializeDevice();
1714 mDeviceLost = false;
1715
1716 return true;
1717}
1718
1719DWORD Renderer11::getAdapterVendor() const
1720{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001721 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001722}
1723
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001724std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001725{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001726 std::ostringstream rendererString;
1727
1728 rendererString << mDescription;
1729 rendererString << " Direct3D11";
1730
1731 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1732 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1733
1734 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001735}
1736
1737GUID Renderer11::getAdapterIdentifier() const
1738{
1739 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001740 // UNIMPLEMENTED();
1741 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001742 return foo;
1743}
1744
1745bool Renderer11::getDXT1TextureSupport()
1746{
1747 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001748 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001749 return false;
1750}
1751
1752bool Renderer11::getDXT3TextureSupport()
1753{
1754 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001755 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001756 return false;
1757}
1758
1759bool Renderer11::getDXT5TextureSupport()
1760{
1761 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001762 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001763 return false;
1764}
1765
1766bool Renderer11::getDepthTextureSupport() const
1767{
1768 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001769 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001770 return false;
1771}
1772
1773bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1774{
1775 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001776 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001777
1778 *filtering = false;
1779 *renderable = false;
1780 return false;
1781}
1782
1783bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1784{
1785 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001786 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001787
1788 *filtering = false;
1789 *renderable = false;
1790 return false;
1791}
1792
1793bool Renderer11::getLuminanceTextureSupport()
1794{
1795 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001796 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797 return false;
1798}
1799
1800bool Renderer11::getLuminanceAlphaTextureSupport()
1801{
1802 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001803 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804 return false;
1805}
1806
1807bool Renderer11::getTextureFilterAnisotropySupport() const
1808{
1809 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001810 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001811 return false;
1812}
1813
1814float Renderer11::getTextureMaxAnisotropy() const
1815{
1816 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001817 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001818 return 1.0f;
1819}
1820
1821bool Renderer11::getEventQuerySupport()
1822{
1823 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001824 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001825 return false;
1826}
1827
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001828unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001829{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001830 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1831 switch (mFeatureLevel)
1832 {
1833 case D3D_FEATURE_LEVEL_11_0:
1834 case D3D_FEATURE_LEVEL_10_1:
1835 case D3D_FEATURE_LEVEL_10_0:
1836 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1837 default: UNREACHABLE();
1838 return 0;
1839 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001840}
1841
1842bool Renderer11::getNonPower2TextureSupport() const
1843{
1844 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001845 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001846 return false;
1847}
1848
1849bool Renderer11::getOcclusionQuerySupport() const
1850{
1851 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001852 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853 return false;
1854}
1855
1856bool Renderer11::getInstancingSupport() const
1857{
1858 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001859 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860 return false;
1861}
1862
1863bool Renderer11::getShareHandleSupport() const
1864{
1865 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001866 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001867
1868 // PIX doesn't seem to support using share handles, so disable them.
1869 return false && !gl::perfActive();
1870}
1871
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001872bool Renderer11::getDerivativeInstructionSupport() const
1873{
1874 // TODO
1875 // UNIMPLEMENTED();
1876 return false;
1877}
1878
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001879int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001880{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001881 switch (mFeatureLevel)
1882 {
1883 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001884 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001885 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1886 default: UNREACHABLE(); return 0;
1887 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001888}
1889
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001890int Renderer11::getMinorShaderModel() const
1891{
1892 switch (mFeatureLevel)
1893 {
1894 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1895 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1896 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1897 default: UNREACHABLE(); return 0;
1898 }
1899}
1900
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001901float Renderer11::getMaxPointSize() const
1902{
1903 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001904 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001905 return 1.0f;
1906}
1907
1908int Renderer11::getMaxTextureWidth() const
1909{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001910 switch (mFeatureLevel)
1911 {
1912 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1913 case D3D_FEATURE_LEVEL_10_1:
1914 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1915 default: UNREACHABLE(); return 0;
1916 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001917}
1918
1919int Renderer11::getMaxTextureHeight() const
1920{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001921 switch (mFeatureLevel)
1922 {
1923 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1924 case D3D_FEATURE_LEVEL_10_1:
1925 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1926 default: UNREACHABLE(); return 0;
1927 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001928}
1929
1930bool Renderer11::get32BitIndexSupport() const
1931{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001932 switch (mFeatureLevel)
1933 {
1934 case D3D_FEATURE_LEVEL_11_0:
1935 case D3D_FEATURE_LEVEL_10_1:
1936 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1937 default: UNREACHABLE(); return false;
1938 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001939}
1940
1941int Renderer11::getMinSwapInterval() const
1942{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001943 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001944}
1945
1946int Renderer11::getMaxSwapInterval() const
1947{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001948 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949}
1950
1951int Renderer11::getMaxSupportedSamples() const
1952{
1953 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001954 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001955 return 1;
1956}
1957
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001958bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001959{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001960 if (source && dest)
1961 {
1962 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1963 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1964
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001965 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001966 return true;
1967 }
1968
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001969 return false;
1970}
1971
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001972bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001973{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001974 if (source && dest)
1975 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001976 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1977 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001978
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001979 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001980 return true;
1981 }
1982
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001983 return false;
1984}
1985
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00001986bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001987 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001988{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001989 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1990 if (!colorbuffer)
1991 {
1992 ERR("Failed to retrieve the color buffer from the frame buffer.");
1993 return error(GL_OUT_OF_MEMORY, false);
1994 }
1995
1996 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1997 if (!sourceRenderTarget)
1998 {
1999 ERR("Failed to retrieve the render target from the frame buffer.");
2000 return error(GL_OUT_OF_MEMORY, false);
2001 }
2002
2003 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2004 if (!source)
2005 {
2006 ERR("Failed to retrieve the render target view from the render target.");
2007 return error(GL_OUT_OF_MEMORY, false);
2008 }
2009
2010 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2011 if (!storage11)
2012 {
2013 source->Release();
2014 ERR("Failed to retrieve the texture storage from the destination.");
2015 return error(GL_OUT_OF_MEMORY, false);
2016 }
2017
2018 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2019 if (!destRenderTarget)
2020 {
2021 source->Release();
2022 ERR("Failed to retrieve the render target from the destination storage.");
2023 return error(GL_OUT_OF_MEMORY, false);
2024 }
2025
2026 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2027 if (!dest)
2028 {
2029 source->Release();
2030 ERR("Failed to retrieve the render target view from the destination render target.");
2031 return error(GL_OUT_OF_MEMORY, false);
2032 }
2033
2034 gl::Rectangle destRect;
2035 destRect.x = xoffset;
2036 destRect.y = yoffset;
2037 destRect.width = sourceRect.width;
2038 destRect.height = sourceRect.height;
2039
2040 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2041 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2042
2043 source->Release();
2044 dest->Release();
2045
2046 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002047}
2048
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002049bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002050 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002051{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002052 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2053 if (!colorbuffer)
2054 {
2055 ERR("Failed to retrieve the color buffer from the frame buffer.");
2056 return error(GL_OUT_OF_MEMORY, false);
2057 }
2058
2059 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2060 if (!sourceRenderTarget)
2061 {
2062 ERR("Failed to retrieve the render target from the frame buffer.");
2063 return error(GL_OUT_OF_MEMORY, false);
2064 }
2065
2066 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2067 if (!source)
2068 {
2069 ERR("Failed to retrieve the render target view from the render target.");
2070 return error(GL_OUT_OF_MEMORY, false);
2071 }
2072
2073 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2074 if (!storage11)
2075 {
2076 source->Release();
2077 ERR("Failed to retrieve the texture storage from the destination.");
2078 return error(GL_OUT_OF_MEMORY, false);
2079 }
2080
2081 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2082 if (!destRenderTarget)
2083 {
2084 source->Release();
2085 ERR("Failed to retrieve the render target from the destination storage.");
2086 return error(GL_OUT_OF_MEMORY, false);
2087 }
2088
2089 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2090 if (!dest)
2091 {
2092 source->Release();
2093 ERR("Failed to retrieve the render target view from the destination render target.");
2094 return error(GL_OUT_OF_MEMORY, false);
2095 }
2096
2097 gl::Rectangle destRect;
2098 destRect.x = xoffset;
2099 destRect.y = yoffset;
2100 destRect.width = sourceRect.width;
2101 destRect.height = sourceRect.height;
2102
2103 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2104 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2105
2106 source->Release();
2107 dest->Release();
2108
2109 return ret;
2110}
2111
2112bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2113 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2114{
2115 HRESULT result;
2116
2117 if (!mCopyResourcesInitialized)
2118 {
2119 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2120
2121 D3D11_BUFFER_DESC vbDesc;
2122 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2123 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2124 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2125 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2126 vbDesc.MiscFlags = 0;
2127 vbDesc.StructureByteStride = 0;
2128
2129 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2130 ASSERT(SUCCEEDED(result));
2131 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2132
2133 D3D11_SAMPLER_DESC samplerDesc;
2134 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2135 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2136 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2137 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2138 samplerDesc.MipLODBias = 0.0f;
2139 samplerDesc.MaxAnisotropy = 0;
2140 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2141 samplerDesc.BorderColor[0] = 0.0f;
2142 samplerDesc.BorderColor[1] = 0.0f;
2143 samplerDesc.BorderColor[2] = 0.0f;
2144 samplerDesc.BorderColor[3] = 0.0f;
2145 samplerDesc.MinLOD = 0.0f;
2146 samplerDesc.MaxLOD = 0.0f;
2147
2148 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2149 ASSERT(SUCCEEDED(result));
2150 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2151
2152 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2153 {
2154 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2155 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2156 };
2157
2158 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2159 ASSERT(SUCCEEDED(result));
2160 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2161
2162 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2163 ASSERT(SUCCEEDED(result));
2164 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2165
2166 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2167 ASSERT(SUCCEEDED(result));
2168 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2169
2170 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2171 ASSERT(SUCCEEDED(result));
2172 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2173
2174 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2175 ASSERT(SUCCEEDED(result));
2176 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2177
2178 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2179 ASSERT(SUCCEEDED(result));
2180 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2181
2182 mCopyResourcesInitialized = true;
2183 }
2184
2185 // Verify the source and destination area sizes
2186 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2187 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2188 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2189 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2190 {
2191 return error(GL_INVALID_VALUE, false);
2192 }
2193
2194 // Set vertices
2195 D3D11_MAPPED_SUBRESOURCE mappedResource;
2196 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2197 if (FAILED(result))
2198 {
2199 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
2200 return error(GL_OUT_OF_MEMORY, false);
2201 }
2202
2203 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2204
2205 // Create a quad in homogeneous coordinates
2206 float x1 = (destArea.x / destWidth) * 2.0f - 1.0f;
2207 float y1 = (destArea.y / destHeight) * 2.0f - 1.0f;
2208 float x2 = ((destArea.x + destArea.width) / destWidth) * 2.0f - 1.0f;
2209 float y2 = ((destArea.y + destArea.height) / destHeight) * 2.0f - 1.0f;
2210
2211 float u1 = sourceArea.x / float(sourceWidth);
2212 float v1 = sourceArea.y / float(sourceHeight);
2213 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2214 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2215
2216 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2217 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2218 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2219 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2220
2221 mDeviceContext->Unmap(mCopyVB, 0);
2222
2223 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2224 static UINT startIdx = 0;
2225 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2226
2227 // Apply state
2228 static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
2229 mDeviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
2230 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2231 mDeviceContext->RSSetState(NULL);
2232
2233 // Apply shaders
2234 mDeviceContext->IASetInputLayout(mCopyIL);
2235 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2236 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2237
2238 ID3D11PixelShader *ps = NULL;
2239 switch(destFormat)
2240 {
2241 case GL_RGBA: ps = mCopyRGBAPS; break;
2242 case GL_RGB: ps = mCopyRGBPS; break;
2243 case GL_ALPHA: ps = mCopyRGBAPS; break;
2244 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2245 case GL_LUMINANCE: ps = mCopyLumPS; break;
2246 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2247 default: UNREACHABLE(); ps = NULL; break;
2248 }
2249
2250 mDeviceContext->PSSetShader(ps, NULL, 0);
2251
2252 // Unset the currently bound shader resource to avoid conflicts
2253 static ID3D11ShaderResourceView *const nullSRV = NULL;
2254 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2255
2256 // Apply render targets
2257 mDeviceContext->OMSetRenderTargets(1, &dest, NULL);
2258
2259 // Set the viewport
2260 D3D11_VIEWPORT viewport;
2261 viewport.TopLeftX = 0;
2262 viewport.TopLeftY = 0;
2263 viewport.Width = destWidth;
2264 viewport.Height = destHeight;
2265 viewport.MinDepth = 0.0f;
2266 viewport.MaxDepth = 1.0f;
2267 mDeviceContext->RSSetViewports(1, &viewport);
2268
2269 // Apply textures
2270 mDeviceContext->PSSetShaderResources(0, 1, &source);
2271 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2272
2273 // Draw the quad
2274 mDeviceContext->Draw(4, 0);
2275
2276 // Unbind textures and render targets and vertex buffer
2277 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2278
2279 static ID3D11RenderTargetView *const nullRTV = NULL;
2280 mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL);
2281
2282 static UINT zero = 0;
2283 static ID3D11Buffer *const nullBuffer = NULL;
2284 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2285
2286 markAllStateDirty();
2287
2288 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002289}
2290
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002291RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2292{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002293 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002294 RenderTarget11 *renderTarget = NULL;
2295 if (depth)
2296 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002297 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL,
2298 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002299 }
2300 else
2301 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002302 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
2303 swapChain11->getRenderTargetShaderResource(),
2304 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002305 }
2306 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002307}
2308
2309RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2310{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002311 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2312 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002313}
2314
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002315ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002316{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002317 ShaderExecutable11 *executable = NULL;
2318
2319 switch (type)
2320 {
2321 case GL_VERTEX_SHADER:
2322 {
2323 ID3D11VertexShader *vshader = NULL;
2324 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2325 ASSERT(SUCCEEDED(result));
2326
2327 if (vshader)
2328 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002329 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002330 }
2331 }
2332 break;
2333 case GL_FRAGMENT_SHADER:
2334 {
2335 ID3D11PixelShader *pshader = NULL;
2336 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2337 ASSERT(SUCCEEDED(result));
2338
2339 if (pshader)
2340 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002341 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002342 }
2343 }
2344 break;
2345 default:
2346 UNREACHABLE();
2347 break;
2348 }
2349
2350 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002351}
2352
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002353ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2354{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002355 const char *profile = NULL;
2356
2357 switch (type)
2358 {
2359 case GL_VERTEX_SHADER:
2360 profile = "vs_4_0";
2361 break;
2362 case GL_FRAGMENT_SHADER:
2363 profile = "ps_4_0";
2364 break;
2365 default:
2366 UNREACHABLE();
2367 return NULL;
2368 }
2369
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002370 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002371 if (!binary)
2372 return NULL;
2373
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002374 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002375 binary->Release();
2376
2377 return executable;
2378}
2379
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002380VertexBuffer *Renderer11::createVertexBuffer()
2381{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002382 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002383}
2384
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002385IndexBuffer *Renderer11::createIndexBuffer()
2386{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002387 return new IndexBuffer11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002388}
2389
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002390bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
2391{
2392 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2393 if (colorbuffer)
2394 {
2395 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2396 if (renderTarget)
2397 {
2398 *subresourceIndex = renderTarget->getSubresourceIndex();
2399
2400 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2401 if (colorBufferRTV)
2402 {
2403 ID3D11Resource *textureResource = NULL;
2404 colorBufferRTV->GetResource(&textureResource);
2405 colorBufferRTV->Release();
2406
2407 if (textureResource)
2408 {
2409 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
2410 textureResource->Release();
2411
2412 if (SUCCEEDED(result))
2413 {
2414 return true;
2415 }
2416 else
2417 {
2418 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2419 "HRESULT: 0x%X.", result);
2420 }
2421 }
2422 }
2423 }
2424 }
2425
2426 return false;
2427}
2428
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002429bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
2430 bool blitRenderTarget, bool blitDepthStencil)
2431{
2432 // TODO
2433 UNIMPLEMENTED();
2434 return false;
2435}
2436
2437void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2438 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2439{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002440 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002441 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002442
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002443 if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002444 {
2445 gl::Rectangle area;
2446 area.x = x;
2447 area.y = y;
2448 area.width = width;
2449 area.height = height;
2450
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002451 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2452 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002453
2454 colorBufferTexture->Release();
2455 colorBufferTexture = NULL;
2456 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002457}
2458
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002459Image *Renderer11::createImage()
2460{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002461 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002462}
2463
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002464void Renderer11::generateMipmap(Image *dest, Image *src)
2465{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002466 Image11 *dest11 = Image11::makeImage11(dest);
2467 Image11 *src11 = Image11::makeImage11(src);
2468 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002469}
2470
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002471TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2472{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002473 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2474 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002475}
2476
2477TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2478{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002479 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002480}
2481
2482TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2483{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002484 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002485}
2486
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002487static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
2488{
2489 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
2490 destFormat == GL_ALPHA &&
2491 destType == GL_UNSIGNED_BYTE)
2492 {
2493 return 1;
2494 }
2495 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
2496 destFormat == GL_RGBA &&
2497 destType == GL_UNSIGNED_BYTE)
2498 {
2499 return 4;
2500 }
2501 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
2502 destFormat == GL_BGRA_EXT &&
2503 destType == GL_UNSIGNED_BYTE)
2504 {
2505 return 4;
2506 }
2507 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
2508 destFormat == GL_RGBA &&
2509 destType == GL_HALF_FLOAT_OES)
2510 {
2511 return 8;
2512 }
2513 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
2514 destFormat == GL_RGB &&
2515 destType == GL_FLOAT)
2516 {
2517 return 12;
2518 }
2519 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
2520 destFormat == GL_RGBA &&
2521 destType == GL_FLOAT)
2522 {
2523 return 16;
2524 }
2525 else
2526 {
2527 return 0;
2528 }
2529}
2530
2531static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
2532 unsigned int y, int inputPitch, gl::Color *outColor)
2533{
2534 switch (format)
2535 {
2536 case DXGI_FORMAT_R8G8B8A8_UNORM:
2537 {
2538 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2539 outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
2540 outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
2541 outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
2542 outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
2543 }
2544 break;
2545
2546 case DXGI_FORMAT_A8_UNORM:
2547 {
2548 outColor->red = 0.0f;
2549 outColor->green = 0.0f;
2550 outColor->blue = 0.0f;
2551 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
2552 }
2553 break;
2554
2555 case DXGI_FORMAT_R32G32B32A32_FLOAT:
2556 {
2557 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
2558 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
2559 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
2560 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
2561 }
2562 break;
2563
2564 case DXGI_FORMAT_R32G32B32_FLOAT:
2565 {
2566 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
2567 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
2568 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
2569 outColor->alpha = 1.0f;
2570 }
2571 break;
2572
2573 case DXGI_FORMAT_R16G16B16A16_FLOAT:
2574 {
2575 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
2576 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
2577 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
2578 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
2579 }
2580 break;
2581
2582 case DXGI_FORMAT_B8G8R8A8_UNORM:
2583 {
2584 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2585 outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
2586 outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
2587 outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
2588 outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
2589 }
2590 break;
2591
2592 case DXGI_FORMAT_R8_UNORM:
2593 {
2594 outColor->red = *(data + x + y * inputPitch) / 255.0f;
2595 outColor->green = 0.0f;
2596 outColor->blue = 0.0f;
2597 outColor->alpha = 1.0f;
2598 }
2599 break;
2600
2601 case DXGI_FORMAT_R8G8_UNORM:
2602 {
2603 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
2604
2605 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
2606 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
2607 outColor->blue = 0.0f;
2608 outColor->alpha = 1.0f;
2609 }
2610 break;
2611
2612 case DXGI_FORMAT_R16_FLOAT:
2613 {
2614 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
2615 outColor->green = 0.0f;
2616 outColor->blue = 0.0f;
2617 outColor->alpha = 1.0f;
2618 }
2619 break;
2620
2621 case DXGI_FORMAT_R16G16_FLOAT:
2622 {
2623 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
2624 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
2625 outColor->blue = 0.0f;
2626 outColor->alpha = 1.0f;
2627 }
2628 break;
2629
2630 default:
2631 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
2632 UNIMPLEMENTED();
2633 break;
2634 }
2635}
2636
2637static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
2638 unsigned int y, int outputPitch, void *outData)
2639{
2640 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
2641 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
2642
2643 switch (format)
2644 {
2645 case GL_RGBA:
2646 switch (type)
2647 {
2648 case GL_UNSIGNED_BYTE:
2649 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2650 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2651 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2652 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2653 break;
2654
2655 default:
2656 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
2657 UNIMPLEMENTED();
2658 break;
2659 }
2660 break;
2661
2662 case GL_BGRA_EXT:
2663 switch (type)
2664 {
2665 case GL_UNSIGNED_BYTE:
2666 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2667 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2668 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
2669 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2670 break;
2671
2672 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2673 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2674 // this type is packed as follows:
2675 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2676 // --------------------------------------------------------------------------------
2677 // | 4th | 3rd | 2nd | 1st component |
2678 // --------------------------------------------------------------------------------
2679 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2680 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2681 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
2682 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
2683 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
2684 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
2685 break;
2686
2687 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2688 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2689 // this type is packed as follows:
2690 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2691 // --------------------------------------------------------------------------------
2692 // | 4th | 3rd | 2nd | 1st component |
2693 // --------------------------------------------------------------------------------
2694 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2695 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2696 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
2697 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
2698 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
2699 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
2700 break;
2701
2702 default:
2703 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
2704 UNIMPLEMENTED();
2705 break;
2706 }
2707 break;
2708
2709 case GL_RGB:
2710 switch (type)
2711 {
2712 case GL_UNSIGNED_SHORT_5_6_5:
2713 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2714 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
2715 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
2716 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
2717 break;
2718
2719 case GL_UNSIGNED_BYTE:
2720 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2721 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2722 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2723 break;
2724
2725 default:
2726 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
2727 UNIMPLEMENTED();
2728 break;
2729 }
2730 break;
2731
2732 default:
2733 ERR("WritePixelColor not implemented for format 0x%X.", format);
2734 UNIMPLEMENTED();
2735 break;
2736 }
2737}
2738
2739void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2740 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2741 GLint packAlignment, void *pixels)
2742{
2743 D3D11_TEXTURE2D_DESC textureDesc;
2744 texture->GetDesc(&textureDesc);
2745
2746 D3D11_TEXTURE2D_DESC stagingDesc;
2747 stagingDesc.Width = area.width;
2748 stagingDesc.Height = area.height;
2749 stagingDesc.MipLevels = 1;
2750 stagingDesc.ArraySize = 1;
2751 stagingDesc.Format = textureDesc.Format;
2752 stagingDesc.SampleDesc.Count = 1;
2753 stagingDesc.SampleDesc.Quality = 0;
2754 stagingDesc.Usage = D3D11_USAGE_STAGING;
2755 stagingDesc.BindFlags = 0;
2756 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2757 stagingDesc.MiscFlags = 0;
2758
2759 ID3D11Texture2D* stagingTex = NULL;
2760 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2761 if (FAILED(result))
2762 {
2763 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2764 return;
2765 }
2766
2767 ID3D11Texture2D* srcTex = NULL;
2768 if (textureDesc.SampleDesc.Count > 1)
2769 {
2770 D3D11_TEXTURE2D_DESC resolveDesc;
2771 resolveDesc.Width = textureDesc.Width;
2772 resolveDesc.Height = textureDesc.Height;
2773 resolveDesc.MipLevels = 1;
2774 resolveDesc.ArraySize = 1;
2775 resolveDesc.Format = textureDesc.Format;
2776 resolveDesc.SampleDesc.Count = 1;
2777 resolveDesc.SampleDesc.Quality = 0;
2778 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2779 resolveDesc.BindFlags = 0;
2780 resolveDesc.CPUAccessFlags = 0;
2781 resolveDesc.MiscFlags = 0;
2782
2783 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2784 if (FAILED(result))
2785 {
2786 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
2787 stagingTex->Release();
2788 return;
2789 }
2790
2791 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2792 subResource = 0;
2793 }
2794 else
2795 {
2796 srcTex = texture;
2797 srcTex->AddRef();
2798 }
2799
2800 D3D11_BOX srcBox;
2801 srcBox.left = area.x;
2802 srcBox.right = area.x + area.width;
2803 srcBox.top = area.y;
2804 srcBox.bottom = area.y + area.height;
2805 srcBox.front = 0;
2806 srcBox.back = 1;
2807
2808 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2809
2810 srcTex->Release();
2811 srcTex = NULL;
2812
2813 D3D11_MAPPED_SUBRESOURCE mapping;
2814 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
2815
2816 unsigned char *source;
2817 int inputPitch;
2818 if (packReverseRowOrder)
2819 {
2820 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
2821 inputPitch = -static_cast<int>(mapping.RowPitch);
2822 }
2823 else
2824 {
2825 source = static_cast<unsigned char*>(mapping.pData);
2826 inputPitch = static_cast<int>(mapping.RowPitch);
2827 }
2828
2829 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
2830 if (fastPixelSize != 0)
2831 {
2832 unsigned char *dest = static_cast<unsigned char*>(pixels);
2833 for (int j = 0; j < area.height; j++)
2834 {
2835 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
2836 }
2837 }
2838 else
2839 {
2840 gl::Color pixelColor;
2841 for (int j = 0; j < area.height; j++)
2842 {
2843 for (int i = 0; i < area.width; i++)
2844 {
2845 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
2846 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
2847 }
2848 }
2849 }
2850
2851 mDeviceContext->Unmap(stagingTex, 0);
2852
2853 stagingTex->Release();
2854 stagingTex = NULL;
2855}
2856
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002857}