blob: 0f350baaca32bc6691c357d844a770a8d06359de [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
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000035#include <sstream>
36
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000037namespace rx
38{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000039static const DXGI_FORMAT RenderTargetFormats[] =
40 {
41 DXGI_FORMAT_R8G8B8A8_UNORM
42 };
43
44static const DXGI_FORMAT DepthStencilFormats[] =
45 {
46 DXGI_FORMAT_D24_UNORM_S8_UINT
47 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000049enum
50{
51 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
52};
53
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000054Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
55{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000056 mVertexDataManager = NULL;
57 mIndexDataManager = NULL;
58
daniel@transgaming.comc5114302012-12-20 21:11:36 +000059 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000060 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000061
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000062 mCopyResourcesInitialized = false;
63 mCopyVB = NULL;
64 mCopySampler = NULL;
65 mCopyIL = NULL;
66 mCopyVS = NULL;
67 mCopyRGBAPS = NULL;
68 mCopyRGBPS = NULL;
69 mCopyLumPS = NULL;
70 mCopyLumAlphaPS = NULL;
71
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000072 mD3d11Module = NULL;
73 mDxgiModule = NULL;
74
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000075 mDeviceLost = false;
76
daniel@transgaming.com25072f62012-11-28 19:31:32 +000077 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000078 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000079 mDxgiAdapter = NULL;
80 mDxgiFactory = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000081}
82
83Renderer11::~Renderer11()
84{
85 releaseDeviceResources();
86
daniel@transgaming.com65e65372012-11-28 19:33:50 +000087 if (mDxgiFactory)
88 {
89 mDxgiFactory->Release();
90 mDxgiFactory = NULL;
91 }
92
93 if (mDxgiAdapter)
94 {
95 mDxgiAdapter->Release();
96 mDxgiAdapter = NULL;
97 }
98
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000099 if (mDeviceContext)
100 {
daniel@transgaming.comd5df4e82013-01-11 04:11:21 +0000101 mDeviceContext->ClearState();
102 mDeviceContext->Flush();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000103 mDeviceContext->Release();
104 mDeviceContext = NULL;
105 }
106
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000107 if (mDevice)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000108 {
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000109 mDevice->Release();
110 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000111 }
112
113 if (mD3d11Module)
114 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000115 FreeLibrary(mD3d11Module);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000116 mD3d11Module = NULL;
117 }
118
119 if (mDxgiModule)
120 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000121 FreeLibrary(mDxgiModule);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000122 mDxgiModule = NULL;
123 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000124}
125
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000126Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
127{
128 ASSERT(dynamic_cast<rx::Renderer11*>(renderer) != NULL);
129 return static_cast<rx::Renderer11*>(renderer);
130}
131
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000132EGLint Renderer11::initialize()
133{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000134 if (!initializeCompiler())
135 {
136 return EGL_NOT_INITIALIZED;
137 }
138
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
140 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000141
142 if (mD3d11Module == NULL || mDxgiModule == NULL)
143 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000144 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000145 return EGL_NOT_INITIALIZED;
146 }
147
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000148 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000149
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000150 if (D3D11CreateDevice == NULL)
151 {
152 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
153 return EGL_NOT_INITIALIZED;
154 }
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000155
156 D3D_FEATURE_LEVEL featureLevel[] =
157 {
158 D3D_FEATURE_LEVEL_11_0,
159 D3D_FEATURE_LEVEL_10_1,
160 D3D_FEATURE_LEVEL_10_0,
161 };
162
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000163 HRESULT result = D3D11CreateDevice(NULL,
164 D3D_DRIVER_TYPE_HARDWARE,
165 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000166 #if defined(_DEBUG)
167 D3D11_CREATE_DEVICE_DEBUG,
168 #else
169 0,
170 #endif
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000171 featureLevel,
172 sizeof(featureLevel)/sizeof(featureLevel[0]),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000173 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000174 &mDevice,
175 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000176 &mDeviceContext);
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000177
178 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000179 {
180 ERR("Could not create D3D11 device - aborting!\n");
181 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
182 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000183
184 IDXGIDevice *dxgiDevice = NULL;
185 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
186
187 if (FAILED(result))
188 {
189 ERR("Could not query DXGI device - aborting!\n");
190 return EGL_NOT_INITIALIZED;
191 }
192
193 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
194
195 if (FAILED(result))
196 {
197 ERR("Could not retrieve DXGI adapter - aborting!\n");
198 return EGL_NOT_INITIALIZED;
199 }
200
201 dxgiDevice->Release();
202
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000203 mDxgiAdapter->GetDesc(&mAdapterDescription);
204 memset(mDescription, 0, sizeof(mDescription));
205 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
206
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000207 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
208
209 if (!mDxgiFactory || FAILED(result))
210 {
211 ERR("Could not create DXGI factory - aborting!\n");
212 return EGL_NOT_INITIALIZED;
213 }
214
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000215 initializeDevice();
216
217 return EGL_SUCCESS;
218}
219
220// do any one-time device initialization
221// NOTE: this is also needed after a device lost/reset
222// to reset the scene status and ensure the default states are reset.
223void Renderer11::initializeDevice()
224{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000225 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000226 mInputLayoutCache.initialize(mDevice, mDeviceContext);
227
228 ASSERT(!mVertexDataManager && !mIndexDataManager);
229 mVertexDataManager = new VertexDataManager(this);
230 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000231
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000232 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000233}
234
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000235int Renderer11::generateConfigs(ConfigDesc **configDescList)
236{
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000237 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
238 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000239 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
240 int numConfigs = 0;
241
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000242 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000243 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000244 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000245 {
246 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
247
248 UINT formatSupport = 0;
249 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
250
251 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
252 {
253 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
254
255 UINT formatSupport = 0;
256 HRESULT result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
257
258 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL))
259 {
260 ConfigDesc newConfig;
261 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
262 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
263 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
264 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
265
266 (*configDescList)[numConfigs++] = newConfig;
267 }
268 }
269 }
270 }
271
272 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000273}
274
275void Renderer11::deleteConfigs(ConfigDesc *configDescList)
276{
277 delete [] (configDescList);
278}
279
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000280void Renderer11::sync(bool block)
281{
282 // TODO
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000283 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000284}
285
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000286SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
287{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000288 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000289}
290
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000291void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
292{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000293 if (type == gl::SAMPLER_PIXEL)
294 {
295 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
296 {
297 ERR("Pixel shader sampler index %i is not valid.", index);
298 return;
299 }
300
301 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
302 {
303 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
304
305 if (!dxSamplerState)
306 {
307 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
308 "sampler state for pixel shaders at slot %i.", index);
309 }
310
311 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
312
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000313 mCurPixelSamplerStates[index] = samplerState;
314 }
315
316 mForceSetPixelSamplerStates[index] = false;
317 }
318 else if (type == gl::SAMPLER_VERTEX)
319 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000320 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000321 {
322 ERR("Vertex shader sampler index %i is not valid.", index);
323 return;
324 }
325
326 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
327 {
328 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
329
330 if (!dxSamplerState)
331 {
332 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
333 "sampler state for vertex shaders at slot %i.", index);
334 }
335
336 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
337
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000338 mCurVertexSamplerStates[index] = samplerState;
339 }
340
341 mForceSetVertexSamplerStates[index] = false;
342 }
343 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000344}
345
346void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
347{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000348 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000349 unsigned int serial = 0;
350 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000351
352 if (texture)
353 {
354 TextureStorageInterface *texStorage = texture->getNativeTexture();
355 if (texStorage)
356 {
357 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
358 textureSRV = storage11->getSRV();
359 }
360
361 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
362 // missing the shader resource view
363 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000364
365 serial = texture->getTextureSerial();
366 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000367 }
368
369 if (type == gl::SAMPLER_PIXEL)
370 {
371 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
372 {
373 ERR("Pixel shader sampler index %i is not valid.", index);
374 return;
375 }
376
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000377 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
378 {
379 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
380 }
381
382 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000383 }
384 else if (type == gl::SAMPLER_VERTEX)
385 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000386 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000387 {
388 ERR("Vertex shader sampler index %i is not valid.", index);
389 return;
390 }
391
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000392 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
393 {
394 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
395 }
396
397 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000398 }
399 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000400}
401
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000402void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000403{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000404 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000405 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000406 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
407 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000408 if (!dxRasterState)
409 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000410 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000411 "rasterizer state.");
412 }
413
414 mDeviceContext->RSSetState(dxRasterState);
415
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000416 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000417 }
418
419 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000420}
421
422void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
423 unsigned int sampleMask)
424{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000425 if (mForceSetBlendState ||
426 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
427 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
428 sampleMask != mCurSampleMask)
429 {
430 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
431 if (!dxBlendState)
432 {
433 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
434 "blend state.");
435 }
436
437 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
438 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
439
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000440 mCurBlendState = blendState;
441 mCurBlendColor = blendColor;
442 mCurSampleMask = sampleMask;
443 }
444
445 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000446}
447
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000448void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000449 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000450{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000451 if (mForceSetDepthStencilState ||
452 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
453 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
454 {
455 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
456 stencilRef != stencilBackRef ||
457 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
458 {
459 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
460 "invalid under WebGL.");
461 return error(GL_INVALID_OPERATION);
462 }
463
464 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
465 if (!dxDepthStencilState)
466 {
467 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
468 "setting the default depth stencil state.");
469 }
470
471 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
472
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000473 mCurDepthStencilState = depthStencilState;
474 mCurStencilRef = stencilRef;
475 mCurStencilBackRef = stencilBackRef;
476 }
477
478 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000479}
480
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000481void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000482{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000483 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
484 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000485 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000486 if (enabled)
487 {
488 D3D11_RECT rect;
489 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
490 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
491 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
492 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000493
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000494 mDeviceContext->RSSetScissorRects(1, &rect);
495 }
496
497 if (enabled != mScissorEnabled)
498 {
499 mForceSetRasterState = true;
500 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000501
502 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000503 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000504 }
505
506 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000507}
508
daniel@transgaming.com12985182012-12-20 20:56:31 +0000509bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000510 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000511{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000512 gl::Rectangle actualViewport = viewport;
513 float actualZNear = gl::clamp01(zNear);
514 float actualZFar = gl::clamp01(zFar);
515 if (ignoreViewport)
516 {
517 actualViewport.x = 0;
518 actualViewport.y = 0;
519 actualViewport.width = mRenderTargetDesc.width;
520 actualViewport.height = mRenderTargetDesc.height;
521 actualZNear = 0.0f;
522 actualZFar = 1.0f;
523 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000524
525 D3D11_VIEWPORT dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000526 dxViewport.TopLeftX = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
527 dxViewport.TopLeftY = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
528 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.TopLeftX));
529 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.TopLeftY));
530 dxViewport.MinDepth = actualZNear;
531 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000532
533 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
534 {
535 return false; // Nothing to render
536 }
537
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000538 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
539 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000540
daniel@transgaming.com53670042012-11-28 20:55:51 +0000541 if (viewportChanged)
542 {
543 mDeviceContext->RSSetViewports(1, &dxViewport);
544
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000545 mCurViewport = actualViewport;
546 mCurNear = actualZNear;
547 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000548 }
549
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000550 if (currentProgram)
daniel@transgaming.com53670042012-11-28 20:55:51 +0000551 {
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000552 dx_VertexConstants vc = {0};
553 dx_PixelConstants pc = {0};
daniel@transgaming.com53670042012-11-28 20:55:51 +0000554
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000555 vc.halfPixelSize[0] = 0.0f;
556 vc.halfPixelSize[1] = 0.0f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000557
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000558 pc.coord[0] = actualViewport.width * 0.5f;
559 pc.coord[1] = actualViewport.height * 0.5f;
560 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
561 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000562
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000563 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
564 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
565 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
566
567 vc.depthRange[0] = actualZNear;
568 vc.depthRange[1] = actualZFar;
569 vc.depthRange[2] = actualZFar - actualZNear;
570
571 pc.depthRange[0] = actualZNear;
572 pc.depthRange[1] = actualZFar;
573 pc.depthRange[2] = actualZFar - actualZNear;
574
575 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
576 {
577 mVertexConstants = vc;
578 mDxUniformsDirty = true;
579 }
580
581 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
582 {
583 mPixelConstants = pc;
584 mDxUniformsDirty = true;
585 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000586 }
587
588 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000589 return true;
590}
591
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000592bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
593{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000594 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000595
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000596 switch (mode)
597 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000598 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
599 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000600 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000601 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
602 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
603 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000604 // emulate fans via rewriting index buffer
605 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000606 default:
607 return error(GL_INVALID_ENUM, false);
608 }
609
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000610 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000611
612 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000613}
614
615bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000616{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000617 // Get the color render buffer and serial
618 gl::Renderbuffer *renderbufferObject = NULL;
619 unsigned int renderTargetSerial = 0;
620 if (framebuffer->getColorbufferType() != GL_NONE)
621 {
622 renderbufferObject = framebuffer->getColorbuffer();
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000623
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000624 if (!renderbufferObject)
625 {
626 ERR("render target pointer unexpectedly null.");
daniel@transgaming.come9c71b42012-11-28 21:02:23 +0000627 return false;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000628 }
629
630 renderTargetSerial = renderbufferObject->getSerial();
631 }
632
633 // Get the depth stencil render buffer and serials
634 gl::Renderbuffer *depthStencil = NULL;
635 unsigned int depthbufferSerial = 0;
636 unsigned int stencilbufferSerial = 0;
637 if (framebuffer->getDepthbufferType() != GL_NONE)
638 {
639 depthStencil = framebuffer->getDepthbuffer();
640 if (!depthStencil)
641 {
642 ERR("Depth stencil pointer unexpectedly null.");
643 return false;
644 }
645
646 depthbufferSerial = depthStencil->getSerial();
647 }
648 else if (framebuffer->getStencilbufferType() != GL_NONE)
649 {
650 depthStencil = framebuffer->getStencilbuffer();
651 if (!depthStencil)
652 {
653 ERR("Depth stencil pointer unexpectedly null.");
654 return false;
655 }
656
657 stencilbufferSerial = depthStencil->getSerial();
658 }
659
660 // Extract the render target dimensions and view
661 unsigned int renderTargetWidth = 0;
662 unsigned int renderTargetHeight = 0;
663 GLenum renderTargetFormat = 0;
664 ID3D11RenderTargetView* framebufferRTV = NULL;
665 if (renderbufferObject)
666 {
667 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
668 if (!renderTarget)
669 {
670 ERR("render target pointer unexpectedly null.");
671 return false;
672 }
673
674 framebufferRTV = renderTarget->getRenderTargetView();
675 if (!framebufferRTV)
676 {
677 ERR("render target view pointer unexpectedly null.");
678 return false;
679 }
680
681 renderTargetWidth = renderbufferObject->getWidth();
682 renderTargetHeight = renderbufferObject->getHeight();
683 renderTargetFormat = renderbufferObject->getActualFormat();
684 }
685
686 // Extract the depth stencil sizes and view
687 unsigned int depthSize = 0;
688 unsigned int stencilSize = 0;
689 ID3D11DepthStencilView* framebufferDSV = NULL;
690 if (depthStencil)
691 {
692 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
693 if (!depthStencilRenderTarget)
694 {
695 ERR("render target pointer unexpectedly null.");
696 if (framebufferRTV)
697 {
698 framebufferRTV->Release();
699 }
700 return false;
701 }
702
703 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
704 if (!framebufferDSV)
705 {
706 ERR("depth stencil view pointer unexpectedly null.");
707 if (framebufferRTV)
708 {
709 framebufferRTV->Release();
710 }
711 return false;
712 }
713
714 // If there is no render buffer, the width, height and format values come from
715 // the depth stencil
716 if (!renderbufferObject)
717 {
718 renderTargetWidth = depthStencil->getWidth();
719 renderTargetHeight = depthStencil->getHeight();
720 renderTargetFormat = depthStencil->getActualFormat();
721 }
722
723 depthSize = depthStencil->getDepthSize();
724 stencilSize = depthStencil->getStencilSize();
725 }
726
727 // Apply the render target and depth stencil
728 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
729 renderTargetSerial != mAppliedRenderTargetSerial ||
730 depthbufferSerial != mAppliedDepthbufferSerial ||
731 stencilbufferSerial != mAppliedStencilbufferSerial)
732 {
733 mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV);
734
735 mRenderTargetDesc.width = renderTargetWidth;
736 mRenderTargetDesc.height = renderTargetHeight;
737 mRenderTargetDesc.format = renderTargetFormat;
738 mForceSetViewport = true; // TODO: It may not be required to clamp the viewport in D3D11
739 mForceSetScissor = true; // TODO: It may not be required to clamp the scissor in D3D11
740
741 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
742 {
743 mCurDepthSize = depthSize;
744 mForceSetRasterState = true;
745 }
746
747 mCurStencilSize = stencilSize;
748
749 mAppliedRenderTargetSerial = renderTargetSerial;
750 mAppliedDepthbufferSerial = depthbufferSerial;
751 mAppliedStencilbufferSerial = stencilbufferSerial;
752 mRenderTargetDescInitialized = true;
753 mDepthStencilInitialized = true;
754 }
755
756 if (framebufferRTV)
757 {
758 framebufferRTV->Release();
759 }
760 if (framebufferDSV)
761 {
762 framebufferDSV->Release();
763 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000764
765 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000766}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000767
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000768GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000769{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000770 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
771 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
772 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000773 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000774 return err;
775 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000776
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000777 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000778}
779
daniel@transgaming.com31240482012-11-28 21:06:41 +0000780GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000781{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000782 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000783
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000784 if (err == GL_NO_ERROR)
785 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000786 if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000787 {
788 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
789
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000790 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000791 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000792 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000793 }
794 }
795
796 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000797}
798
799void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
800{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000801 if (mode == GL_LINE_LOOP)
802 {
803 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
804 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000805 else if (mode == GL_TRIANGLE_FAN)
806 {
807 drawTriangleFan(count, GL_NONE, NULL, 0, NULL);
808 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000809 else if (instances > 0)
810 {
811 // TODO
812 UNIMPLEMENTED();
813 }
814 else
815 {
816 mDeviceContext->Draw(count, 0);
817 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000818}
819
daniel@transgaming.com31240482012-11-28 21:06:41 +0000820void 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 +0000821{
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000822 if (mode == GL_LINE_LOOP)
823 {
824 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
825 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000826 else if (mode == GL_TRIANGLE_FAN)
827 {
828 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
829 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000830 else
831 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +0000832 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000833 }
834}
835
836void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
837{
838 // Get the raw indices for an indexed draw
839 if (type != GL_NONE && elementArrayBuffer)
840 {
841 gl::Buffer *indexBuffer = elementArrayBuffer;
842 intptr_t offset = reinterpret_cast<intptr_t>(indices);
843 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
844 }
845
846 if (!mLineLoopIB)
847 {
848 mLineLoopIB = new StreamingIndexBufferInterface(this);
849 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
850 {
851 delete mLineLoopIB;
852 mLineLoopIB = NULL;
853
854 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
855 return error(GL_OUT_OF_MEMORY);
856 }
857 }
858
859 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
860 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
861 {
862 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
863 return error(GL_OUT_OF_MEMORY);
864 }
865
866 void* mappedMemory = NULL;
867 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
868 if (offset == -1 || mappedMemory == NULL)
869 {
870 ERR("Could not map index buffer for GL_LINE_LOOP.");
871 return error(GL_OUT_OF_MEMORY);
872 }
873
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000874 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000875 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000876
877 switch (type)
878 {
879 case GL_NONE: // Non-indexed draw
880 for (int i = 0; i < count; i++)
881 {
882 data[i] = i;
883 }
884 data[count] = 0;
885 break;
886 case GL_UNSIGNED_BYTE:
887 for (int i = 0; i < count; i++)
888 {
889 data[i] = static_cast<const GLubyte*>(indices)[i];
890 }
891 data[count] = static_cast<const GLubyte*>(indices)[0];
892 break;
893 case GL_UNSIGNED_SHORT:
894 for (int i = 0; i < count; i++)
895 {
896 data[i] = static_cast<const GLushort*>(indices)[i];
897 }
898 data[count] = static_cast<const GLushort*>(indices)[0];
899 break;
900 case GL_UNSIGNED_INT:
901 for (int i = 0; i < count; i++)
902 {
903 data[i] = static_cast<const GLuint*>(indices)[i];
904 }
905 data[count] = static_cast<const GLuint*>(indices)[0];
906 break;
907 default: UNREACHABLE();
908 }
909
910 if (!mLineLoopIB->unmapBuffer())
911 {
912 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
913 return error(GL_OUT_OF_MEMORY);
914 }
915
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000916 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000917 {
918 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
919
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000920 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000921 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000922 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000923 }
924
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000925 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000926}
927
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000928void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
929{
930 // Get the raw indices for an indexed draw
931 if (type != GL_NONE && elementArrayBuffer)
932 {
933 gl::Buffer *indexBuffer = elementArrayBuffer;
934 intptr_t offset = reinterpret_cast<intptr_t>(indices);
935 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
936 }
937
938 if (!mTriangleFanIB)
939 {
940 mTriangleFanIB = new StreamingIndexBufferInterface(this);
941 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
942 {
943 delete mTriangleFanIB;
944 mTriangleFanIB = NULL;
945
946 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
947 return error(GL_OUT_OF_MEMORY);
948 }
949 }
950
951 const int numTris = count - 2;
952 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
953 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
954 {
955 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
956 return error(GL_OUT_OF_MEMORY);
957 }
958
959 void* mappedMemory = NULL;
960 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
961 if (offset == -1 || mappedMemory == NULL)
962 {
963 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
964 return error(GL_OUT_OF_MEMORY);
965 }
966
967 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
968 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
969
970 switch (type)
971 {
972 case GL_NONE: // Non-indexed draw
973 for (int i = 0; i < numTris; i++)
974 {
975 data[i*3 + 0] = 0;
976 data[i*3 + 1] = i + 1;
977 data[i*3 + 2] = i + 2;
978 }
979 break;
980 case GL_UNSIGNED_BYTE:
981 for (int i = 0; i < numTris; i++)
982 {
983 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
984 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
985 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
986 }
987 break;
988 case GL_UNSIGNED_SHORT:
989 for (int i = 0; i < numTris; i++)
990 {
991 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
992 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
993 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
994 }
995 break;
996 case GL_UNSIGNED_INT:
997 for (int i = 0; i < numTris; i++)
998 {
999 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1000 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1001 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1002 }
1003 break;
1004 default: UNREACHABLE();
1005 }
1006
1007 if (!mTriangleFanIB->unmapBuffer())
1008 {
1009 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
1010 return error(GL_OUT_OF_MEMORY);
1011 }
1012
1013 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1014 {
1015 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1016
1017 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1018 mAppliedIBSerial = mTriangleFanIB->getSerial();
1019 mAppliedIBOffset = indexBufferOffset;
1020 }
1021
1022 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1023}
1024
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001025void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1026{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001027 unsigned int programBinarySerial = programBinary->getSerial();
1028 if (programBinarySerial != mAppliedProgramBinarySerial)
1029 {
1030 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1031 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001032
daniel@transgaming.come4991412012-12-20 20:55:34 +00001033 ID3D11VertexShader *vertexShader = NULL;
1034 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001035
daniel@transgaming.come4991412012-12-20 20:55:34 +00001036 ID3D11PixelShader *pixelShader = NULL;
1037 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001038
daniel@transgaming.come4991412012-12-20 20:55:34 +00001039 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1040 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1041 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001042 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001043
1044 mAppliedProgramBinarySerial = programBinarySerial;
1045 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001046}
1047
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001048void Renderer11::applyUniforms(const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001049{
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001050 D3D11_BUFFER_DESC constantBufferDescription = {0};
1051 constantBufferDescription.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1052 constantBufferDescription.Usage = D3D11_USAGE_DYNAMIC;
1053 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1054 constantBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1055 constantBufferDescription.MiscFlags = 0;
1056 constantBufferDescription.StructureByteStride = 0;
1057
1058 ID3D11Buffer *constantBufferVS = NULL;
1059 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferVS);
1060 ASSERT(SUCCEEDED(result));
1061
1062 ID3D11Buffer *constantBufferPS = NULL;
1063 result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferPS);
1064 ASSERT(SUCCEEDED(result));
1065
1066 D3D11_MAPPED_SUBRESOURCE mapVS = {0};
1067 result = mDeviceContext->Map(constantBufferVS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS);
1068 ASSERT(SUCCEEDED(result));
1069
1070 D3D11_MAPPED_SUBRESOURCE mapPS = {0};
1071 result = mDeviceContext->Map(constantBufferPS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS);
1072 ASSERT(SUCCEEDED(result));
1073
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001074 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001075 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001076 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001077
1078 switch (uniform->type)
1079 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001080 case GL_SAMPLER_2D:
1081 case GL_SAMPLER_CUBE:
1082 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001083 case GL_FLOAT:
1084 case GL_FLOAT_VEC2:
1085 case GL_FLOAT_VEC3:
1086 case GL_FLOAT_VEC4:
1087 case GL_FLOAT_MAT2:
1088 case GL_FLOAT_MAT3:
1089 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001090 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001091 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001092 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS.pData;
1093 float (*f)[4] = (float(*)[4])uniform->data;
1094
1095 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001096 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001097 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1098 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1099 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1100 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001101 }
1102 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001103 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001104 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001105 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS.pData;
1106 float (*f)[4] = (float(*)[4])uniform->data;
1107
1108 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001109 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001110 c[uniform->psRegisterIndex + i][0] = f[i][0];
1111 c[uniform->psRegisterIndex + i][1] = f[i][1];
1112 c[uniform->psRegisterIndex + i][2] = f[i][2];
1113 c[uniform->psRegisterIndex + i][3] = f[i][3];
1114 }
1115 }
1116 break;
1117 case GL_INT:
1118 case GL_INT_VEC2:
1119 case GL_INT_VEC3:
1120 case GL_INT_VEC4:
1121 if (uniform->vsRegisterIndex >= 0)
1122 {
1123 int (*c)[4] = (int(*)[4])mapVS.pData;
1124 GLint *x = (GLint*)uniform->data;
1125 int count = gl::VariableColumnCount(uniform->type);
1126
1127 for (unsigned int i = 0; i < uniform->registerCount; i++)
1128 {
1129 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1130 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1131 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1132 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1133 }
1134 }
1135 if (uniform->psRegisterIndex >= 0)
1136 {
1137 int (*c)[4] = (int(*)[4])mapPS.pData;
1138 GLint *x = (GLint*)uniform->data;
1139 int count = gl::VariableColumnCount(uniform->type);
1140
1141 for (unsigned int i = 0; i < uniform->registerCount; i++)
1142 {
1143 if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0];
1144 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1145 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1146 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1147 }
1148 }
1149 break;
1150 case GL_BOOL:
1151 case GL_BOOL_VEC2:
1152 case GL_BOOL_VEC3:
1153 case GL_BOOL_VEC4:
1154 if (uniform->vsRegisterIndex >= 0)
1155 {
1156 int (*c)[4] = (int(*)[4])mapVS.pData;
1157 GLboolean *b = (GLboolean*)uniform->data;
1158 int count = gl::VariableColumnCount(uniform->type);
1159
1160 for (unsigned int i = 0; i < uniform->registerCount; i++)
1161 {
1162 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1163 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1164 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1165 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1166 }
1167 }
1168 if (uniform->psRegisterIndex >= 0)
1169 {
1170 int (*c)[4] = (int(*)[4])mapPS.pData;
1171 GLboolean *b = (GLboolean*)uniform->data;
1172 int count = gl::VariableColumnCount(uniform->type);
1173
1174 for (unsigned int i = 0; i < uniform->registerCount; i++)
1175 {
1176 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1177 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1178 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1179 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001180 }
1181 }
1182 break;
1183 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001184 UNREACHABLE();
1185 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001186 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001187
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001188 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001189 memcpy(mapVS.pData, &mVertexConstants, sizeof(dx_VertexConstants));
1190 memcpy(mapPS.pData, &mPixelConstants, sizeof(dx_PixelConstants));
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001191
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001192 mDeviceContext->Unmap(constantBufferVS, 0);
1193 mDeviceContext->VSSetConstantBuffers(0, 1, &constantBufferVS);
1194 constantBufferVS->Release();
1195
1196 mDeviceContext->Unmap(constantBufferPS, 0);
1197 mDeviceContext->PSSetConstantBuffers(0, 1, &constantBufferPS);
1198 constantBufferPS->Release();
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001199}
1200
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001201void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001202{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001203 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1204 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1205 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1206 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001207
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001208 unsigned int stencilUnmasked = 0x0;
1209 if (frameBuffer->hasStencil())
1210 {
1211 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1212 stencilUnmasked = (0x1 << stencilSize) - 1;
1213 }
1214 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1215 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001216
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001217 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1218 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1219 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001220
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001221 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1222 {
1223 // TODO
1224 UNIMPLEMENTED();
1225 }
1226 else
1227 {
1228 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1229 {
1230 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1231 if (renderbufferObject)
1232 {
1233 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1234 if (!renderTarget)
1235 {
1236 ERR("render target pointer unexpectedly null.");
1237 return;
1238 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001239
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001240 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1241 if (!framebufferRTV)
1242 {
1243 ERR("render target view pointer unexpectedly null.");
1244 return;
1245 }
1246
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001247 const float clearValues[4] = { clearParams.colorClearValue.red,
1248 clearParams.colorClearValue.green,
1249 clearParams.colorClearValue.blue,
1250 clearParams.colorClearValue.alpha };
1251 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001252
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001253 framebufferRTV->Release();
1254 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001255 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001256 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001257 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001258 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1259 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001260 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001261 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1262 if (!renderTarget)
1263 {
1264 ERR("render target pointer unexpectedly null.");
1265 return;
1266 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001267
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001268 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1269 if (!framebufferDSV)
1270 {
1271 ERR("depth stencil view pointer unexpectedly null.");
1272 return;
1273 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001274
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001275 UINT clearFlags = 0;
1276 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1277 {
1278 clearFlags |= D3D11_CLEAR_DEPTH;
1279 }
1280 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1281 {
1282 clearFlags |= D3D11_CLEAR_STENCIL;
1283 }
1284
1285 float depthClear = clearParams.depthClearValue;
1286 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1287
1288 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001289
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001290 framebufferDSV->Release();
1291 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001292 }
1293 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001294}
1295
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001296void Renderer11::markAllStateDirty()
1297{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001298 mAppliedRenderTargetSerial = 0;
1299 mAppliedDepthbufferSerial = 0;
1300 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001301 mDepthStencilInitialized = false;
1302 mRenderTargetDescInitialized = false;
1303
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001304 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001305 {
1306 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001307 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001308 }
1309 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1310 {
1311 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001312 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001313 }
1314
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001315 mForceSetBlendState = true;
1316 mForceSetRasterState = true;
1317 mForceSetDepthStencilState = true;
1318 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001319 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001320
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001321 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001322 mAppliedIBOffset = 0;
1323
daniel@transgaming.come4991412012-12-20 20:55:34 +00001324 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001325 mDxUniformsDirty = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001326}
1327
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001328void Renderer11::releaseDeviceResources()
1329{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001330 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001331 mInputLayoutCache.clear();
1332
1333 delete mVertexDataManager;
1334 mVertexDataManager = NULL;
1335
1336 delete mIndexDataManager;
1337 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001338
1339 delete mLineLoopIB;
1340 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001341
1342 delete mTriangleFanIB;
1343 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001344
1345 if (mCopyVB)
1346 {
1347 mCopyVB->Release();
1348 mCopyVB = NULL;
1349 }
1350
1351 if (mCopySampler)
1352 {
1353 mCopySampler->Release();
1354 mCopySampler = NULL;
1355 }
1356
1357 if (mCopyIL)
1358 {
1359 mCopyIL->Release();
1360 mCopyIL = NULL;
1361 }
1362
1363 if (mCopyVS)
1364 {
1365 mCopyVS->Release();
1366 mCopyVS = NULL;
1367 }
1368
1369 if (mCopyRGBAPS)
1370 {
1371 mCopyRGBAPS->Release();
1372 mCopyRGBAPS = NULL;
1373 }
1374
1375 if (mCopyRGBPS)
1376 {
1377 mCopyRGBPS->Release();
1378 mCopyRGBPS = NULL;
1379 }
1380
1381 if (mCopyLumPS)
1382 {
1383 mCopyLumPS->Release();
1384 mCopyLumPS = NULL;
1385 }
1386
1387 if (mCopyLumAlphaPS)
1388 {
1389 mCopyLumAlphaPS->Release();
1390 mCopyLumAlphaPS = NULL;
1391 }
1392
1393 mCopyResourcesInitialized = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001394}
1395
1396void Renderer11::markDeviceLost()
1397{
1398 mDeviceLost = true;
1399}
1400
1401bool Renderer11::isDeviceLost()
1402{
1403 return mDeviceLost;
1404}
1405
1406// set notify to true to broadcast a message to all contexts of the device loss
1407bool Renderer11::testDeviceLost(bool notify)
1408{
1409 bool isLost = false;
1410
1411 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001412 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001413
1414 if (isLost)
1415 {
1416 // ensure we note the device loss --
1417 // we'll probably get this done again by markDeviceLost
1418 // but best to remember it!
1419 // Note that we don't want to clear the device loss status here
1420 // -- this needs to be done by resetDevice
1421 mDeviceLost = true;
1422 if (notify)
1423 {
1424 mDisplay->notifyDeviceLost();
1425 }
1426 }
1427
1428 return isLost;
1429}
1430
1431bool Renderer11::testDeviceResettable()
1432{
1433 HRESULT status = D3D_OK;
1434
1435 // TODO
1436 UNIMPLEMENTED();
1437
1438 switch (status)
1439 {
1440 case D3DERR_DEVICENOTRESET:
1441 case D3DERR_DEVICEHUNG:
1442 return true;
1443 default:
1444 return false;
1445 }
1446}
1447
1448bool Renderer11::resetDevice()
1449{
1450 releaseDeviceResources();
1451
1452 // TODO
1453 UNIMPLEMENTED();
1454
1455 // reset device defaults
1456 initializeDevice();
1457 mDeviceLost = false;
1458
1459 return true;
1460}
1461
1462DWORD Renderer11::getAdapterVendor() const
1463{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001464 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001465}
1466
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001467std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001468{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001469 std::ostringstream rendererString;
1470
1471 rendererString << mDescription;
1472 rendererString << " Direct3D11";
1473
1474 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1475 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1476
1477 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001478}
1479
1480GUID Renderer11::getAdapterIdentifier() const
1481{
1482 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001483 // UNIMPLEMENTED();
1484 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001485 return foo;
1486}
1487
1488bool Renderer11::getDXT1TextureSupport()
1489{
1490 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001491 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001492 return false;
1493}
1494
1495bool Renderer11::getDXT3TextureSupport()
1496{
1497 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001498 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001499 return false;
1500}
1501
1502bool Renderer11::getDXT5TextureSupport()
1503{
1504 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001505 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001506 return false;
1507}
1508
1509bool Renderer11::getDepthTextureSupport() const
1510{
1511 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001512 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001513 return false;
1514}
1515
1516bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1517{
1518 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001519 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001520
1521 *filtering = false;
1522 *renderable = false;
1523 return false;
1524}
1525
1526bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1527{
1528 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001529 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001530
1531 *filtering = false;
1532 *renderable = false;
1533 return false;
1534}
1535
1536bool Renderer11::getLuminanceTextureSupport()
1537{
1538 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001539 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001540 return false;
1541}
1542
1543bool Renderer11::getLuminanceAlphaTextureSupport()
1544{
1545 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001546 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001547 return false;
1548}
1549
1550bool Renderer11::getTextureFilterAnisotropySupport() const
1551{
1552 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001553 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001554 return false;
1555}
1556
1557float Renderer11::getTextureMaxAnisotropy() const
1558{
1559 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001560 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001561 return 1.0f;
1562}
1563
1564bool Renderer11::getEventQuerySupport()
1565{
1566 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001567 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001568 return false;
1569}
1570
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001571unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001572{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001573 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1574 switch (mFeatureLevel)
1575 {
1576 case D3D_FEATURE_LEVEL_11_0:
1577 case D3D_FEATURE_LEVEL_10_1:
1578 case D3D_FEATURE_LEVEL_10_0:
1579 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1580 default: UNREACHABLE();
1581 return 0;
1582 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001583}
1584
1585bool Renderer11::getNonPower2TextureSupport() const
1586{
1587 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001588 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001589 return false;
1590}
1591
1592bool Renderer11::getOcclusionQuerySupport() const
1593{
1594 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001595 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001596 return false;
1597}
1598
1599bool Renderer11::getInstancingSupport() const
1600{
1601 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001602 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001603 return false;
1604}
1605
1606bool Renderer11::getShareHandleSupport() const
1607{
1608 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001609 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001610
1611 // PIX doesn't seem to support using share handles, so disable them.
1612 return false && !gl::perfActive();
1613}
1614
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001615bool Renderer11::getDerivativeInstructionSupport() const
1616{
1617 // TODO
1618 // UNIMPLEMENTED();
1619 return false;
1620}
1621
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001622int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001623{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001624 switch (mFeatureLevel)
1625 {
1626 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001627 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001628 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1629 default: UNREACHABLE(); return 0;
1630 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001631}
1632
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001633int Renderer11::getMinorShaderModel() const
1634{
1635 switch (mFeatureLevel)
1636 {
1637 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1638 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1639 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1640 default: UNREACHABLE(); return 0;
1641 }
1642}
1643
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001644float Renderer11::getMaxPointSize() const
1645{
1646 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001647 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001648 return 1.0f;
1649}
1650
1651int Renderer11::getMaxTextureWidth() const
1652{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001653 switch (mFeatureLevel)
1654 {
1655 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1656 case D3D_FEATURE_LEVEL_10_1:
1657 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1658 default: UNREACHABLE(); return 0;
1659 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001660}
1661
1662int Renderer11::getMaxTextureHeight() const
1663{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001664 switch (mFeatureLevel)
1665 {
1666 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1667 case D3D_FEATURE_LEVEL_10_1:
1668 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1669 default: UNREACHABLE(); return 0;
1670 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001671}
1672
1673bool Renderer11::get32BitIndexSupport() const
1674{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001675 switch (mFeatureLevel)
1676 {
1677 case D3D_FEATURE_LEVEL_11_0:
1678 case D3D_FEATURE_LEVEL_10_1:
1679 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1680 default: UNREACHABLE(); return false;
1681 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001682}
1683
1684int Renderer11::getMinSwapInterval() const
1685{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001686 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001687}
1688
1689int Renderer11::getMaxSwapInterval() const
1690{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001691 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001692}
1693
1694int Renderer11::getMaxSupportedSamples() const
1695{
1696 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001697 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001698 return 1;
1699}
1700
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001701bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001702{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001703 if (source && dest)
1704 {
1705 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1706 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1707
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001708 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001709 return true;
1710 }
1711
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001712 return false;
1713}
1714
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001715bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001716{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001717 if (source && dest)
1718 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001719 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1720 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001721
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001722 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001723 return true;
1724 }
1725
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001726 return false;
1727}
1728
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00001729bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001730 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001731{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001732 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1733 if (!colorbuffer)
1734 {
1735 ERR("Failed to retrieve the color buffer from the frame buffer.");
1736 return error(GL_OUT_OF_MEMORY, false);
1737 }
1738
1739 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1740 if (!sourceRenderTarget)
1741 {
1742 ERR("Failed to retrieve the render target from the frame buffer.");
1743 return error(GL_OUT_OF_MEMORY, false);
1744 }
1745
1746 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
1747 if (!source)
1748 {
1749 ERR("Failed to retrieve the render target view from the render target.");
1750 return error(GL_OUT_OF_MEMORY, false);
1751 }
1752
1753 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
1754 if (!storage11)
1755 {
1756 source->Release();
1757 ERR("Failed to retrieve the texture storage from the destination.");
1758 return error(GL_OUT_OF_MEMORY, false);
1759 }
1760
1761 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
1762 if (!destRenderTarget)
1763 {
1764 source->Release();
1765 ERR("Failed to retrieve the render target from the destination storage.");
1766 return error(GL_OUT_OF_MEMORY, false);
1767 }
1768
1769 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
1770 if (!dest)
1771 {
1772 source->Release();
1773 ERR("Failed to retrieve the render target view from the destination render target.");
1774 return error(GL_OUT_OF_MEMORY, false);
1775 }
1776
1777 gl::Rectangle destRect;
1778 destRect.x = xoffset;
1779 destRect.y = yoffset;
1780 destRect.width = sourceRect.width;
1781 destRect.height = sourceRect.height;
1782
1783 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
1784 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
1785
1786 source->Release();
1787 dest->Release();
1788
1789 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00001790}
1791
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00001792bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001793 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001794{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001795 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1796 if (!colorbuffer)
1797 {
1798 ERR("Failed to retrieve the color buffer from the frame buffer.");
1799 return error(GL_OUT_OF_MEMORY, false);
1800 }
1801
1802 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1803 if (!sourceRenderTarget)
1804 {
1805 ERR("Failed to retrieve the render target from the frame buffer.");
1806 return error(GL_OUT_OF_MEMORY, false);
1807 }
1808
1809 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
1810 if (!source)
1811 {
1812 ERR("Failed to retrieve the render target view from the render target.");
1813 return error(GL_OUT_OF_MEMORY, false);
1814 }
1815
1816 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
1817 if (!storage11)
1818 {
1819 source->Release();
1820 ERR("Failed to retrieve the texture storage from the destination.");
1821 return error(GL_OUT_OF_MEMORY, false);
1822 }
1823
1824 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
1825 if (!destRenderTarget)
1826 {
1827 source->Release();
1828 ERR("Failed to retrieve the render target from the destination storage.");
1829 return error(GL_OUT_OF_MEMORY, false);
1830 }
1831
1832 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
1833 if (!dest)
1834 {
1835 source->Release();
1836 ERR("Failed to retrieve the render target view from the destination render target.");
1837 return error(GL_OUT_OF_MEMORY, false);
1838 }
1839
1840 gl::Rectangle destRect;
1841 destRect.x = xoffset;
1842 destRect.y = yoffset;
1843 destRect.width = sourceRect.width;
1844 destRect.height = sourceRect.height;
1845
1846 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
1847 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
1848
1849 source->Release();
1850 dest->Release();
1851
1852 return ret;
1853}
1854
1855bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
1856 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
1857{
1858 HRESULT result;
1859
1860 if (!mCopyResourcesInitialized)
1861 {
1862 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
1863
1864 D3D11_BUFFER_DESC vbDesc;
1865 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
1866 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1867 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1868 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1869 vbDesc.MiscFlags = 0;
1870 vbDesc.StructureByteStride = 0;
1871
1872 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
1873 ASSERT(SUCCEEDED(result));
1874 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
1875
1876 D3D11_SAMPLER_DESC samplerDesc;
1877 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
1878 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
1879 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
1880 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
1881 samplerDesc.MipLODBias = 0.0f;
1882 samplerDesc.MaxAnisotropy = 0;
1883 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
1884 samplerDesc.BorderColor[0] = 0.0f;
1885 samplerDesc.BorderColor[1] = 0.0f;
1886 samplerDesc.BorderColor[2] = 0.0f;
1887 samplerDesc.BorderColor[3] = 0.0f;
1888 samplerDesc.MinLOD = 0.0f;
1889 samplerDesc.MaxLOD = 0.0f;
1890
1891 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
1892 ASSERT(SUCCEEDED(result));
1893 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
1894
1895 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1896 {
1897 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1898 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1899 };
1900
1901 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
1902 ASSERT(SUCCEEDED(result));
1903 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
1904
1905 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
1906 ASSERT(SUCCEEDED(result));
1907 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
1908
1909 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
1910 ASSERT(SUCCEEDED(result));
1911 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
1912
1913 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
1914 ASSERT(SUCCEEDED(result));
1915 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
1916
1917 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
1918 ASSERT(SUCCEEDED(result));
1919 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
1920
1921 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
1922 ASSERT(SUCCEEDED(result));
1923 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
1924
1925 mCopyResourcesInitialized = true;
1926 }
1927
1928 // Verify the source and destination area sizes
1929 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
1930 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
1931 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
1932 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
1933 {
1934 return error(GL_INVALID_VALUE, false);
1935 }
1936
1937 // Set vertices
1938 D3D11_MAPPED_SUBRESOURCE mappedResource;
1939 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1940 if (FAILED(result))
1941 {
1942 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
1943 return error(GL_OUT_OF_MEMORY, false);
1944 }
1945
1946 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
1947
1948 // Create a quad in homogeneous coordinates
1949 float x1 = (destArea.x / destWidth) * 2.0f - 1.0f;
1950 float y1 = (destArea.y / destHeight) * 2.0f - 1.0f;
1951 float x2 = ((destArea.x + destArea.width) / destWidth) * 2.0f - 1.0f;
1952 float y2 = ((destArea.y + destArea.height) / destHeight) * 2.0f - 1.0f;
1953
1954 float u1 = sourceArea.x / float(sourceWidth);
1955 float v1 = sourceArea.y / float(sourceHeight);
1956 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
1957 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
1958
1959 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
1960 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
1961 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
1962 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
1963
1964 mDeviceContext->Unmap(mCopyVB, 0);
1965
1966 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
1967 static UINT startIdx = 0;
1968 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
1969
1970 // Apply state
1971 static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1972 mDeviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
1973 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
1974 mDeviceContext->RSSetState(NULL);
1975
1976 // Apply shaders
1977 mDeviceContext->IASetInputLayout(mCopyIL);
1978 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1979 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
1980
1981 ID3D11PixelShader *ps = NULL;
1982 switch(destFormat)
1983 {
1984 case GL_RGBA: ps = mCopyRGBAPS; break;
1985 case GL_RGB: ps = mCopyRGBPS; break;
1986 case GL_ALPHA: ps = mCopyRGBAPS; break;
1987 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
1988 case GL_LUMINANCE: ps = mCopyLumPS; break;
1989 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
1990 default: UNREACHABLE(); ps = NULL; break;
1991 }
1992
1993 mDeviceContext->PSSetShader(ps, NULL, 0);
1994
1995 // Unset the currently bound shader resource to avoid conflicts
1996 static ID3D11ShaderResourceView *const nullSRV = NULL;
1997 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
1998
1999 // Apply render targets
2000 mDeviceContext->OMSetRenderTargets(1, &dest, NULL);
2001
2002 // Set the viewport
2003 D3D11_VIEWPORT viewport;
2004 viewport.TopLeftX = 0;
2005 viewport.TopLeftY = 0;
2006 viewport.Width = destWidth;
2007 viewport.Height = destHeight;
2008 viewport.MinDepth = 0.0f;
2009 viewport.MaxDepth = 1.0f;
2010 mDeviceContext->RSSetViewports(1, &viewport);
2011
2012 // Apply textures
2013 mDeviceContext->PSSetShaderResources(0, 1, &source);
2014 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2015
2016 // Draw the quad
2017 mDeviceContext->Draw(4, 0);
2018
2019 // Unbind textures and render targets and vertex buffer
2020 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2021
2022 static ID3D11RenderTargetView *const nullRTV = NULL;
2023 mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL);
2024
2025 static UINT zero = 0;
2026 static ID3D11Buffer *const nullBuffer = NULL;
2027 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2028
2029 markAllStateDirty();
2030
2031 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002032}
2033
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002034RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2035{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002036 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002037 RenderTarget11 *renderTarget = NULL;
2038 if (depth)
2039 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002040 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL,
2041 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002042 }
2043 else
2044 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002045 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
2046 swapChain11->getRenderTargetShaderResource(),
2047 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002048 }
2049 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002050}
2051
2052RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2053{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002054 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2055 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002056}
2057
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002058ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002059{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002060 ShaderExecutable11 *executable = NULL;
2061
2062 switch (type)
2063 {
2064 case GL_VERTEX_SHADER:
2065 {
2066 ID3D11VertexShader *vshader = NULL;
2067 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2068 ASSERT(SUCCEEDED(result));
2069
2070 if (vshader)
2071 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002072 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002073 }
2074 }
2075 break;
2076 case GL_FRAGMENT_SHADER:
2077 {
2078 ID3D11PixelShader *pshader = NULL;
2079 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2080 ASSERT(SUCCEEDED(result));
2081
2082 if (pshader)
2083 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002084 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002085 }
2086 }
2087 break;
2088 default:
2089 UNREACHABLE();
2090 break;
2091 }
2092
2093 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002094}
2095
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002096ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2097{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002098 const char *profile = NULL;
2099
2100 switch (type)
2101 {
2102 case GL_VERTEX_SHADER:
2103 profile = "vs_4_0";
2104 break;
2105 case GL_FRAGMENT_SHADER:
2106 profile = "ps_4_0";
2107 break;
2108 default:
2109 UNREACHABLE();
2110 return NULL;
2111 }
2112
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002113 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002114 if (!binary)
2115 return NULL;
2116
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002117 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002118 binary->Release();
2119
2120 return executable;
2121}
2122
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002123VertexBuffer *Renderer11::createVertexBuffer()
2124{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002125 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002126}
2127
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002128IndexBuffer *Renderer11::createIndexBuffer()
2129{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002130 return new IndexBuffer11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002131}
2132
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002133bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
2134 bool blitRenderTarget, bool blitDepthStencil)
2135{
2136 // TODO
2137 UNIMPLEMENTED();
2138 return false;
2139}
2140
2141void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2142 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2143{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002144 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002145 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002146
2147 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2148 if (colorbuffer)
2149 {
2150 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2151 if (renderTarget)
2152 {
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002153 subresourceIndex = renderTarget->getSubresourceIndex();
2154
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002155 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2156 if (colorBufferRTV)
2157 {
2158 ID3D11Resource *textureResource = NULL;
2159 colorBufferRTV->GetResource(&textureResource);
2160
2161 if (textureResource)
2162 {
2163 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)&colorBufferTexture);
2164 textureResource->Release();
2165
2166 if (FAILED(result))
2167 {
2168 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2169 "HRESULT: 0x%X.", result);
2170 return;
2171 }
2172 }
2173 }
2174 }
2175 }
2176
2177 if (colorBufferTexture)
2178 {
2179 gl::Rectangle area;
2180 area.x = x;
2181 area.y = y;
2182 area.width = width;
2183 area.height = height;
2184
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002185 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2186 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002187
2188 colorBufferTexture->Release();
2189 colorBufferTexture = NULL;
2190 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002191}
2192
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002193Image *Renderer11::createImage()
2194{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002195 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002196}
2197
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002198void Renderer11::generateMipmap(Image *dest, Image *src)
2199{
2200 // TODO
2201 UNIMPLEMENTED();
2202 return;
2203}
2204
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002205TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2206{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002207 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2208 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002209}
2210
2211TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2212{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002213 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002214}
2215
2216TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2217{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002218 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002219}
2220
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002221static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
2222{
2223 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
2224 destFormat == GL_ALPHA &&
2225 destType == GL_UNSIGNED_BYTE)
2226 {
2227 return 1;
2228 }
2229 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
2230 destFormat == GL_RGBA &&
2231 destType == GL_UNSIGNED_BYTE)
2232 {
2233 return 4;
2234 }
2235 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
2236 destFormat == GL_BGRA_EXT &&
2237 destType == GL_UNSIGNED_BYTE)
2238 {
2239 return 4;
2240 }
2241 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
2242 destFormat == GL_RGBA &&
2243 destType == GL_HALF_FLOAT_OES)
2244 {
2245 return 8;
2246 }
2247 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
2248 destFormat == GL_RGB &&
2249 destType == GL_FLOAT)
2250 {
2251 return 12;
2252 }
2253 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
2254 destFormat == GL_RGBA &&
2255 destType == GL_FLOAT)
2256 {
2257 return 16;
2258 }
2259 else
2260 {
2261 return 0;
2262 }
2263}
2264
2265static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
2266 unsigned int y, int inputPitch, gl::Color *outColor)
2267{
2268 switch (format)
2269 {
2270 case DXGI_FORMAT_R8G8B8A8_UNORM:
2271 {
2272 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2273 outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
2274 outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
2275 outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
2276 outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
2277 }
2278 break;
2279
2280 case DXGI_FORMAT_A8_UNORM:
2281 {
2282 outColor->red = 0.0f;
2283 outColor->green = 0.0f;
2284 outColor->blue = 0.0f;
2285 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
2286 }
2287 break;
2288
2289 case DXGI_FORMAT_R32G32B32A32_FLOAT:
2290 {
2291 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
2292 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
2293 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
2294 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
2295 }
2296 break;
2297
2298 case DXGI_FORMAT_R32G32B32_FLOAT:
2299 {
2300 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
2301 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
2302 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
2303 outColor->alpha = 1.0f;
2304 }
2305 break;
2306
2307 case DXGI_FORMAT_R16G16B16A16_FLOAT:
2308 {
2309 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
2310 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
2311 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
2312 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
2313 }
2314 break;
2315
2316 case DXGI_FORMAT_B8G8R8A8_UNORM:
2317 {
2318 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2319 outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
2320 outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
2321 outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
2322 outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
2323 }
2324 break;
2325
2326 case DXGI_FORMAT_R8_UNORM:
2327 {
2328 outColor->red = *(data + x + y * inputPitch) / 255.0f;
2329 outColor->green = 0.0f;
2330 outColor->blue = 0.0f;
2331 outColor->alpha = 1.0f;
2332 }
2333 break;
2334
2335 case DXGI_FORMAT_R8G8_UNORM:
2336 {
2337 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
2338
2339 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
2340 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
2341 outColor->blue = 0.0f;
2342 outColor->alpha = 1.0f;
2343 }
2344 break;
2345
2346 case DXGI_FORMAT_R16_FLOAT:
2347 {
2348 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
2349 outColor->green = 0.0f;
2350 outColor->blue = 0.0f;
2351 outColor->alpha = 1.0f;
2352 }
2353 break;
2354
2355 case DXGI_FORMAT_R16G16_FLOAT:
2356 {
2357 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
2358 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
2359 outColor->blue = 0.0f;
2360 outColor->alpha = 1.0f;
2361 }
2362 break;
2363
2364 default:
2365 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
2366 UNIMPLEMENTED();
2367 break;
2368 }
2369}
2370
2371static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
2372 unsigned int y, int outputPitch, void *outData)
2373{
2374 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
2375 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
2376
2377 switch (format)
2378 {
2379 case GL_RGBA:
2380 switch (type)
2381 {
2382 case GL_UNSIGNED_BYTE:
2383 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2384 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2385 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2386 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2387 break;
2388
2389 default:
2390 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
2391 UNIMPLEMENTED();
2392 break;
2393 }
2394 break;
2395
2396 case GL_BGRA_EXT:
2397 switch (type)
2398 {
2399 case GL_UNSIGNED_BYTE:
2400 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2401 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2402 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
2403 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2404 break;
2405
2406 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2407 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2408 // this type is packed as follows:
2409 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2410 // --------------------------------------------------------------------------------
2411 // | 4th | 3rd | 2nd | 1st component |
2412 // --------------------------------------------------------------------------------
2413 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2414 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2415 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
2416 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
2417 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
2418 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
2419 break;
2420
2421 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2422 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2423 // this type is packed as follows:
2424 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2425 // --------------------------------------------------------------------------------
2426 // | 4th | 3rd | 2nd | 1st component |
2427 // --------------------------------------------------------------------------------
2428 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2429 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2430 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
2431 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
2432 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
2433 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
2434 break;
2435
2436 default:
2437 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
2438 UNIMPLEMENTED();
2439 break;
2440 }
2441 break;
2442
2443 case GL_RGB:
2444 switch (type)
2445 {
2446 case GL_UNSIGNED_SHORT_5_6_5:
2447 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2448 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
2449 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
2450 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
2451 break;
2452
2453 case GL_UNSIGNED_BYTE:
2454 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2455 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2456 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2457 break;
2458
2459 default:
2460 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
2461 UNIMPLEMENTED();
2462 break;
2463 }
2464 break;
2465
2466 default:
2467 ERR("WritePixelColor not implemented for format 0x%X.", format);
2468 UNIMPLEMENTED();
2469 break;
2470 }
2471}
2472
2473void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2474 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2475 GLint packAlignment, void *pixels)
2476{
2477 D3D11_TEXTURE2D_DESC textureDesc;
2478 texture->GetDesc(&textureDesc);
2479
2480 D3D11_TEXTURE2D_DESC stagingDesc;
2481 stagingDesc.Width = area.width;
2482 stagingDesc.Height = area.height;
2483 stagingDesc.MipLevels = 1;
2484 stagingDesc.ArraySize = 1;
2485 stagingDesc.Format = textureDesc.Format;
2486 stagingDesc.SampleDesc.Count = 1;
2487 stagingDesc.SampleDesc.Quality = 0;
2488 stagingDesc.Usage = D3D11_USAGE_STAGING;
2489 stagingDesc.BindFlags = 0;
2490 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2491 stagingDesc.MiscFlags = 0;
2492
2493 ID3D11Texture2D* stagingTex = NULL;
2494 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2495 if (FAILED(result))
2496 {
2497 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2498 return;
2499 }
2500
2501 ID3D11Texture2D* srcTex = NULL;
2502 if (textureDesc.SampleDesc.Count > 1)
2503 {
2504 D3D11_TEXTURE2D_DESC resolveDesc;
2505 resolveDesc.Width = textureDesc.Width;
2506 resolveDesc.Height = textureDesc.Height;
2507 resolveDesc.MipLevels = 1;
2508 resolveDesc.ArraySize = 1;
2509 resolveDesc.Format = textureDesc.Format;
2510 resolveDesc.SampleDesc.Count = 1;
2511 resolveDesc.SampleDesc.Quality = 0;
2512 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2513 resolveDesc.BindFlags = 0;
2514 resolveDesc.CPUAccessFlags = 0;
2515 resolveDesc.MiscFlags = 0;
2516
2517 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2518 if (FAILED(result))
2519 {
2520 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
2521 stagingTex->Release();
2522 return;
2523 }
2524
2525 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2526 subResource = 0;
2527 }
2528 else
2529 {
2530 srcTex = texture;
2531 srcTex->AddRef();
2532 }
2533
2534 D3D11_BOX srcBox;
2535 srcBox.left = area.x;
2536 srcBox.right = area.x + area.width;
2537 srcBox.top = area.y;
2538 srcBox.bottom = area.y + area.height;
2539 srcBox.front = 0;
2540 srcBox.back = 1;
2541
2542 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2543
2544 srcTex->Release();
2545 srcTex = NULL;
2546
2547 D3D11_MAPPED_SUBRESOURCE mapping;
2548 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
2549
2550 unsigned char *source;
2551 int inputPitch;
2552 if (packReverseRowOrder)
2553 {
2554 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
2555 inputPitch = -static_cast<int>(mapping.RowPitch);
2556 }
2557 else
2558 {
2559 source = static_cast<unsigned char*>(mapping.pData);
2560 inputPitch = static_cast<int>(mapping.RowPitch);
2561 }
2562
2563 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
2564 if (fastPixelSize != 0)
2565 {
2566 unsigned char *dest = static_cast<unsigned char*>(pixels);
2567 for (int j = 0; j < area.height; j++)
2568 {
2569 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
2570 }
2571 }
2572 else
2573 {
2574 gl::Color pixelColor;
2575 for (int j = 0; j < area.height; j++)
2576 {
2577 for (int i = 0; i < area.width; i++)
2578 {
2579 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
2580 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
2581 }
2582 }
2583 }
2584
2585 mDeviceContext->Unmap(stagingTex, 0);
2586
2587 stagingTex->Release();
2588 stagingTex = NULL;
2589}
2590
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002591}