blob: 10f1165eb434330e21a0afb6909204c9c8ffba92 [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
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +00001046void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001047{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001048 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1049 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001050
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001051 unsigned int totalRegisterCountVS = 0;
1052 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001053
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001054 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1055 {
1056 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001057
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001058 if (uniform->vsRegisterIndex >= 0)
1059 {
1060 totalRegisterCountVS += uniform->registerCount;
1061 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001062
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001063 if (uniform->psRegisterIndex >= 0)
1064 {
1065 totalRegisterCountPS += uniform->registerCount;
1066 }
1067 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001068
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001069 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1070 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1071
1072 void *mapVS = new float[4 * totalRegisterCountVS];
1073 void *mapPS = new float[4 * totalRegisterCountPS];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001074
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001075 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001076 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001077 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001078
1079 switch (uniform->type)
1080 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001081 case GL_SAMPLER_2D:
1082 case GL_SAMPLER_CUBE:
1083 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001084 case GL_FLOAT:
1085 case GL_FLOAT_VEC2:
1086 case GL_FLOAT_VEC3:
1087 case GL_FLOAT_VEC4:
1088 case GL_FLOAT_MAT2:
1089 case GL_FLOAT_MAT3:
1090 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001091 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001092 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001093 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001094 float (*f)[4] = (float(*)[4])uniform->data;
1095
1096 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001097 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001098 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1099 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1100 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1101 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001102 }
1103 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001104 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001105 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001106 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001107 float (*f)[4] = (float(*)[4])uniform->data;
1108
1109 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001110 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001111 c[uniform->psRegisterIndex + i][0] = f[i][0];
1112 c[uniform->psRegisterIndex + i][1] = f[i][1];
1113 c[uniform->psRegisterIndex + i][2] = f[i][2];
1114 c[uniform->psRegisterIndex + i][3] = f[i][3];
1115 }
1116 }
1117 break;
1118 case GL_INT:
1119 case GL_INT_VEC2:
1120 case GL_INT_VEC3:
1121 case GL_INT_VEC4:
1122 if (uniform->vsRegisterIndex >= 0)
1123 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001124 int (*c)[4] = (int(*)[4])mapVS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001125 GLint *x = (GLint*)uniform->data;
1126 int count = gl::VariableColumnCount(uniform->type);
1127
1128 for (unsigned int i = 0; i < uniform->registerCount; i++)
1129 {
1130 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1131 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1132 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1133 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1134 }
1135 }
1136 if (uniform->psRegisterIndex >= 0)
1137 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001138 int (*c)[4] = (int(*)[4])mapPS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001139 GLint *x = (GLint*)uniform->data;
1140 int count = gl::VariableColumnCount(uniform->type);
1141
1142 for (unsigned int i = 0; i < uniform->registerCount; i++)
1143 {
1144 if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0];
1145 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1146 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1147 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1148 }
1149 }
1150 break;
1151 case GL_BOOL:
1152 case GL_BOOL_VEC2:
1153 case GL_BOOL_VEC3:
1154 case GL_BOOL_VEC4:
1155 if (uniform->vsRegisterIndex >= 0)
1156 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001157 int (*c)[4] = (int(*)[4])mapVS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001158 GLboolean *b = (GLboolean*)uniform->data;
1159 int count = gl::VariableColumnCount(uniform->type);
1160
1161 for (unsigned int i = 0; i < uniform->registerCount; i++)
1162 {
1163 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1164 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1165 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1166 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1167 }
1168 }
1169 if (uniform->psRegisterIndex >= 0)
1170 {
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001171 int (*c)[4] = (int(*)[4])mapPS;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001172 GLboolean *b = (GLboolean*)uniform->data;
1173 int count = gl::VariableColumnCount(uniform->type);
1174
1175 for (unsigned int i = 0; i < uniform->registerCount; i++)
1176 {
1177 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1178 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1179 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1180 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001181 }
1182 }
1183 break;
1184 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001185 UNREACHABLE();
1186 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001187 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001188
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001189 if (vertexConstantBuffer)
1190 {
1191 mDeviceContext->UpdateSubresource(vertexConstantBuffer, 0, NULL, mapVS, 0, 0);
1192 }
1193
1194 if (pixelConstantBuffer)
1195 {
1196 mDeviceContext->UpdateSubresource(pixelConstantBuffer, 0, NULL, mapPS, 0, 0);
1197 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001198
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001199 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1200 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1201
1202 delete[] mapVS;
1203 delete[] mapPS;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001204
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001205 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001206 if (!mDriverConstantBufferVS)
1207 {
1208 D3D11_BUFFER_DESC constantBufferDescription = {0};
1209 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1210 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1211 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1212 constantBufferDescription.CPUAccessFlags = 0;
1213 constantBufferDescription.MiscFlags = 0;
1214 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001215
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001216 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001217 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001218
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001219 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1220 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001221
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001222 if (!mDriverConstantBufferPS)
1223 {
1224 D3D11_BUFFER_DESC constantBufferDescription = {0};
1225 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1226 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1227 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1228 constantBufferDescription.CPUAccessFlags = 0;
1229 constantBufferDescription.MiscFlags = 0;
1230 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001231
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001232 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001233 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001234
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001235 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1236 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001237
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001238 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1239 {
1240 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1241 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1242 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001243
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001244 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1245 {
1246 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1247 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1248 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001249}
1250
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001251void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001252{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001253 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1254 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1255 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1256 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001257
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001258 unsigned int stencilUnmasked = 0x0;
1259 if (frameBuffer->hasStencil())
1260 {
1261 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1262 stencilUnmasked = (0x1 << stencilSize) - 1;
1263 }
1264 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1265 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001266
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001267 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1268 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1269 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001270
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001271 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1272 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001273 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001274 }
1275 else
1276 {
1277 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1278 {
1279 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1280 if (renderbufferObject)
1281 {
1282 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1283 if (!renderTarget)
1284 {
1285 ERR("render target pointer unexpectedly null.");
1286 return;
1287 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001288
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001289 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1290 if (!framebufferRTV)
1291 {
1292 ERR("render target view pointer unexpectedly null.");
1293 return;
1294 }
1295
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001296 const float clearValues[4] = { clearParams.colorClearValue.red,
1297 clearParams.colorClearValue.green,
1298 clearParams.colorClearValue.blue,
1299 clearParams.colorClearValue.alpha };
1300 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001301
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001302 framebufferRTV->Release();
1303 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001304 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001305 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001306 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001307 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1308 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001309 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001310 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1311 if (!renderTarget)
1312 {
1313 ERR("render target pointer unexpectedly null.");
1314 return;
1315 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001316
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001317 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1318 if (!framebufferDSV)
1319 {
1320 ERR("depth stencil view pointer unexpectedly null.");
1321 return;
1322 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001323
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001324 UINT clearFlags = 0;
1325 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1326 {
1327 clearFlags |= D3D11_CLEAR_DEPTH;
1328 }
1329 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1330 {
1331 clearFlags |= D3D11_CLEAR_STENCIL;
1332 }
1333
1334 float depthClear = clearParams.depthClearValue;
1335 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1336
1337 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001338
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001339 framebufferDSV->Release();
1340 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001341 }
1342 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001343}
1344
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001345void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1346{
1347 HRESULT result;
1348
1349 if (!mClearResourcesInitialized)
1350 {
1351 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1352
1353 D3D11_BUFFER_DESC vbDesc;
1354 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1355 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1356 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1357 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1358 vbDesc.MiscFlags = 0;
1359 vbDesc.StructureByteStride = 0;
1360
1361 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1362 ASSERT(SUCCEEDED(result));
1363 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1364
1365 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1366 {
1367 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1368 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1369 };
1370
1371 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1372 ASSERT(SUCCEEDED(result));
1373 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1374
1375 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1376 ASSERT(SUCCEEDED(result));
1377 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1378
1379 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1380 ASSERT(SUCCEEDED(result));
1381 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1382
1383 D3D11_RASTERIZER_DESC rsScissorDesc;
1384 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1385 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1386 rsScissorDesc.FrontCounterClockwise = FALSE;
1387 rsScissorDesc.DepthBias = 0;
1388 rsScissorDesc.DepthBiasClamp = 0.0f;
1389 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1390 rsScissorDesc.DepthClipEnable = FALSE;
1391 rsScissorDesc.ScissorEnable = TRUE;
1392 rsScissorDesc.MultisampleEnable = FALSE;
1393 rsScissorDesc.AntialiasedLineEnable = FALSE;
1394
1395 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1396 ASSERT(SUCCEEDED(result));
1397 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1398
1399 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1400 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1401 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1402 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1403 rsNoScissorDesc.DepthBias = 0;
1404 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1405 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1406 rsNoScissorDesc.DepthClipEnable = FALSE;
1407 rsNoScissorDesc.ScissorEnable = FALSE;
1408 rsNoScissorDesc.MultisampleEnable = FALSE;
1409 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1410
1411 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1412 ASSERT(SUCCEEDED(result));
1413 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1414
1415 mClearResourcesInitialized = true;
1416 }
1417
1418 // Prepare the depth stencil state to write depth values if the depth should be cleared
1419 // and stencil values if the stencil should be cleared
1420 gl::DepthStencilState glDSState;
1421 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1422 glDSState.depthFunc = GL_ALWAYS;
1423 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1424 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1425 glDSState.stencilFunc = GL_ALWAYS;
1426 glDSState.stencilMask = 0;
1427 glDSState.stencilFail = GL_REPLACE;
1428 glDSState.stencilPassDepthFail = GL_REPLACE;
1429 glDSState.stencilPassDepthPass = GL_REPLACE;
1430 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1431 glDSState.stencilBackFunc = GL_ALWAYS;
1432 glDSState.stencilBackMask = 0;
1433 glDSState.stencilBackFail = GL_REPLACE;
1434 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1435 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1436 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1437
1438 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1439
1440 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1441
1442 // Prepare the blend state to use a write mask if the color buffer should be cleared
1443 gl::BlendState glBlendState;
1444 glBlendState.blend = false;
1445 glBlendState.sourceBlendRGB = GL_ONE;
1446 glBlendState.destBlendRGB = GL_ZERO;
1447 glBlendState.sourceBlendAlpha = GL_ONE;
1448 glBlendState.destBlendAlpha = GL_ZERO;
1449 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1450 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1451 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1452 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1453 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1454 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1455 glBlendState.sampleAlphaToCoverage = false;
1456 glBlendState.dither = false;
1457
1458 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1459 static const UINT sampleMask = 0xFFFFFFFF;
1460
1461 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1462
1463 // Set the vertices
1464 D3D11_MAPPED_SUBRESOURCE mappedResource;
1465 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1466 if (FAILED(result))
1467 {
1468 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1469 return;
1470 }
1471
1472 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1473
1474 float depthClear = gl::clamp01(clearParams.depthClearValue);
1475 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1476 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1477 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1478 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1479
1480 mDeviceContext->Unmap(mClearVB, 0);
1481
1482 // Apply state
1483 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1484 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1485 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1486
1487 // Apply shaders
1488 mDeviceContext->IASetInputLayout(mClearIL);
1489 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1490 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
1491
1492 // Apply vertex buffer
1493 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1494 static UINT startIdx = 0;
1495 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1496 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1497
1498 // Draw the clear quad
1499 mDeviceContext->Draw(4, 0);
1500
1501 // Clean up
1502 markAllStateDirty();
1503}
1504
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001505void Renderer11::markAllStateDirty()
1506{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001507 mAppliedRenderTargetSerial = 0;
1508 mAppliedDepthbufferSerial = 0;
1509 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001510 mDepthStencilInitialized = false;
1511 mRenderTargetDescInitialized = false;
1512
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001513 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001514 {
1515 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001516 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001517 }
1518 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1519 {
1520 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001521 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001522 }
1523
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001524 mForceSetBlendState = true;
1525 mForceSetRasterState = true;
1526 mForceSetDepthStencilState = true;
1527 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001528 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001529
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001530 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001531 mAppliedIBOffset = 0;
1532
daniel@transgaming.come4991412012-12-20 20:55:34 +00001533 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001534 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1535 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001536}
1537
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001538void Renderer11::releaseDeviceResources()
1539{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001540 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001541 mInputLayoutCache.clear();
1542
1543 delete mVertexDataManager;
1544 mVertexDataManager = NULL;
1545
1546 delete mIndexDataManager;
1547 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001548
1549 delete mLineLoopIB;
1550 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001551
1552 delete mTriangleFanIB;
1553 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001554
1555 if (mCopyVB)
1556 {
1557 mCopyVB->Release();
1558 mCopyVB = NULL;
1559 }
1560
1561 if (mCopySampler)
1562 {
1563 mCopySampler->Release();
1564 mCopySampler = NULL;
1565 }
1566
1567 if (mCopyIL)
1568 {
1569 mCopyIL->Release();
1570 mCopyIL = NULL;
1571 }
1572
1573 if (mCopyVS)
1574 {
1575 mCopyVS->Release();
1576 mCopyVS = NULL;
1577 }
1578
1579 if (mCopyRGBAPS)
1580 {
1581 mCopyRGBAPS->Release();
1582 mCopyRGBAPS = NULL;
1583 }
1584
1585 if (mCopyRGBPS)
1586 {
1587 mCopyRGBPS->Release();
1588 mCopyRGBPS = NULL;
1589 }
1590
1591 if (mCopyLumPS)
1592 {
1593 mCopyLumPS->Release();
1594 mCopyLumPS = NULL;
1595 }
1596
1597 if (mCopyLumAlphaPS)
1598 {
1599 mCopyLumAlphaPS->Release();
1600 mCopyLumAlphaPS = NULL;
1601 }
1602
1603 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001604
1605 if (mClearVB)
1606 {
1607 mClearVB->Release();
1608 mClearVB = NULL;
1609 }
1610
1611 if (mClearIL)
1612 {
1613 mClearIL->Release();
1614 mClearIL = NULL;
1615 }
1616
1617 if (mClearVS)
1618 {
1619 mClearVS->Release();
1620 mClearVS = NULL;
1621 }
1622
1623 if (mClearPS)
1624 {
1625 mClearPS->Release();
1626 mClearPS = NULL;
1627 }
1628
1629 if (mClearScissorRS)
1630 {
1631 mClearScissorRS->Release();
1632 mClearScissorRS = NULL;
1633 }
1634
1635 if (mClearNoScissorRS)
1636 {
1637 mClearNoScissorRS->Release();
1638 mClearNoScissorRS = NULL;
1639 }
1640
1641 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001642
1643 if (mDriverConstantBufferVS)
1644 {
1645 mDriverConstantBufferVS->Release();
1646 mDriverConstantBufferVS = NULL;
1647 }
1648
1649 if (mDriverConstantBufferPS)
1650 {
1651 mDriverConstantBufferPS->Release();
1652 mDriverConstantBufferPS = NULL;
1653 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001654}
1655
1656void Renderer11::markDeviceLost()
1657{
1658 mDeviceLost = true;
1659}
1660
1661bool Renderer11::isDeviceLost()
1662{
1663 return mDeviceLost;
1664}
1665
1666// set notify to true to broadcast a message to all contexts of the device loss
1667bool Renderer11::testDeviceLost(bool notify)
1668{
1669 bool isLost = false;
1670
1671 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001672 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001673
1674 if (isLost)
1675 {
1676 // ensure we note the device loss --
1677 // we'll probably get this done again by markDeviceLost
1678 // but best to remember it!
1679 // Note that we don't want to clear the device loss status here
1680 // -- this needs to be done by resetDevice
1681 mDeviceLost = true;
1682 if (notify)
1683 {
1684 mDisplay->notifyDeviceLost();
1685 }
1686 }
1687
1688 return isLost;
1689}
1690
1691bool Renderer11::testDeviceResettable()
1692{
1693 HRESULT status = D3D_OK;
1694
1695 // TODO
1696 UNIMPLEMENTED();
1697
1698 switch (status)
1699 {
1700 case D3DERR_DEVICENOTRESET:
1701 case D3DERR_DEVICEHUNG:
1702 return true;
1703 default:
1704 return false;
1705 }
1706}
1707
1708bool Renderer11::resetDevice()
1709{
1710 releaseDeviceResources();
1711
1712 // TODO
1713 UNIMPLEMENTED();
1714
1715 // reset device defaults
1716 initializeDevice();
1717 mDeviceLost = false;
1718
1719 return true;
1720}
1721
1722DWORD Renderer11::getAdapterVendor() const
1723{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001724 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001725}
1726
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001727std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001728{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001729 std::ostringstream rendererString;
1730
1731 rendererString << mDescription;
1732 rendererString << " Direct3D11";
1733
1734 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1735 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1736
1737 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001738}
1739
1740GUID Renderer11::getAdapterIdentifier() const
1741{
1742 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001743 // UNIMPLEMENTED();
1744 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001745 return foo;
1746}
1747
1748bool Renderer11::getDXT1TextureSupport()
1749{
1750 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001751 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001752 return false;
1753}
1754
1755bool Renderer11::getDXT3TextureSupport()
1756{
1757 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001758 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001759 return false;
1760}
1761
1762bool Renderer11::getDXT5TextureSupport()
1763{
1764 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001765 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001766 return false;
1767}
1768
1769bool Renderer11::getDepthTextureSupport() const
1770{
1771 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001772 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001773 return false;
1774}
1775
1776bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1777{
1778 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001779 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001780
1781 *filtering = false;
1782 *renderable = false;
1783 return false;
1784}
1785
1786bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1787{
1788 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001789 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001790
1791 *filtering = false;
1792 *renderable = false;
1793 return false;
1794}
1795
1796bool Renderer11::getLuminanceTextureSupport()
1797{
1798 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001799 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001800 return false;
1801}
1802
1803bool Renderer11::getLuminanceAlphaTextureSupport()
1804{
1805 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001806 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001807 return false;
1808}
1809
1810bool Renderer11::getTextureFilterAnisotropySupport() const
1811{
1812 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001813 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001814 return false;
1815}
1816
1817float Renderer11::getTextureMaxAnisotropy() const
1818{
1819 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001820 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001821 return 1.0f;
1822}
1823
1824bool Renderer11::getEventQuerySupport()
1825{
1826 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001827 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001828 return false;
1829}
1830
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001831unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001832{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001833 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1834 switch (mFeatureLevel)
1835 {
1836 case D3D_FEATURE_LEVEL_11_0:
1837 case D3D_FEATURE_LEVEL_10_1:
1838 case D3D_FEATURE_LEVEL_10_0:
1839 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1840 default: UNREACHABLE();
1841 return 0;
1842 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001843}
1844
1845bool Renderer11::getNonPower2TextureSupport() const
1846{
1847 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001848 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001849 return false;
1850}
1851
1852bool Renderer11::getOcclusionQuerySupport() const
1853{
1854 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001855 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856 return false;
1857}
1858
1859bool Renderer11::getInstancingSupport() const
1860{
1861 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001862 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863 return false;
1864}
1865
1866bool Renderer11::getShareHandleSupport() const
1867{
1868 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001869 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001870
1871 // PIX doesn't seem to support using share handles, so disable them.
1872 return false && !gl::perfActive();
1873}
1874
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001875bool Renderer11::getDerivativeInstructionSupport() const
1876{
1877 // TODO
1878 // UNIMPLEMENTED();
1879 return false;
1880}
1881
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001882int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001883{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001884 switch (mFeatureLevel)
1885 {
1886 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001887 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001888 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1889 default: UNREACHABLE(); return 0;
1890 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001891}
1892
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001893int Renderer11::getMinorShaderModel() const
1894{
1895 switch (mFeatureLevel)
1896 {
1897 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1898 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1899 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1900 default: UNREACHABLE(); return 0;
1901 }
1902}
1903
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904float Renderer11::getMaxPointSize() const
1905{
1906 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001907 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908 return 1.0f;
1909}
1910
1911int Renderer11::getMaxTextureWidth() const
1912{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001913 switch (mFeatureLevel)
1914 {
1915 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1916 case D3D_FEATURE_LEVEL_10_1:
1917 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1918 default: UNREACHABLE(); return 0;
1919 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001920}
1921
1922int Renderer11::getMaxTextureHeight() const
1923{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001924 switch (mFeatureLevel)
1925 {
1926 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1927 case D3D_FEATURE_LEVEL_10_1:
1928 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1929 default: UNREACHABLE(); return 0;
1930 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001931}
1932
1933bool Renderer11::get32BitIndexSupport() const
1934{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001935 switch (mFeatureLevel)
1936 {
1937 case D3D_FEATURE_LEVEL_11_0:
1938 case D3D_FEATURE_LEVEL_10_1:
1939 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1940 default: UNREACHABLE(); return false;
1941 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001942}
1943
1944int Renderer11::getMinSwapInterval() const
1945{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001946 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001947}
1948
1949int Renderer11::getMaxSwapInterval() const
1950{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001951 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001952}
1953
1954int Renderer11::getMaxSupportedSamples() const
1955{
1956 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001957 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001958 return 1;
1959}
1960
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001961bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001962{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001963 if (source && dest)
1964 {
1965 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1966 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1967
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001968 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001969 return true;
1970 }
1971
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001972 return false;
1973}
1974
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001975bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001976{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001977 if (source && dest)
1978 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001979 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1980 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001981
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001982 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001983 return true;
1984 }
1985
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001986 return false;
1987}
1988
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00001989bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001990 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001991{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001992 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1993 if (!colorbuffer)
1994 {
1995 ERR("Failed to retrieve the color buffer from the frame buffer.");
1996 return error(GL_OUT_OF_MEMORY, false);
1997 }
1998
1999 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2000 if (!sourceRenderTarget)
2001 {
2002 ERR("Failed to retrieve the render target from the frame buffer.");
2003 return error(GL_OUT_OF_MEMORY, false);
2004 }
2005
2006 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2007 if (!source)
2008 {
2009 ERR("Failed to retrieve the render target view from the render target.");
2010 return error(GL_OUT_OF_MEMORY, false);
2011 }
2012
2013 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2014 if (!storage11)
2015 {
2016 source->Release();
2017 ERR("Failed to retrieve the texture storage from the destination.");
2018 return error(GL_OUT_OF_MEMORY, false);
2019 }
2020
2021 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2022 if (!destRenderTarget)
2023 {
2024 source->Release();
2025 ERR("Failed to retrieve the render target from the destination storage.");
2026 return error(GL_OUT_OF_MEMORY, false);
2027 }
2028
2029 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2030 if (!dest)
2031 {
2032 source->Release();
2033 ERR("Failed to retrieve the render target view from the destination render target.");
2034 return error(GL_OUT_OF_MEMORY, false);
2035 }
2036
2037 gl::Rectangle destRect;
2038 destRect.x = xoffset;
2039 destRect.y = yoffset;
2040 destRect.width = sourceRect.width;
2041 destRect.height = sourceRect.height;
2042
2043 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2044 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2045
2046 source->Release();
2047 dest->Release();
2048
2049 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002050}
2051
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002052bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002053 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002054{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002055 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2056 if (!colorbuffer)
2057 {
2058 ERR("Failed to retrieve the color buffer from the frame buffer.");
2059 return error(GL_OUT_OF_MEMORY, false);
2060 }
2061
2062 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2063 if (!sourceRenderTarget)
2064 {
2065 ERR("Failed to retrieve the render target from the frame buffer.");
2066 return error(GL_OUT_OF_MEMORY, false);
2067 }
2068
2069 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2070 if (!source)
2071 {
2072 ERR("Failed to retrieve the render target view from the render target.");
2073 return error(GL_OUT_OF_MEMORY, false);
2074 }
2075
2076 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2077 if (!storage11)
2078 {
2079 source->Release();
2080 ERR("Failed to retrieve the texture storage from the destination.");
2081 return error(GL_OUT_OF_MEMORY, false);
2082 }
2083
2084 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2085 if (!destRenderTarget)
2086 {
2087 source->Release();
2088 ERR("Failed to retrieve the render target from the destination storage.");
2089 return error(GL_OUT_OF_MEMORY, false);
2090 }
2091
2092 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2093 if (!dest)
2094 {
2095 source->Release();
2096 ERR("Failed to retrieve the render target view from the destination render target.");
2097 return error(GL_OUT_OF_MEMORY, false);
2098 }
2099
2100 gl::Rectangle destRect;
2101 destRect.x = xoffset;
2102 destRect.y = yoffset;
2103 destRect.width = sourceRect.width;
2104 destRect.height = sourceRect.height;
2105
2106 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2107 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2108
2109 source->Release();
2110 dest->Release();
2111
2112 return ret;
2113}
2114
2115bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2116 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2117{
2118 HRESULT result;
2119
2120 if (!mCopyResourcesInitialized)
2121 {
2122 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2123
2124 D3D11_BUFFER_DESC vbDesc;
2125 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2126 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2127 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2128 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2129 vbDesc.MiscFlags = 0;
2130 vbDesc.StructureByteStride = 0;
2131
2132 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2133 ASSERT(SUCCEEDED(result));
2134 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2135
2136 D3D11_SAMPLER_DESC samplerDesc;
2137 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2138 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2139 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2140 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2141 samplerDesc.MipLODBias = 0.0f;
2142 samplerDesc.MaxAnisotropy = 0;
2143 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2144 samplerDesc.BorderColor[0] = 0.0f;
2145 samplerDesc.BorderColor[1] = 0.0f;
2146 samplerDesc.BorderColor[2] = 0.0f;
2147 samplerDesc.BorderColor[3] = 0.0f;
2148 samplerDesc.MinLOD = 0.0f;
2149 samplerDesc.MaxLOD = 0.0f;
2150
2151 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2152 ASSERT(SUCCEEDED(result));
2153 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2154
2155 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2156 {
2157 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2158 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2159 };
2160
2161 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2162 ASSERT(SUCCEEDED(result));
2163 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2164
2165 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2166 ASSERT(SUCCEEDED(result));
2167 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2168
2169 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2170 ASSERT(SUCCEEDED(result));
2171 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2172
2173 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2174 ASSERT(SUCCEEDED(result));
2175 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2176
2177 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2178 ASSERT(SUCCEEDED(result));
2179 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2180
2181 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2182 ASSERT(SUCCEEDED(result));
2183 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2184
2185 mCopyResourcesInitialized = true;
2186 }
2187
2188 // Verify the source and destination area sizes
2189 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2190 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2191 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2192 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2193 {
2194 return error(GL_INVALID_VALUE, false);
2195 }
2196
2197 // Set vertices
2198 D3D11_MAPPED_SUBRESOURCE mappedResource;
2199 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2200 if (FAILED(result))
2201 {
2202 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
2203 return error(GL_OUT_OF_MEMORY, false);
2204 }
2205
2206 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2207
2208 // Create a quad in homogeneous coordinates
2209 float x1 = (destArea.x / destWidth) * 2.0f - 1.0f;
2210 float y1 = (destArea.y / destHeight) * 2.0f - 1.0f;
2211 float x2 = ((destArea.x + destArea.width) / destWidth) * 2.0f - 1.0f;
2212 float y2 = ((destArea.y + destArea.height) / destHeight) * 2.0f - 1.0f;
2213
2214 float u1 = sourceArea.x / float(sourceWidth);
2215 float v1 = sourceArea.y / float(sourceHeight);
2216 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2217 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2218
2219 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2220 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2221 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2222 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2223
2224 mDeviceContext->Unmap(mCopyVB, 0);
2225
2226 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2227 static UINT startIdx = 0;
2228 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2229
2230 // Apply state
2231 static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
2232 mDeviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
2233 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2234 mDeviceContext->RSSetState(NULL);
2235
2236 // Apply shaders
2237 mDeviceContext->IASetInputLayout(mCopyIL);
2238 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2239 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2240
2241 ID3D11PixelShader *ps = NULL;
2242 switch(destFormat)
2243 {
2244 case GL_RGBA: ps = mCopyRGBAPS; break;
2245 case GL_RGB: ps = mCopyRGBPS; break;
2246 case GL_ALPHA: ps = mCopyRGBAPS; break;
2247 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2248 case GL_LUMINANCE: ps = mCopyLumPS; break;
2249 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2250 default: UNREACHABLE(); ps = NULL; break;
2251 }
2252
2253 mDeviceContext->PSSetShader(ps, NULL, 0);
2254
2255 // Unset the currently bound shader resource to avoid conflicts
2256 static ID3D11ShaderResourceView *const nullSRV = NULL;
2257 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2258
2259 // Apply render targets
2260 mDeviceContext->OMSetRenderTargets(1, &dest, NULL);
2261
2262 // Set the viewport
2263 D3D11_VIEWPORT viewport;
2264 viewport.TopLeftX = 0;
2265 viewport.TopLeftY = 0;
2266 viewport.Width = destWidth;
2267 viewport.Height = destHeight;
2268 viewport.MinDepth = 0.0f;
2269 viewport.MaxDepth = 1.0f;
2270 mDeviceContext->RSSetViewports(1, &viewport);
2271
2272 // Apply textures
2273 mDeviceContext->PSSetShaderResources(0, 1, &source);
2274 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2275
2276 // Draw the quad
2277 mDeviceContext->Draw(4, 0);
2278
2279 // Unbind textures and render targets and vertex buffer
2280 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2281
2282 static ID3D11RenderTargetView *const nullRTV = NULL;
2283 mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL);
2284
2285 static UINT zero = 0;
2286 static ID3D11Buffer *const nullBuffer = NULL;
2287 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2288
2289 markAllStateDirty();
2290
2291 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002292}
2293
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002294RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2295{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002296 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002297 RenderTarget11 *renderTarget = NULL;
2298 if (depth)
2299 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002300 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL,
2301 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002302 }
2303 else
2304 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002305 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
2306 swapChain11->getRenderTargetShaderResource(),
2307 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002308 }
2309 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002310}
2311
2312RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2313{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002314 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2315 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002316}
2317
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002318ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002319{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002320 ShaderExecutable11 *executable = NULL;
2321
2322 switch (type)
2323 {
2324 case GL_VERTEX_SHADER:
2325 {
2326 ID3D11VertexShader *vshader = NULL;
2327 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2328 ASSERT(SUCCEEDED(result));
2329
2330 if (vshader)
2331 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002332 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002333 }
2334 }
2335 break;
2336 case GL_FRAGMENT_SHADER:
2337 {
2338 ID3D11PixelShader *pshader = NULL;
2339 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2340 ASSERT(SUCCEEDED(result));
2341
2342 if (pshader)
2343 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002344 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002345 }
2346 }
2347 break;
2348 default:
2349 UNREACHABLE();
2350 break;
2351 }
2352
2353 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002354}
2355
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002356ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2357{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002358 const char *profile = NULL;
2359
2360 switch (type)
2361 {
2362 case GL_VERTEX_SHADER:
2363 profile = "vs_4_0";
2364 break;
2365 case GL_FRAGMENT_SHADER:
2366 profile = "ps_4_0";
2367 break;
2368 default:
2369 UNREACHABLE();
2370 return NULL;
2371 }
2372
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002373 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002374 if (!binary)
2375 return NULL;
2376
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002377 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002378 binary->Release();
2379
2380 return executable;
2381}
2382
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002383VertexBuffer *Renderer11::createVertexBuffer()
2384{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002385 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002386}
2387
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002388IndexBuffer *Renderer11::createIndexBuffer()
2389{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002390 return new IndexBuffer11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002391}
2392
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002393bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
2394{
2395 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2396 if (colorbuffer)
2397 {
2398 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2399 if (renderTarget)
2400 {
2401 *subresourceIndex = renderTarget->getSubresourceIndex();
2402
2403 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2404 if (colorBufferRTV)
2405 {
2406 ID3D11Resource *textureResource = NULL;
2407 colorBufferRTV->GetResource(&textureResource);
2408 colorBufferRTV->Release();
2409
2410 if (textureResource)
2411 {
2412 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
2413 textureResource->Release();
2414
2415 if (SUCCEEDED(result))
2416 {
2417 return true;
2418 }
2419 else
2420 {
2421 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2422 "HRESULT: 0x%X.", result);
2423 }
2424 }
2425 }
2426 }
2427 }
2428
2429 return false;
2430}
2431
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002432bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
2433 bool blitRenderTarget, bool blitDepthStencil)
2434{
2435 // TODO
2436 UNIMPLEMENTED();
2437 return false;
2438}
2439
2440void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2441 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2442{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002443 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002444 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002445
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002446 if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002447 {
2448 gl::Rectangle area;
2449 area.x = x;
2450 area.y = y;
2451 area.width = width;
2452 area.height = height;
2453
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002454 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2455 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002456
2457 colorBufferTexture->Release();
2458 colorBufferTexture = NULL;
2459 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002460}
2461
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002462Image *Renderer11::createImage()
2463{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002464 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002465}
2466
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002467void Renderer11::generateMipmap(Image *dest, Image *src)
2468{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002469 Image11 *dest11 = Image11::makeImage11(dest);
2470 Image11 *src11 = Image11::makeImage11(src);
2471 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002472}
2473
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002474TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2475{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002476 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2477 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002478}
2479
2480TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2481{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002482 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002483}
2484
2485TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2486{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002487 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002488}
2489
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002490static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
2491{
2492 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
2493 destFormat == GL_ALPHA &&
2494 destType == GL_UNSIGNED_BYTE)
2495 {
2496 return 1;
2497 }
2498 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
2499 destFormat == GL_RGBA &&
2500 destType == GL_UNSIGNED_BYTE)
2501 {
2502 return 4;
2503 }
2504 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
2505 destFormat == GL_BGRA_EXT &&
2506 destType == GL_UNSIGNED_BYTE)
2507 {
2508 return 4;
2509 }
2510 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
2511 destFormat == GL_RGBA &&
2512 destType == GL_HALF_FLOAT_OES)
2513 {
2514 return 8;
2515 }
2516 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
2517 destFormat == GL_RGB &&
2518 destType == GL_FLOAT)
2519 {
2520 return 12;
2521 }
2522 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
2523 destFormat == GL_RGBA &&
2524 destType == GL_FLOAT)
2525 {
2526 return 16;
2527 }
2528 else
2529 {
2530 return 0;
2531 }
2532}
2533
2534static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
2535 unsigned int y, int inputPitch, gl::Color *outColor)
2536{
2537 switch (format)
2538 {
2539 case DXGI_FORMAT_R8G8B8A8_UNORM:
2540 {
2541 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2542 outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
2543 outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
2544 outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
2545 outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
2546 }
2547 break;
2548
2549 case DXGI_FORMAT_A8_UNORM:
2550 {
2551 outColor->red = 0.0f;
2552 outColor->green = 0.0f;
2553 outColor->blue = 0.0f;
2554 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
2555 }
2556 break;
2557
2558 case DXGI_FORMAT_R32G32B32A32_FLOAT:
2559 {
2560 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
2561 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
2562 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
2563 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
2564 }
2565 break;
2566
2567 case DXGI_FORMAT_R32G32B32_FLOAT:
2568 {
2569 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
2570 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
2571 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
2572 outColor->alpha = 1.0f;
2573 }
2574 break;
2575
2576 case DXGI_FORMAT_R16G16B16A16_FLOAT:
2577 {
2578 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
2579 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
2580 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
2581 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
2582 }
2583 break;
2584
2585 case DXGI_FORMAT_B8G8R8A8_UNORM:
2586 {
2587 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2588 outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
2589 outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
2590 outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
2591 outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
2592 }
2593 break;
2594
2595 case DXGI_FORMAT_R8_UNORM:
2596 {
2597 outColor->red = *(data + x + y * inputPitch) / 255.0f;
2598 outColor->green = 0.0f;
2599 outColor->blue = 0.0f;
2600 outColor->alpha = 1.0f;
2601 }
2602 break;
2603
2604 case DXGI_FORMAT_R8G8_UNORM:
2605 {
2606 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
2607
2608 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
2609 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
2610 outColor->blue = 0.0f;
2611 outColor->alpha = 1.0f;
2612 }
2613 break;
2614
2615 case DXGI_FORMAT_R16_FLOAT:
2616 {
2617 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
2618 outColor->green = 0.0f;
2619 outColor->blue = 0.0f;
2620 outColor->alpha = 1.0f;
2621 }
2622 break;
2623
2624 case DXGI_FORMAT_R16G16_FLOAT:
2625 {
2626 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
2627 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
2628 outColor->blue = 0.0f;
2629 outColor->alpha = 1.0f;
2630 }
2631 break;
2632
2633 default:
2634 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
2635 UNIMPLEMENTED();
2636 break;
2637 }
2638}
2639
2640static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
2641 unsigned int y, int outputPitch, void *outData)
2642{
2643 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
2644 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
2645
2646 switch (format)
2647 {
2648 case GL_RGBA:
2649 switch (type)
2650 {
2651 case GL_UNSIGNED_BYTE:
2652 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2653 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2654 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2655 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2656 break;
2657
2658 default:
2659 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
2660 UNIMPLEMENTED();
2661 break;
2662 }
2663 break;
2664
2665 case GL_BGRA_EXT:
2666 switch (type)
2667 {
2668 case GL_UNSIGNED_BYTE:
2669 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2670 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2671 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
2672 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2673 break;
2674
2675 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2676 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2677 // this type is packed as follows:
2678 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2679 // --------------------------------------------------------------------------------
2680 // | 4th | 3rd | 2nd | 1st component |
2681 // --------------------------------------------------------------------------------
2682 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2683 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2684 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
2685 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
2686 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
2687 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
2688 break;
2689
2690 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2691 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2692 // this type is packed as follows:
2693 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2694 // --------------------------------------------------------------------------------
2695 // | 4th | 3rd | 2nd | 1st component |
2696 // --------------------------------------------------------------------------------
2697 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2698 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2699 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
2700 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
2701 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
2702 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
2703 break;
2704
2705 default:
2706 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
2707 UNIMPLEMENTED();
2708 break;
2709 }
2710 break;
2711
2712 case GL_RGB:
2713 switch (type)
2714 {
2715 case GL_UNSIGNED_SHORT_5_6_5:
2716 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2717 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
2718 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
2719 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
2720 break;
2721
2722 case GL_UNSIGNED_BYTE:
2723 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2724 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2725 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2726 break;
2727
2728 default:
2729 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
2730 UNIMPLEMENTED();
2731 break;
2732 }
2733 break;
2734
2735 default:
2736 ERR("WritePixelColor not implemented for format 0x%X.", format);
2737 UNIMPLEMENTED();
2738 break;
2739 }
2740}
2741
2742void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2743 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2744 GLint packAlignment, void *pixels)
2745{
2746 D3D11_TEXTURE2D_DESC textureDesc;
2747 texture->GetDesc(&textureDesc);
2748
2749 D3D11_TEXTURE2D_DESC stagingDesc;
2750 stagingDesc.Width = area.width;
2751 stagingDesc.Height = area.height;
2752 stagingDesc.MipLevels = 1;
2753 stagingDesc.ArraySize = 1;
2754 stagingDesc.Format = textureDesc.Format;
2755 stagingDesc.SampleDesc.Count = 1;
2756 stagingDesc.SampleDesc.Quality = 0;
2757 stagingDesc.Usage = D3D11_USAGE_STAGING;
2758 stagingDesc.BindFlags = 0;
2759 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2760 stagingDesc.MiscFlags = 0;
2761
2762 ID3D11Texture2D* stagingTex = NULL;
2763 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2764 if (FAILED(result))
2765 {
2766 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2767 return;
2768 }
2769
2770 ID3D11Texture2D* srcTex = NULL;
2771 if (textureDesc.SampleDesc.Count > 1)
2772 {
2773 D3D11_TEXTURE2D_DESC resolveDesc;
2774 resolveDesc.Width = textureDesc.Width;
2775 resolveDesc.Height = textureDesc.Height;
2776 resolveDesc.MipLevels = 1;
2777 resolveDesc.ArraySize = 1;
2778 resolveDesc.Format = textureDesc.Format;
2779 resolveDesc.SampleDesc.Count = 1;
2780 resolveDesc.SampleDesc.Quality = 0;
2781 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2782 resolveDesc.BindFlags = 0;
2783 resolveDesc.CPUAccessFlags = 0;
2784 resolveDesc.MiscFlags = 0;
2785
2786 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2787 if (FAILED(result))
2788 {
2789 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
2790 stagingTex->Release();
2791 return;
2792 }
2793
2794 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2795 subResource = 0;
2796 }
2797 else
2798 {
2799 srcTex = texture;
2800 srcTex->AddRef();
2801 }
2802
2803 D3D11_BOX srcBox;
2804 srcBox.left = area.x;
2805 srcBox.right = area.x + area.width;
2806 srcBox.top = area.y;
2807 srcBox.bottom = area.y + area.height;
2808 srcBox.front = 0;
2809 srcBox.back = 1;
2810
2811 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2812
2813 srcTex->Release();
2814 srcTex = NULL;
2815
2816 D3D11_MAPPED_SUBRESOURCE mapping;
2817 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
2818
2819 unsigned char *source;
2820 int inputPitch;
2821 if (packReverseRowOrder)
2822 {
2823 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
2824 inputPitch = -static_cast<int>(mapping.RowPitch);
2825 }
2826 else
2827 {
2828 source = static_cast<unsigned char*>(mapping.pData);
2829 inputPitch = static_cast<int>(mapping.RowPitch);
2830 }
2831
2832 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
2833 if (fastPixelSize != 0)
2834 {
2835 unsigned char *dest = static_cast<unsigned char*>(pixels);
2836 for (int j = 0; j < area.height; j++)
2837 {
2838 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
2839 }
2840 }
2841 else
2842 {
2843 gl::Color pixelColor;
2844 for (int j = 0; j < area.height; j++)
2845 {
2846 for (int i = 0; i < area.width; i++)
2847 {
2848 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
2849 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
2850 }
2851 }
2852 }
2853
2854 mDeviceContext->Unmap(stagingTex, 0);
2855
2856 stagingTex->Release();
2857 stagingTex = NULL;
2858}
2859
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002860}