blob: 0c3aa3749f0eaf6940b501935f02298b7e8b3948 [file] [log] [blame]
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
8
9#include "common/debug.h"
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000014#include "libGLESv2/Program.h"
15#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000016#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000017#include "libGLESv2/renderer/Renderer11.h"
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +000018#include "libGLESv2/renderer/RenderTarget11.h"
daniel@transgaming.com65e65372012-11-28 19:33:50 +000019#include "libGLESv2/renderer/renderer11_utils.h"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000020#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000021#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000022#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000023#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000024#include "libGLESv2/renderer/IndexBuffer11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000025#include "libGLESv2/renderer/VertexDataManager.h"
26#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000027#include "libGLESv2/renderer/TextureStorage11.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000028
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000029#include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h"
30#include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h"
31#include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h"
32#include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h"
33#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h"
34
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000035#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
36#include "libGLESv2/renderer/shaders/compiled/clear11ps.h"
37
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000038#include <sstream>
39
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000040namespace rx
41{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000042static const DXGI_FORMAT RenderTargetFormats[] =
43 {
44 DXGI_FORMAT_R8G8B8A8_UNORM
45 };
46
47static const DXGI_FORMAT DepthStencilFormats[] =
48 {
49 DXGI_FORMAT_D24_UNORM_S8_UINT
50 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000051
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000052enum
53{
54 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
55};
56
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000057Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
58{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000059 mVertexDataManager = NULL;
60 mIndexDataManager = NULL;
61
daniel@transgaming.comc5114302012-12-20 21:11:36 +000062 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000063 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000064
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000065 mCopyResourcesInitialized = false;
66 mCopyVB = NULL;
67 mCopySampler = NULL;
68 mCopyIL = NULL;
69 mCopyVS = NULL;
70 mCopyRGBAPS = NULL;
71 mCopyRGBPS = NULL;
72 mCopyLumPS = NULL;
73 mCopyLumAlphaPS = NULL;
74
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000075 mClearResourcesInitialized = false;
76 mClearVB = NULL;
77 mClearIL = NULL;
78 mClearVS = NULL;
79 mClearPS = NULL;
80 mClearScissorRS = NULL;
81 mClearNoScissorRS = NULL;
82
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000083 mD3d11Module = NULL;
84 mDxgiModule = NULL;
85
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000086 mDeviceLost = false;
87
daniel@transgaming.com25072f62012-11-28 19:31:32 +000088 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000089 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000090 mDxgiAdapter = NULL;
91 mDxgiFactory = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000092}
93
94Renderer11::~Renderer11()
95{
96 releaseDeviceResources();
97
daniel@transgaming.com65e65372012-11-28 19:33:50 +000098 if (mDxgiFactory)
99 {
100 mDxgiFactory->Release();
101 mDxgiFactory = NULL;
102 }
103
104 if (mDxgiAdapter)
105 {
106 mDxgiAdapter->Release();
107 mDxgiAdapter = NULL;
108 }
109
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000110 if (mDeviceContext)
111 {
daniel@transgaming.comd5df4e82013-01-11 04:11:21 +0000112 mDeviceContext->ClearState();
113 mDeviceContext->Flush();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114 mDeviceContext->Release();
115 mDeviceContext = NULL;
116 }
117
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000118 if (mDevice)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000119 {
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000120 mDevice->Release();
121 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000122 }
123
124 if (mD3d11Module)
125 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 FreeLibrary(mD3d11Module);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000127 mD3d11Module = NULL;
128 }
129
130 if (mDxgiModule)
131 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 FreeLibrary(mDxgiModule);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133 mDxgiModule = NULL;
134 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000135}
136
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000137Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
138{
139 ASSERT(dynamic_cast<rx::Renderer11*>(renderer) != NULL);
140 return static_cast<rx::Renderer11*>(renderer);
141}
142
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000143EGLint Renderer11::initialize()
144{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000145 if (!initializeCompiler())
146 {
147 return EGL_NOT_INITIALIZED;
148 }
149
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000150 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
151 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000152
153 if (mD3d11Module == NULL || mDxgiModule == NULL)
154 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000155 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000156 return EGL_NOT_INITIALIZED;
157 }
158
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000159 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000160
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000161 if (D3D11CreateDevice == NULL)
162 {
163 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
164 return EGL_NOT_INITIALIZED;
165 }
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000166
167 D3D_FEATURE_LEVEL featureLevel[] =
168 {
169 D3D_FEATURE_LEVEL_11_0,
170 D3D_FEATURE_LEVEL_10_1,
171 D3D_FEATURE_LEVEL_10_0,
172 };
173
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000174 HRESULT result = D3D11CreateDevice(NULL,
175 D3D_DRIVER_TYPE_HARDWARE,
176 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000177 #if defined(_DEBUG)
178 D3D11_CREATE_DEVICE_DEBUG,
179 #else
180 0,
181 #endif
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000182 featureLevel,
183 sizeof(featureLevel)/sizeof(featureLevel[0]),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000184 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000185 &mDevice,
186 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000187 &mDeviceContext);
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000188
189 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000190 {
191 ERR("Could not create D3D11 device - aborting!\n");
192 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
193 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000194
195 IDXGIDevice *dxgiDevice = NULL;
196 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
197
198 if (FAILED(result))
199 {
200 ERR("Could not query DXGI device - aborting!\n");
201 return EGL_NOT_INITIALIZED;
202 }
203
204 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
205
206 if (FAILED(result))
207 {
208 ERR("Could not retrieve DXGI adapter - aborting!\n");
209 return EGL_NOT_INITIALIZED;
210 }
211
212 dxgiDevice->Release();
213
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000214 mDxgiAdapter->GetDesc(&mAdapterDescription);
215 memset(mDescription, 0, sizeof(mDescription));
216 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
217
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000218 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
219
220 if (!mDxgiFactory || FAILED(result))
221 {
222 ERR("Could not create DXGI factory - aborting!\n");
223 return EGL_NOT_INITIALIZED;
224 }
225
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000226 initializeDevice();
227
228 return EGL_SUCCESS;
229}
230
231// do any one-time device initialization
232// NOTE: this is also needed after a device lost/reset
233// to reset the scene status and ensure the default states are reset.
234void Renderer11::initializeDevice()
235{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000236 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000237 mInputLayoutCache.initialize(mDevice, mDeviceContext);
238
239 ASSERT(!mVertexDataManager && !mIndexDataManager);
240 mVertexDataManager = new VertexDataManager(this);
241 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000242
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000243 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000244}
245
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000246int Renderer11::generateConfigs(ConfigDesc **configDescList)
247{
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000248 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
249 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000250 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
251 int numConfigs = 0;
252
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000253 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000254 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000255 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000256 {
257 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
258
259 UINT formatSupport = 0;
260 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
261
262 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
263 {
264 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
265
266 UINT formatSupport = 0;
267 HRESULT result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
268
269 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL))
270 {
271 ConfigDesc newConfig;
272 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
273 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
274 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
275 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
276
277 (*configDescList)[numConfigs++] = newConfig;
278 }
279 }
280 }
281 }
282
283 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000284}
285
286void Renderer11::deleteConfigs(ConfigDesc *configDescList)
287{
288 delete [] (configDescList);
289}
290
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000291void Renderer11::sync(bool block)
292{
293 // TODO
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000294 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000295}
296
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000297SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
298{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000299 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000300}
301
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000302void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
303{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000304 if (type == gl::SAMPLER_PIXEL)
305 {
306 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
307 {
308 ERR("Pixel shader sampler index %i is not valid.", index);
309 return;
310 }
311
312 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
313 {
314 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
315
316 if (!dxSamplerState)
317 {
318 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
319 "sampler state for pixel shaders at slot %i.", index);
320 }
321
322 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
323
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000324 mCurPixelSamplerStates[index] = samplerState;
325 }
326
327 mForceSetPixelSamplerStates[index] = false;
328 }
329 else if (type == gl::SAMPLER_VERTEX)
330 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000331 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000332 {
333 ERR("Vertex shader sampler index %i is not valid.", index);
334 return;
335 }
336
337 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
338 {
339 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
340
341 if (!dxSamplerState)
342 {
343 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
344 "sampler state for vertex shaders at slot %i.", index);
345 }
346
347 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
348
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000349 mCurVertexSamplerStates[index] = samplerState;
350 }
351
352 mForceSetVertexSamplerStates[index] = false;
353 }
354 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000355}
356
357void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
358{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000359 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000360 unsigned int serial = 0;
361 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000362
363 if (texture)
364 {
365 TextureStorageInterface *texStorage = texture->getNativeTexture();
366 if (texStorage)
367 {
368 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
369 textureSRV = storage11->getSRV();
370 }
371
372 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
373 // missing the shader resource view
374 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000375
376 serial = texture->getTextureSerial();
377 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000378 }
379
380 if (type == gl::SAMPLER_PIXEL)
381 {
382 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
383 {
384 ERR("Pixel shader sampler index %i is not valid.", index);
385 return;
386 }
387
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000388 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
389 {
390 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
391 }
392
393 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000394 }
395 else if (type == gl::SAMPLER_VERTEX)
396 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000397 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000398 {
399 ERR("Vertex shader sampler index %i is not valid.", index);
400 return;
401 }
402
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000403 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
404 {
405 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
406 }
407
408 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000409 }
410 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000411}
412
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000413void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000414{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000415 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000416 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000417 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
418 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000419 if (!dxRasterState)
420 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000421 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000422 "rasterizer state.");
423 }
424
425 mDeviceContext->RSSetState(dxRasterState);
426
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000427 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000428 }
429
430 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000431}
432
433void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
434 unsigned int sampleMask)
435{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000436 if (mForceSetBlendState ||
437 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
438 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
439 sampleMask != mCurSampleMask)
440 {
441 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
442 if (!dxBlendState)
443 {
444 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
445 "blend state.");
446 }
447
448 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
449 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
450
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000451 mCurBlendState = blendState;
452 mCurBlendColor = blendColor;
453 mCurSampleMask = sampleMask;
454 }
455
456 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000457}
458
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000459void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000460 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000461{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000462 if (mForceSetDepthStencilState ||
463 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
464 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
465 {
466 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
467 stencilRef != stencilBackRef ||
468 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
469 {
470 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
471 "invalid under WebGL.");
472 return error(GL_INVALID_OPERATION);
473 }
474
475 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
476 if (!dxDepthStencilState)
477 {
478 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
479 "setting the default depth stencil state.");
480 }
481
482 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
483
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000484 mCurDepthStencilState = depthStencilState;
485 mCurStencilRef = stencilRef;
486 mCurStencilBackRef = stencilBackRef;
487 }
488
489 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000490}
491
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000492void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000493{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000494 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
495 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000496 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000497 if (enabled)
498 {
499 D3D11_RECT rect;
500 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
501 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
502 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
503 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000504
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000505 mDeviceContext->RSSetScissorRects(1, &rect);
506 }
507
508 if (enabled != mScissorEnabled)
509 {
510 mForceSetRasterState = true;
511 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000512
513 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000514 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000515 }
516
517 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000518}
519
daniel@transgaming.com12985182012-12-20 20:56:31 +0000520bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000521 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000522{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000523 gl::Rectangle actualViewport = viewport;
524 float actualZNear = gl::clamp01(zNear);
525 float actualZFar = gl::clamp01(zFar);
526 if (ignoreViewport)
527 {
528 actualViewport.x = 0;
529 actualViewport.y = 0;
530 actualViewport.width = mRenderTargetDesc.width;
531 actualViewport.height = mRenderTargetDesc.height;
532 actualZNear = 0.0f;
533 actualZFar = 1.0f;
534 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000535
536 D3D11_VIEWPORT dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000537 dxViewport.TopLeftX = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
538 dxViewport.TopLeftY = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
539 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.TopLeftX));
540 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.TopLeftY));
541 dxViewport.MinDepth = actualZNear;
542 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000543
544 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
545 {
546 return false; // Nothing to render
547 }
548
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000549 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
550 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000551
daniel@transgaming.com53670042012-11-28 20:55:51 +0000552 if (viewportChanged)
553 {
554 mDeviceContext->RSSetViewports(1, &dxViewport);
555
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000556 mCurViewport = actualViewport;
557 mCurNear = actualZNear;
558 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000559 }
560
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000561 if (currentProgram)
daniel@transgaming.com53670042012-11-28 20:55:51 +0000562 {
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000563 dx_VertexConstants vc = {0};
564 dx_PixelConstants pc = {0};
daniel@transgaming.com53670042012-11-28 20:55:51 +0000565
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000566 vc.halfPixelSize[0] = 0.0f;
567 vc.halfPixelSize[1] = 0.0f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000568
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000569 pc.coord[0] = actualViewport.width * 0.5f;
570 pc.coord[1] = actualViewport.height * 0.5f;
571 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
572 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000573
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000574 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
575 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
576 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
577
578 vc.depthRange[0] = actualZNear;
579 vc.depthRange[1] = actualZFar;
580 vc.depthRange[2] = actualZFar - actualZNear;
581
582 pc.depthRange[0] = actualZNear;
583 pc.depthRange[1] = actualZFar;
584 pc.depthRange[2] = actualZFar - actualZNear;
585
586 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
587 {
588 mVertexConstants = vc;
589 mDxUniformsDirty = true;
590 }
591
592 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
593 {
594 mPixelConstants = pc;
595 mDxUniformsDirty = true;
596 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000597 }
598
599 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000600 return true;
601}
602
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000603bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
604{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000605 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000606
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000607 switch (mode)
608 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000609 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
610 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000611 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000612 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
613 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
614 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000615 // emulate fans via rewriting index buffer
616 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000617 default:
618 return error(GL_INVALID_ENUM, false);
619 }
620
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000621 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000622
623 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000624}
625
626bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000627{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000628 // Get the color render buffer and serial
629 gl::Renderbuffer *renderbufferObject = NULL;
630 unsigned int renderTargetSerial = 0;
631 if (framebuffer->getColorbufferType() != GL_NONE)
632 {
633 renderbufferObject = framebuffer->getColorbuffer();
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000634
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000635 if (!renderbufferObject)
636 {
637 ERR("render target pointer unexpectedly null.");
daniel@transgaming.come9c71b42012-11-28 21:02:23 +0000638 return false;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000639 }
640
641 renderTargetSerial = renderbufferObject->getSerial();
642 }
643
644 // Get the depth stencil render buffer and serials
645 gl::Renderbuffer *depthStencil = NULL;
646 unsigned int depthbufferSerial = 0;
647 unsigned int stencilbufferSerial = 0;
648 if (framebuffer->getDepthbufferType() != GL_NONE)
649 {
650 depthStencil = framebuffer->getDepthbuffer();
651 if (!depthStencil)
652 {
653 ERR("Depth stencil pointer unexpectedly null.");
654 return false;
655 }
656
657 depthbufferSerial = depthStencil->getSerial();
658 }
659 else if (framebuffer->getStencilbufferType() != GL_NONE)
660 {
661 depthStencil = framebuffer->getStencilbuffer();
662 if (!depthStencil)
663 {
664 ERR("Depth stencil pointer unexpectedly null.");
665 return false;
666 }
667
668 stencilbufferSerial = depthStencil->getSerial();
669 }
670
671 // Extract the render target dimensions and view
672 unsigned int renderTargetWidth = 0;
673 unsigned int renderTargetHeight = 0;
674 GLenum renderTargetFormat = 0;
675 ID3D11RenderTargetView* framebufferRTV = NULL;
676 if (renderbufferObject)
677 {
678 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
679 if (!renderTarget)
680 {
681 ERR("render target pointer unexpectedly null.");
682 return false;
683 }
684
685 framebufferRTV = renderTarget->getRenderTargetView();
686 if (!framebufferRTV)
687 {
688 ERR("render target view pointer unexpectedly null.");
689 return false;
690 }
691
692 renderTargetWidth = renderbufferObject->getWidth();
693 renderTargetHeight = renderbufferObject->getHeight();
694 renderTargetFormat = renderbufferObject->getActualFormat();
695 }
696
697 // Extract the depth stencil sizes and view
698 unsigned int depthSize = 0;
699 unsigned int stencilSize = 0;
700 ID3D11DepthStencilView* framebufferDSV = NULL;
701 if (depthStencil)
702 {
703 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
704 if (!depthStencilRenderTarget)
705 {
706 ERR("render target pointer unexpectedly null.");
707 if (framebufferRTV)
708 {
709 framebufferRTV->Release();
710 }
711 return false;
712 }
713
714 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
715 if (!framebufferDSV)
716 {
717 ERR("depth stencil view pointer unexpectedly null.");
718 if (framebufferRTV)
719 {
720 framebufferRTV->Release();
721 }
722 return false;
723 }
724
725 // If there is no render buffer, the width, height and format values come from
726 // the depth stencil
727 if (!renderbufferObject)
728 {
729 renderTargetWidth = depthStencil->getWidth();
730 renderTargetHeight = depthStencil->getHeight();
731 renderTargetFormat = depthStencil->getActualFormat();
732 }
733
734 depthSize = depthStencil->getDepthSize();
735 stencilSize = depthStencil->getStencilSize();
736 }
737
738 // Apply the render target and depth stencil
739 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
740 renderTargetSerial != mAppliedRenderTargetSerial ||
741 depthbufferSerial != mAppliedDepthbufferSerial ||
742 stencilbufferSerial != mAppliedStencilbufferSerial)
743 {
744 mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV);
745
746 mRenderTargetDesc.width = renderTargetWidth;
747 mRenderTargetDesc.height = renderTargetHeight;
748 mRenderTargetDesc.format = renderTargetFormat;
749 mForceSetViewport = true; // TODO: It may not be required to clamp the viewport in D3D11
750 mForceSetScissor = true; // TODO: It may not be required to clamp the scissor in D3D11
751
752 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
753 {
754 mCurDepthSize = depthSize;
755 mForceSetRasterState = true;
756 }
757
758 mCurStencilSize = stencilSize;
759
760 mAppliedRenderTargetSerial = renderTargetSerial;
761 mAppliedDepthbufferSerial = depthbufferSerial;
762 mAppliedStencilbufferSerial = stencilbufferSerial;
763 mRenderTargetDescInitialized = true;
764 mDepthStencilInitialized = true;
765 }
766
767 if (framebufferRTV)
768 {
769 framebufferRTV->Release();
770 }
771 if (framebufferDSV)
772 {
773 framebufferDSV->Release();
774 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000775
776 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000777}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000778
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000779GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000780{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000781 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
782 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
783 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000784 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000785 return err;
786 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000787
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000788 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000789}
790
daniel@transgaming.com31240482012-11-28 21:06:41 +0000791GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000792{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000793 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000794
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000795 if (err == GL_NO_ERROR)
796 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000797 if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000798 {
799 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
800
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000801 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000802 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000803 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000804 }
805 }
806
807 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000808}
809
810void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
811{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000812 if (mode == GL_LINE_LOOP)
813 {
814 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
815 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000816 else if (mode == GL_TRIANGLE_FAN)
817 {
818 drawTriangleFan(count, GL_NONE, NULL, 0, NULL);
819 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000820 else if (instances > 0)
821 {
822 // TODO
823 UNIMPLEMENTED();
824 }
825 else
826 {
827 mDeviceContext->Draw(count, 0);
828 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000829}
830
daniel@transgaming.com31240482012-11-28 21:06:41 +0000831void 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 +0000832{
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000833 if (mode == GL_LINE_LOOP)
834 {
835 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
836 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000837 else if (mode == GL_TRIANGLE_FAN)
838 {
839 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
840 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000841 else
842 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +0000843 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000844 }
845}
846
847void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
848{
849 // Get the raw indices for an indexed draw
850 if (type != GL_NONE && elementArrayBuffer)
851 {
852 gl::Buffer *indexBuffer = elementArrayBuffer;
853 intptr_t offset = reinterpret_cast<intptr_t>(indices);
854 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
855 }
856
857 if (!mLineLoopIB)
858 {
859 mLineLoopIB = new StreamingIndexBufferInterface(this);
860 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
861 {
862 delete mLineLoopIB;
863 mLineLoopIB = NULL;
864
865 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
866 return error(GL_OUT_OF_MEMORY);
867 }
868 }
869
870 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
871 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
872 {
873 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
874 return error(GL_OUT_OF_MEMORY);
875 }
876
877 void* mappedMemory = NULL;
878 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
879 if (offset == -1 || mappedMemory == NULL)
880 {
881 ERR("Could not map index buffer for GL_LINE_LOOP.");
882 return error(GL_OUT_OF_MEMORY);
883 }
884
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000885 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000886 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000887
888 switch (type)
889 {
890 case GL_NONE: // Non-indexed draw
891 for (int i = 0; i < count; i++)
892 {
893 data[i] = i;
894 }
895 data[count] = 0;
896 break;
897 case GL_UNSIGNED_BYTE:
898 for (int i = 0; i < count; i++)
899 {
900 data[i] = static_cast<const GLubyte*>(indices)[i];
901 }
902 data[count] = static_cast<const GLubyte*>(indices)[0];
903 break;
904 case GL_UNSIGNED_SHORT:
905 for (int i = 0; i < count; i++)
906 {
907 data[i] = static_cast<const GLushort*>(indices)[i];
908 }
909 data[count] = static_cast<const GLushort*>(indices)[0];
910 break;
911 case GL_UNSIGNED_INT:
912 for (int i = 0; i < count; i++)
913 {
914 data[i] = static_cast<const GLuint*>(indices)[i];
915 }
916 data[count] = static_cast<const GLuint*>(indices)[0];
917 break;
918 default: UNREACHABLE();
919 }
920
921 if (!mLineLoopIB->unmapBuffer())
922 {
923 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
924 return error(GL_OUT_OF_MEMORY);
925 }
926
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000927 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000928 {
929 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
930
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000931 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000932 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000933 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000934 }
935
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000936 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000937}
938
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000939void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
940{
941 // Get the raw indices for an indexed draw
942 if (type != GL_NONE && elementArrayBuffer)
943 {
944 gl::Buffer *indexBuffer = elementArrayBuffer;
945 intptr_t offset = reinterpret_cast<intptr_t>(indices);
946 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
947 }
948
949 if (!mTriangleFanIB)
950 {
951 mTriangleFanIB = new StreamingIndexBufferInterface(this);
952 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
953 {
954 delete mTriangleFanIB;
955 mTriangleFanIB = NULL;
956
957 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
958 return error(GL_OUT_OF_MEMORY);
959 }
960 }
961
962 const int numTris = count - 2;
963 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
964 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
965 {
966 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
967 return error(GL_OUT_OF_MEMORY);
968 }
969
970 void* mappedMemory = NULL;
971 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
972 if (offset == -1 || mappedMemory == NULL)
973 {
974 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
975 return error(GL_OUT_OF_MEMORY);
976 }
977
978 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
979 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
980
981 switch (type)
982 {
983 case GL_NONE: // Non-indexed draw
984 for (int i = 0; i < numTris; i++)
985 {
986 data[i*3 + 0] = 0;
987 data[i*3 + 1] = i + 1;
988 data[i*3 + 2] = i + 2;
989 }
990 break;
991 case GL_UNSIGNED_BYTE:
992 for (int i = 0; i < numTris; i++)
993 {
994 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
995 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
996 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
997 }
998 break;
999 case GL_UNSIGNED_SHORT:
1000 for (int i = 0; i < numTris; i++)
1001 {
1002 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1003 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1004 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1005 }
1006 break;
1007 case GL_UNSIGNED_INT:
1008 for (int i = 0; i < numTris; i++)
1009 {
1010 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1011 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1012 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1013 }
1014 break;
1015 default: UNREACHABLE();
1016 }
1017
1018 if (!mTriangleFanIB->unmapBuffer())
1019 {
1020 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
1021 return error(GL_OUT_OF_MEMORY);
1022 }
1023
1024 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1025 {
1026 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1027
1028 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1029 mAppliedIBSerial = mTriangleFanIB->getSerial();
1030 mAppliedIBOffset = indexBufferOffset;
1031 }
1032
1033 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1034}
1035
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001036void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1037{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001038 unsigned int programBinarySerial = programBinary->getSerial();
1039 if (programBinarySerial != mAppliedProgramBinarySerial)
1040 {
1041 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1042 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001043
daniel@transgaming.come4991412012-12-20 20:55:34 +00001044 ID3D11VertexShader *vertexShader = NULL;
1045 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001046
daniel@transgaming.come4991412012-12-20 20:55:34 +00001047 ID3D11PixelShader *pixelShader = NULL;
1048 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001049
daniel@transgaming.come4991412012-12-20 20:55:34 +00001050 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1051 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1052 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001053 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001054
1055 mAppliedProgramBinarySerial = programBinarySerial;
1056 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001057}
1058
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001059void Renderer11::applyUniforms(const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001060{
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001061 D3D11_BUFFER_DESC constantBufferDescriptionVS0 = {0};
1062 constantBufferDescriptionVS0.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1063 constantBufferDescriptionVS0.Usage = D3D11_USAGE_DYNAMIC;
1064 constantBufferDescriptionVS0.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1065 constantBufferDescriptionVS0.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1066 constantBufferDescriptionVS0.MiscFlags = 0;
1067 constantBufferDescriptionVS0.StructureByteStride = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001068
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001069 ID3D11Buffer *constantBufferVS0 = NULL;
1070 HRESULT result = mDevice->CreateBuffer(&constantBufferDescriptionVS0, NULL, &constantBufferVS0);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001071 ASSERT(SUCCEEDED(result));
1072
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001073 D3D11_BUFFER_DESC constantBufferDescriptionPS0 = {0};
1074 constantBufferDescriptionPS0.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1075 constantBufferDescriptionPS0.Usage = D3D11_USAGE_DYNAMIC;
1076 constantBufferDescriptionPS0.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1077 constantBufferDescriptionPS0.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1078 constantBufferDescriptionPS0.MiscFlags = 0;
1079 constantBufferDescriptionPS0.StructureByteStride = 0;
1080
1081 ID3D11Buffer *constantBufferPS0 = NULL;
1082 result = mDevice->CreateBuffer(&constantBufferDescriptionPS0, NULL, &constantBufferPS0);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001083 ASSERT(SUCCEEDED(result));
1084
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001085 D3D11_MAPPED_SUBRESOURCE mapVS0 = {0};
1086 result = mDeviceContext->Map(constantBufferVS0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS0);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001087 ASSERT(SUCCEEDED(result));
1088
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001089 D3D11_MAPPED_SUBRESOURCE mapPS0 = {0};
1090 result = mDeviceContext->Map(constantBufferPS0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS0);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001091 ASSERT(SUCCEEDED(result));
1092
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001093 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001094 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001095 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001096
1097 switch (uniform->type)
1098 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001099 case GL_SAMPLER_2D:
1100 case GL_SAMPLER_CUBE:
1101 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001102 case GL_FLOAT:
1103 case GL_FLOAT_VEC2:
1104 case GL_FLOAT_VEC3:
1105 case GL_FLOAT_VEC4:
1106 case GL_FLOAT_MAT2:
1107 case GL_FLOAT_MAT3:
1108 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001109 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001110 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001111 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001112 float (*f)[4] = (float(*)[4])uniform->data;
1113
1114 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001115 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001116 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1117 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1118 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1119 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001120 }
1121 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001122 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001123 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001124 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001125 float (*f)[4] = (float(*)[4])uniform->data;
1126
1127 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001128 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001129 c[uniform->psRegisterIndex + i][0] = f[i][0];
1130 c[uniform->psRegisterIndex + i][1] = f[i][1];
1131 c[uniform->psRegisterIndex + i][2] = f[i][2];
1132 c[uniform->psRegisterIndex + i][3] = f[i][3];
1133 }
1134 }
1135 break;
1136 case GL_INT:
1137 case GL_INT_VEC2:
1138 case GL_INT_VEC3:
1139 case GL_INT_VEC4:
1140 if (uniform->vsRegisterIndex >= 0)
1141 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001142 int (*c)[4] = (int(*)[4])mapVS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001143 GLint *x = (GLint*)uniform->data;
1144 int count = gl::VariableColumnCount(uniform->type);
1145
1146 for (unsigned int i = 0; i < uniform->registerCount; i++)
1147 {
1148 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1149 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1150 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1151 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1152 }
1153 }
1154 if (uniform->psRegisterIndex >= 0)
1155 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001156 int (*c)[4] = (int(*)[4])mapPS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001157 GLint *x = (GLint*)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->psRegisterIndex + i][0] = x[i * count + 0];
1163 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1164 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1165 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1166 }
1167 }
1168 break;
1169 case GL_BOOL:
1170 case GL_BOOL_VEC2:
1171 case GL_BOOL_VEC3:
1172 case GL_BOOL_VEC4:
1173 if (uniform->vsRegisterIndex >= 0)
1174 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001175 int (*c)[4] = (int(*)[4])mapVS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001176 GLboolean *b = (GLboolean*)uniform->data;
1177 int count = gl::VariableColumnCount(uniform->type);
1178
1179 for (unsigned int i = 0; i < uniform->registerCount; i++)
1180 {
1181 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1182 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1183 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1184 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1185 }
1186 }
1187 if (uniform->psRegisterIndex >= 0)
1188 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001189 int (*c)[4] = (int(*)[4])mapPS0.pData;
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001190 GLboolean *b = (GLboolean*)uniform->data;
1191 int count = gl::VariableColumnCount(uniform->type);
1192
1193 for (unsigned int i = 0; i < uniform->registerCount; i++)
1194 {
1195 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1196 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1197 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1198 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001199 }
1200 }
1201 break;
1202 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001203 UNREACHABLE();
1204 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001205 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001206
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001207 mDeviceContext->Unmap(constantBufferVS0, 0);
1208 mDeviceContext->Unmap(constantBufferPS0, 0);
1209
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001210 // Driver uniforms
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001211 D3D11_BUFFER_DESC constantBufferDescriptionVS1 = {0};
1212 constantBufferDescriptionVS1.ByteWidth = sizeof(dx_VertexConstants);
1213 constantBufferDescriptionVS1.Usage = D3D11_USAGE_DYNAMIC;
1214 constantBufferDescriptionVS1.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1215 constantBufferDescriptionVS1.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1216 constantBufferDescriptionVS1.MiscFlags = 0;
1217 constantBufferDescriptionVS1.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001218
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001219 ID3D11Buffer *constantBufferVS1 = NULL;
1220 result = mDevice->CreateBuffer(&constantBufferDescriptionVS1, NULL, &constantBufferVS1);
1221 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001222
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001223 D3D11_BUFFER_DESC constantBufferDescriptionPS1 = {0};
1224 constantBufferDescriptionPS1.ByteWidth = sizeof(dx_PixelConstants);
1225 constantBufferDescriptionPS1.Usage = D3D11_USAGE_DYNAMIC;
1226 constantBufferDescriptionPS1.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1227 constantBufferDescriptionPS1.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1228 constantBufferDescriptionPS1.MiscFlags = 0;
1229 constantBufferDescriptionPS1.StructureByteStride = 0;
1230
1231 ID3D11Buffer *constantBufferPS1 = NULL;
1232 result = mDevice->CreateBuffer(&constantBufferDescriptionPS1, NULL, &constantBufferPS1);
1233 ASSERT(SUCCEEDED(result));
1234
1235 D3D11_MAPPED_SUBRESOURCE mapVS1 = {0};
1236 result = mDeviceContext->Map(constantBufferVS1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS1);
1237 ASSERT(SUCCEEDED(result));
1238
1239 D3D11_MAPPED_SUBRESOURCE mapPS1 = {0};
1240 result = mDeviceContext->Map(constantBufferPS1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS1);
1241 ASSERT(SUCCEEDED(result));
1242
1243 memcpy(mapVS1.pData, &mVertexConstants, sizeof(dx_VertexConstants));
1244 memcpy(mapPS1.pData, &mPixelConstants, sizeof(dx_PixelConstants));
1245
1246 mDeviceContext->Unmap(constantBufferVS1, 0);
1247 mDeviceContext->Unmap(constantBufferPS1, 0);
1248
1249 ID3D11Buffer *buffersVS[2] = {constantBufferVS0, constantBufferVS1};
1250 mDeviceContext->VSSetConstantBuffers(0, 2, buffersVS);
1251 constantBufferVS0->Release();
1252 constantBufferVS1->Release();
1253
1254 ID3D11Buffer *buffersPS[2] = {constantBufferPS0, constantBufferPS1};
1255 mDeviceContext->PSSetConstantBuffers(0, 2, buffersPS);
1256 constantBufferPS0->Release();
1257 constantBufferPS1->Release();
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001258}
1259
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001260void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001261{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001262 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1263 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1264 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1265 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001266
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001267 unsigned int stencilUnmasked = 0x0;
1268 if (frameBuffer->hasStencil())
1269 {
1270 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1271 stencilUnmasked = (0x1 << stencilSize) - 1;
1272 }
1273 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1274 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001275
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001276 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1277 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1278 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001279
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001280 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1281 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001282 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001283 }
1284 else
1285 {
1286 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1287 {
1288 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1289 if (renderbufferObject)
1290 {
1291 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1292 if (!renderTarget)
1293 {
1294 ERR("render target pointer unexpectedly null.");
1295 return;
1296 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001297
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001298 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1299 if (!framebufferRTV)
1300 {
1301 ERR("render target view pointer unexpectedly null.");
1302 return;
1303 }
1304
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001305 const float clearValues[4] = { clearParams.colorClearValue.red,
1306 clearParams.colorClearValue.green,
1307 clearParams.colorClearValue.blue,
1308 clearParams.colorClearValue.alpha };
1309 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001310
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001311 framebufferRTV->Release();
1312 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001313 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001314 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001315 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001316 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1317 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001318 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001319 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1320 if (!renderTarget)
1321 {
1322 ERR("render target pointer unexpectedly null.");
1323 return;
1324 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001325
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001326 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1327 if (!framebufferDSV)
1328 {
1329 ERR("depth stencil view pointer unexpectedly null.");
1330 return;
1331 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001332
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001333 UINT clearFlags = 0;
1334 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1335 {
1336 clearFlags |= D3D11_CLEAR_DEPTH;
1337 }
1338 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1339 {
1340 clearFlags |= D3D11_CLEAR_STENCIL;
1341 }
1342
1343 float depthClear = clearParams.depthClearValue;
1344 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1345
1346 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001347
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001348 framebufferDSV->Release();
1349 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001350 }
1351 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001352}
1353
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001354void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1355{
1356 HRESULT result;
1357
1358 if (!mClearResourcesInitialized)
1359 {
1360 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1361
1362 D3D11_BUFFER_DESC vbDesc;
1363 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1364 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1365 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1366 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1367 vbDesc.MiscFlags = 0;
1368 vbDesc.StructureByteStride = 0;
1369
1370 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1371 ASSERT(SUCCEEDED(result));
1372 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1373
1374 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1375 {
1376 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1377 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1378 };
1379
1380 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1381 ASSERT(SUCCEEDED(result));
1382 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1383
1384 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1385 ASSERT(SUCCEEDED(result));
1386 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1387
1388 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1389 ASSERT(SUCCEEDED(result));
1390 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1391
1392 D3D11_RASTERIZER_DESC rsScissorDesc;
1393 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1394 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1395 rsScissorDesc.FrontCounterClockwise = FALSE;
1396 rsScissorDesc.DepthBias = 0;
1397 rsScissorDesc.DepthBiasClamp = 0.0f;
1398 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1399 rsScissorDesc.DepthClipEnable = FALSE;
1400 rsScissorDesc.ScissorEnable = TRUE;
1401 rsScissorDesc.MultisampleEnable = FALSE;
1402 rsScissorDesc.AntialiasedLineEnable = FALSE;
1403
1404 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1405 ASSERT(SUCCEEDED(result));
1406 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1407
1408 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1409 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1410 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1411 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1412 rsNoScissorDesc.DepthBias = 0;
1413 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1414 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1415 rsNoScissorDesc.DepthClipEnable = FALSE;
1416 rsNoScissorDesc.ScissorEnable = FALSE;
1417 rsNoScissorDesc.MultisampleEnable = FALSE;
1418 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1419
1420 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1421 ASSERT(SUCCEEDED(result));
1422 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1423
1424 mClearResourcesInitialized = true;
1425 }
1426
1427 // Prepare the depth stencil state to write depth values if the depth should be cleared
1428 // and stencil values if the stencil should be cleared
1429 gl::DepthStencilState glDSState;
1430 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1431 glDSState.depthFunc = GL_ALWAYS;
1432 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1433 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1434 glDSState.stencilFunc = GL_ALWAYS;
1435 glDSState.stencilMask = 0;
1436 glDSState.stencilFail = GL_REPLACE;
1437 glDSState.stencilPassDepthFail = GL_REPLACE;
1438 glDSState.stencilPassDepthPass = GL_REPLACE;
1439 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1440 glDSState.stencilBackFunc = GL_ALWAYS;
1441 glDSState.stencilBackMask = 0;
1442 glDSState.stencilBackFail = GL_REPLACE;
1443 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1444 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1445 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1446
1447 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1448
1449 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1450
1451 // Prepare the blend state to use a write mask if the color buffer should be cleared
1452 gl::BlendState glBlendState;
1453 glBlendState.blend = false;
1454 glBlendState.sourceBlendRGB = GL_ONE;
1455 glBlendState.destBlendRGB = GL_ZERO;
1456 glBlendState.sourceBlendAlpha = GL_ONE;
1457 glBlendState.destBlendAlpha = GL_ZERO;
1458 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1459 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1460 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1461 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1462 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1463 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
1464 glBlendState.sampleAlphaToCoverage = false;
1465 glBlendState.dither = false;
1466
1467 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1468 static const UINT sampleMask = 0xFFFFFFFF;
1469
1470 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1471
1472 // Set the vertices
1473 D3D11_MAPPED_SUBRESOURCE mappedResource;
1474 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1475 if (FAILED(result))
1476 {
1477 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1478 return;
1479 }
1480
1481 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1482
1483 float depthClear = gl::clamp01(clearParams.depthClearValue);
1484 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1485 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1486 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1487 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1488
1489 mDeviceContext->Unmap(mClearVB, 0);
1490
1491 // Apply state
1492 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1493 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1494 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1495
1496 // Apply shaders
1497 mDeviceContext->IASetInputLayout(mClearIL);
1498 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1499 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
1500
1501 // Apply vertex buffer
1502 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1503 static UINT startIdx = 0;
1504 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1505 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1506
1507 // Draw the clear quad
1508 mDeviceContext->Draw(4, 0);
1509
1510 // Clean up
1511 markAllStateDirty();
1512}
1513
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001514void Renderer11::markAllStateDirty()
1515{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001516 mAppliedRenderTargetSerial = 0;
1517 mAppliedDepthbufferSerial = 0;
1518 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001519 mDepthStencilInitialized = false;
1520 mRenderTargetDescInitialized = false;
1521
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001522 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001523 {
1524 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001525 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001526 }
1527 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1528 {
1529 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001530 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001531 }
1532
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001533 mForceSetBlendState = true;
1534 mForceSetRasterState = true;
1535 mForceSetDepthStencilState = true;
1536 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001537 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001538
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001539 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001540 mAppliedIBOffset = 0;
1541
daniel@transgaming.come4991412012-12-20 20:55:34 +00001542 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001543 mDxUniformsDirty = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001544}
1545
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001546void Renderer11::releaseDeviceResources()
1547{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001548 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001549 mInputLayoutCache.clear();
1550
1551 delete mVertexDataManager;
1552 mVertexDataManager = NULL;
1553
1554 delete mIndexDataManager;
1555 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001556
1557 delete mLineLoopIB;
1558 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001559
1560 delete mTriangleFanIB;
1561 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001562
1563 if (mCopyVB)
1564 {
1565 mCopyVB->Release();
1566 mCopyVB = NULL;
1567 }
1568
1569 if (mCopySampler)
1570 {
1571 mCopySampler->Release();
1572 mCopySampler = NULL;
1573 }
1574
1575 if (mCopyIL)
1576 {
1577 mCopyIL->Release();
1578 mCopyIL = NULL;
1579 }
1580
1581 if (mCopyVS)
1582 {
1583 mCopyVS->Release();
1584 mCopyVS = NULL;
1585 }
1586
1587 if (mCopyRGBAPS)
1588 {
1589 mCopyRGBAPS->Release();
1590 mCopyRGBAPS = NULL;
1591 }
1592
1593 if (mCopyRGBPS)
1594 {
1595 mCopyRGBPS->Release();
1596 mCopyRGBPS = NULL;
1597 }
1598
1599 if (mCopyLumPS)
1600 {
1601 mCopyLumPS->Release();
1602 mCopyLumPS = NULL;
1603 }
1604
1605 if (mCopyLumAlphaPS)
1606 {
1607 mCopyLumAlphaPS->Release();
1608 mCopyLumAlphaPS = NULL;
1609 }
1610
1611 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001612
1613 if (mClearVB)
1614 {
1615 mClearVB->Release();
1616 mClearVB = NULL;
1617 }
1618
1619 if (mClearIL)
1620 {
1621 mClearIL->Release();
1622 mClearIL = NULL;
1623 }
1624
1625 if (mClearVS)
1626 {
1627 mClearVS->Release();
1628 mClearVS = NULL;
1629 }
1630
1631 if (mClearPS)
1632 {
1633 mClearPS->Release();
1634 mClearPS = NULL;
1635 }
1636
1637 if (mClearScissorRS)
1638 {
1639 mClearScissorRS->Release();
1640 mClearScissorRS = NULL;
1641 }
1642
1643 if (mClearNoScissorRS)
1644 {
1645 mClearNoScissorRS->Release();
1646 mClearNoScissorRS = NULL;
1647 }
1648
1649 mClearResourcesInitialized = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001650}
1651
1652void Renderer11::markDeviceLost()
1653{
1654 mDeviceLost = true;
1655}
1656
1657bool Renderer11::isDeviceLost()
1658{
1659 return mDeviceLost;
1660}
1661
1662// set notify to true to broadcast a message to all contexts of the device loss
1663bool Renderer11::testDeviceLost(bool notify)
1664{
1665 bool isLost = false;
1666
1667 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001668 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001669
1670 if (isLost)
1671 {
1672 // ensure we note the device loss --
1673 // we'll probably get this done again by markDeviceLost
1674 // but best to remember it!
1675 // Note that we don't want to clear the device loss status here
1676 // -- this needs to be done by resetDevice
1677 mDeviceLost = true;
1678 if (notify)
1679 {
1680 mDisplay->notifyDeviceLost();
1681 }
1682 }
1683
1684 return isLost;
1685}
1686
1687bool Renderer11::testDeviceResettable()
1688{
1689 HRESULT status = D3D_OK;
1690
1691 // TODO
1692 UNIMPLEMENTED();
1693
1694 switch (status)
1695 {
1696 case D3DERR_DEVICENOTRESET:
1697 case D3DERR_DEVICEHUNG:
1698 return true;
1699 default:
1700 return false;
1701 }
1702}
1703
1704bool Renderer11::resetDevice()
1705{
1706 releaseDeviceResources();
1707
1708 // TODO
1709 UNIMPLEMENTED();
1710
1711 // reset device defaults
1712 initializeDevice();
1713 mDeviceLost = false;
1714
1715 return true;
1716}
1717
1718DWORD Renderer11::getAdapterVendor() const
1719{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001720 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001721}
1722
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001723std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001724{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001725 std::ostringstream rendererString;
1726
1727 rendererString << mDescription;
1728 rendererString << " Direct3D11";
1729
1730 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1731 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1732
1733 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001734}
1735
1736GUID Renderer11::getAdapterIdentifier() const
1737{
1738 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001739 // UNIMPLEMENTED();
1740 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001741 return foo;
1742}
1743
1744bool Renderer11::getDXT1TextureSupport()
1745{
1746 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001747 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001748 return false;
1749}
1750
1751bool Renderer11::getDXT3TextureSupport()
1752{
1753 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001754 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001755 return false;
1756}
1757
1758bool Renderer11::getDXT5TextureSupport()
1759{
1760 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001761 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001762 return false;
1763}
1764
1765bool Renderer11::getDepthTextureSupport() const
1766{
1767 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001768 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001769 return false;
1770}
1771
1772bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1773{
1774 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001775 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001776
1777 *filtering = false;
1778 *renderable = false;
1779 return false;
1780}
1781
1782bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1783{
1784 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001785 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001786
1787 *filtering = false;
1788 *renderable = false;
1789 return false;
1790}
1791
1792bool Renderer11::getLuminanceTextureSupport()
1793{
1794 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001795 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001796 return false;
1797}
1798
1799bool Renderer11::getLuminanceAlphaTextureSupport()
1800{
1801 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001802 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001803 return false;
1804}
1805
1806bool Renderer11::getTextureFilterAnisotropySupport() const
1807{
1808 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001809 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810 return false;
1811}
1812
1813float Renderer11::getTextureMaxAnisotropy() const
1814{
1815 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001816 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817 return 1.0f;
1818}
1819
1820bool Renderer11::getEventQuerySupport()
1821{
1822 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001823 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001824 return false;
1825}
1826
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001827unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001828{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001829 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1830 switch (mFeatureLevel)
1831 {
1832 case D3D_FEATURE_LEVEL_11_0:
1833 case D3D_FEATURE_LEVEL_10_1:
1834 case D3D_FEATURE_LEVEL_10_0:
1835 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1836 default: UNREACHABLE();
1837 return 0;
1838 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001839}
1840
1841bool Renderer11::getNonPower2TextureSupport() const
1842{
1843 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001844 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001845 return false;
1846}
1847
1848bool Renderer11::getOcclusionQuerySupport() const
1849{
1850 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001851 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001852 return false;
1853}
1854
1855bool Renderer11::getInstancingSupport() const
1856{
1857 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001858 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001859 return false;
1860}
1861
1862bool Renderer11::getShareHandleSupport() const
1863{
1864 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001865 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001866
1867 // PIX doesn't seem to support using share handles, so disable them.
1868 return false && !gl::perfActive();
1869}
1870
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001871bool Renderer11::getDerivativeInstructionSupport() const
1872{
1873 // TODO
1874 // UNIMPLEMENTED();
1875 return false;
1876}
1877
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001878int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001879{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001880 switch (mFeatureLevel)
1881 {
1882 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001883 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001884 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1885 default: UNREACHABLE(); return 0;
1886 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001887}
1888
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001889int Renderer11::getMinorShaderModel() const
1890{
1891 switch (mFeatureLevel)
1892 {
1893 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1894 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1895 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1896 default: UNREACHABLE(); return 0;
1897 }
1898}
1899
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001900float Renderer11::getMaxPointSize() const
1901{
1902 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001903 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904 return 1.0f;
1905}
1906
1907int Renderer11::getMaxTextureWidth() const
1908{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001909 switch (mFeatureLevel)
1910 {
1911 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1912 case D3D_FEATURE_LEVEL_10_1:
1913 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1914 default: UNREACHABLE(); return 0;
1915 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001916}
1917
1918int Renderer11::getMaxTextureHeight() const
1919{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001920 switch (mFeatureLevel)
1921 {
1922 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1923 case D3D_FEATURE_LEVEL_10_1:
1924 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1925 default: UNREACHABLE(); return 0;
1926 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927}
1928
1929bool Renderer11::get32BitIndexSupport() const
1930{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001931 switch (mFeatureLevel)
1932 {
1933 case D3D_FEATURE_LEVEL_11_0:
1934 case D3D_FEATURE_LEVEL_10_1:
1935 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1936 default: UNREACHABLE(); return false;
1937 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001938}
1939
1940int Renderer11::getMinSwapInterval() const
1941{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001942 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001943}
1944
1945int Renderer11::getMaxSwapInterval() const
1946{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001947 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001948}
1949
1950int Renderer11::getMaxSupportedSamples() const
1951{
1952 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001953 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001954 return 1;
1955}
1956
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001957bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001958{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001959 if (source && dest)
1960 {
1961 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1962 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1963
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001964 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001965 return true;
1966 }
1967
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001968 return false;
1969}
1970
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001971bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001972{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001973 if (source && dest)
1974 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001975 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1976 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001977
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001978 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001979 return true;
1980 }
1981
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001982 return false;
1983}
1984
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00001985bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001986 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001987{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001988 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1989 if (!colorbuffer)
1990 {
1991 ERR("Failed to retrieve the color buffer from the frame buffer.");
1992 return error(GL_OUT_OF_MEMORY, false);
1993 }
1994
1995 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1996 if (!sourceRenderTarget)
1997 {
1998 ERR("Failed to retrieve the render target from the frame buffer.");
1999 return error(GL_OUT_OF_MEMORY, false);
2000 }
2001
2002 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2003 if (!source)
2004 {
2005 ERR("Failed to retrieve the render target view from the render target.");
2006 return error(GL_OUT_OF_MEMORY, false);
2007 }
2008
2009 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2010 if (!storage11)
2011 {
2012 source->Release();
2013 ERR("Failed to retrieve the texture storage from the destination.");
2014 return error(GL_OUT_OF_MEMORY, false);
2015 }
2016
2017 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2018 if (!destRenderTarget)
2019 {
2020 source->Release();
2021 ERR("Failed to retrieve the render target from the destination storage.");
2022 return error(GL_OUT_OF_MEMORY, false);
2023 }
2024
2025 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2026 if (!dest)
2027 {
2028 source->Release();
2029 ERR("Failed to retrieve the render target view from the destination render target.");
2030 return error(GL_OUT_OF_MEMORY, false);
2031 }
2032
2033 gl::Rectangle destRect;
2034 destRect.x = xoffset;
2035 destRect.y = yoffset;
2036 destRect.width = sourceRect.width;
2037 destRect.height = sourceRect.height;
2038
2039 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2040 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2041
2042 source->Release();
2043 dest->Release();
2044
2045 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002046}
2047
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002048bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002049 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002050{
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002051 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2052 if (!colorbuffer)
2053 {
2054 ERR("Failed to retrieve the color buffer from the frame buffer.");
2055 return error(GL_OUT_OF_MEMORY, false);
2056 }
2057
2058 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2059 if (!sourceRenderTarget)
2060 {
2061 ERR("Failed to retrieve the render target from the frame buffer.");
2062 return error(GL_OUT_OF_MEMORY, false);
2063 }
2064
2065 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2066 if (!source)
2067 {
2068 ERR("Failed to retrieve the render target view from the render target.");
2069 return error(GL_OUT_OF_MEMORY, false);
2070 }
2071
2072 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2073 if (!storage11)
2074 {
2075 source->Release();
2076 ERR("Failed to retrieve the texture storage from the destination.");
2077 return error(GL_OUT_OF_MEMORY, false);
2078 }
2079
2080 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2081 if (!destRenderTarget)
2082 {
2083 source->Release();
2084 ERR("Failed to retrieve the render target from the destination storage.");
2085 return error(GL_OUT_OF_MEMORY, false);
2086 }
2087
2088 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2089 if (!dest)
2090 {
2091 source->Release();
2092 ERR("Failed to retrieve the render target view from the destination render target.");
2093 return error(GL_OUT_OF_MEMORY, false);
2094 }
2095
2096 gl::Rectangle destRect;
2097 destRect.x = xoffset;
2098 destRect.y = yoffset;
2099 destRect.width = sourceRect.width;
2100 destRect.height = sourceRect.height;
2101
2102 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2103 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2104
2105 source->Release();
2106 dest->Release();
2107
2108 return ret;
2109}
2110
2111bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2112 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2113{
2114 HRESULT result;
2115
2116 if (!mCopyResourcesInitialized)
2117 {
2118 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2119
2120 D3D11_BUFFER_DESC vbDesc;
2121 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2122 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2123 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2124 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2125 vbDesc.MiscFlags = 0;
2126 vbDesc.StructureByteStride = 0;
2127
2128 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2129 ASSERT(SUCCEEDED(result));
2130 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2131
2132 D3D11_SAMPLER_DESC samplerDesc;
2133 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2134 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2135 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2136 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2137 samplerDesc.MipLODBias = 0.0f;
2138 samplerDesc.MaxAnisotropy = 0;
2139 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2140 samplerDesc.BorderColor[0] = 0.0f;
2141 samplerDesc.BorderColor[1] = 0.0f;
2142 samplerDesc.BorderColor[2] = 0.0f;
2143 samplerDesc.BorderColor[3] = 0.0f;
2144 samplerDesc.MinLOD = 0.0f;
2145 samplerDesc.MaxLOD = 0.0f;
2146
2147 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2148 ASSERT(SUCCEEDED(result));
2149 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2150
2151 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2152 {
2153 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2154 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2155 };
2156
2157 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2158 ASSERT(SUCCEEDED(result));
2159 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2160
2161 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2162 ASSERT(SUCCEEDED(result));
2163 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2164
2165 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2166 ASSERT(SUCCEEDED(result));
2167 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2168
2169 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2170 ASSERT(SUCCEEDED(result));
2171 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2172
2173 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2174 ASSERT(SUCCEEDED(result));
2175 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2176
2177 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2178 ASSERT(SUCCEEDED(result));
2179 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2180
2181 mCopyResourcesInitialized = true;
2182 }
2183
2184 // Verify the source and destination area sizes
2185 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2186 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2187 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2188 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2189 {
2190 return error(GL_INVALID_VALUE, false);
2191 }
2192
2193 // Set vertices
2194 D3D11_MAPPED_SUBRESOURCE mappedResource;
2195 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2196 if (FAILED(result))
2197 {
2198 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
2199 return error(GL_OUT_OF_MEMORY, false);
2200 }
2201
2202 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2203
2204 // Create a quad in homogeneous coordinates
2205 float x1 = (destArea.x / destWidth) * 2.0f - 1.0f;
2206 float y1 = (destArea.y / destHeight) * 2.0f - 1.0f;
2207 float x2 = ((destArea.x + destArea.width) / destWidth) * 2.0f - 1.0f;
2208 float y2 = ((destArea.y + destArea.height) / destHeight) * 2.0f - 1.0f;
2209
2210 float u1 = sourceArea.x / float(sourceWidth);
2211 float v1 = sourceArea.y / float(sourceHeight);
2212 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2213 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2214
2215 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2216 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2217 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2218 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2219
2220 mDeviceContext->Unmap(mCopyVB, 0);
2221
2222 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2223 static UINT startIdx = 0;
2224 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2225
2226 // Apply state
2227 static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
2228 mDeviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF);
2229 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2230 mDeviceContext->RSSetState(NULL);
2231
2232 // Apply shaders
2233 mDeviceContext->IASetInputLayout(mCopyIL);
2234 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2235 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2236
2237 ID3D11PixelShader *ps = NULL;
2238 switch(destFormat)
2239 {
2240 case GL_RGBA: ps = mCopyRGBAPS; break;
2241 case GL_RGB: ps = mCopyRGBPS; break;
2242 case GL_ALPHA: ps = mCopyRGBAPS; break;
2243 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2244 case GL_LUMINANCE: ps = mCopyLumPS; break;
2245 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2246 default: UNREACHABLE(); ps = NULL; break;
2247 }
2248
2249 mDeviceContext->PSSetShader(ps, NULL, 0);
2250
2251 // Unset the currently bound shader resource to avoid conflicts
2252 static ID3D11ShaderResourceView *const nullSRV = NULL;
2253 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2254
2255 // Apply render targets
2256 mDeviceContext->OMSetRenderTargets(1, &dest, NULL);
2257
2258 // Set the viewport
2259 D3D11_VIEWPORT viewport;
2260 viewport.TopLeftX = 0;
2261 viewport.TopLeftY = 0;
2262 viewport.Width = destWidth;
2263 viewport.Height = destHeight;
2264 viewport.MinDepth = 0.0f;
2265 viewport.MaxDepth = 1.0f;
2266 mDeviceContext->RSSetViewports(1, &viewport);
2267
2268 // Apply textures
2269 mDeviceContext->PSSetShaderResources(0, 1, &source);
2270 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2271
2272 // Draw the quad
2273 mDeviceContext->Draw(4, 0);
2274
2275 // Unbind textures and render targets and vertex buffer
2276 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2277
2278 static ID3D11RenderTargetView *const nullRTV = NULL;
2279 mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL);
2280
2281 static UINT zero = 0;
2282 static ID3D11Buffer *const nullBuffer = NULL;
2283 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2284
2285 markAllStateDirty();
2286
2287 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002288}
2289
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002290RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2291{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002292 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002293 RenderTarget11 *renderTarget = NULL;
2294 if (depth)
2295 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002296 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL,
2297 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002298 }
2299 else
2300 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002301 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
2302 swapChain11->getRenderTargetShaderResource(),
2303 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002304 }
2305 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002306}
2307
2308RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2309{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002310 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2311 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002312}
2313
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002314ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002315{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002316 ShaderExecutable11 *executable = NULL;
2317
2318 switch (type)
2319 {
2320 case GL_VERTEX_SHADER:
2321 {
2322 ID3D11VertexShader *vshader = NULL;
2323 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2324 ASSERT(SUCCEEDED(result));
2325
2326 if (vshader)
2327 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002328 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002329 }
2330 }
2331 break;
2332 case GL_FRAGMENT_SHADER:
2333 {
2334 ID3D11PixelShader *pshader = NULL;
2335 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2336 ASSERT(SUCCEEDED(result));
2337
2338 if (pshader)
2339 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002340 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002341 }
2342 }
2343 break;
2344 default:
2345 UNREACHABLE();
2346 break;
2347 }
2348
2349 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002350}
2351
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002352ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2353{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002354 const char *profile = NULL;
2355
2356 switch (type)
2357 {
2358 case GL_VERTEX_SHADER:
2359 profile = "vs_4_0";
2360 break;
2361 case GL_FRAGMENT_SHADER:
2362 profile = "ps_4_0";
2363 break;
2364 default:
2365 UNREACHABLE();
2366 return NULL;
2367 }
2368
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002369 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002370 if (!binary)
2371 return NULL;
2372
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002373 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002374 binary->Release();
2375
2376 return executable;
2377}
2378
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002379VertexBuffer *Renderer11::createVertexBuffer()
2380{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002381 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002382}
2383
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002384IndexBuffer *Renderer11::createIndexBuffer()
2385{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002386 return new IndexBuffer11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002387}
2388
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002389bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
2390{
2391 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2392 if (colorbuffer)
2393 {
2394 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2395 if (renderTarget)
2396 {
2397 *subresourceIndex = renderTarget->getSubresourceIndex();
2398
2399 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2400 if (colorBufferRTV)
2401 {
2402 ID3D11Resource *textureResource = NULL;
2403 colorBufferRTV->GetResource(&textureResource);
2404 colorBufferRTV->Release();
2405
2406 if (textureResource)
2407 {
2408 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
2409 textureResource->Release();
2410
2411 if (SUCCEEDED(result))
2412 {
2413 return true;
2414 }
2415 else
2416 {
2417 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2418 "HRESULT: 0x%X.", result);
2419 }
2420 }
2421 }
2422 }
2423 }
2424
2425 return false;
2426}
2427
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002428bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
2429 bool blitRenderTarget, bool blitDepthStencil)
2430{
2431 // TODO
2432 UNIMPLEMENTED();
2433 return false;
2434}
2435
2436void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2437 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2438{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002439 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002440 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002441
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002442 if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002443 {
2444 gl::Rectangle area;
2445 area.x = x;
2446 area.y = y;
2447 area.width = width;
2448 area.height = height;
2449
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002450 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2451 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002452
2453 colorBufferTexture->Release();
2454 colorBufferTexture = NULL;
2455 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002456}
2457
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002458Image *Renderer11::createImage()
2459{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002460 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002461}
2462
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002463void Renderer11::generateMipmap(Image *dest, Image *src)
2464{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002465 Image11 *dest11 = Image11::makeImage11(dest);
2466 Image11 *src11 = Image11::makeImage11(src);
2467 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002468}
2469
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002470TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2471{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002472 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2473 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002474}
2475
2476TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2477{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002478 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002479}
2480
2481TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2482{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002483 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002484}
2485
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002486static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
2487{
2488 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
2489 destFormat == GL_ALPHA &&
2490 destType == GL_UNSIGNED_BYTE)
2491 {
2492 return 1;
2493 }
2494 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
2495 destFormat == GL_RGBA &&
2496 destType == GL_UNSIGNED_BYTE)
2497 {
2498 return 4;
2499 }
2500 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
2501 destFormat == GL_BGRA_EXT &&
2502 destType == GL_UNSIGNED_BYTE)
2503 {
2504 return 4;
2505 }
2506 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
2507 destFormat == GL_RGBA &&
2508 destType == GL_HALF_FLOAT_OES)
2509 {
2510 return 8;
2511 }
2512 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
2513 destFormat == GL_RGB &&
2514 destType == GL_FLOAT)
2515 {
2516 return 12;
2517 }
2518 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
2519 destFormat == GL_RGBA &&
2520 destType == GL_FLOAT)
2521 {
2522 return 16;
2523 }
2524 else
2525 {
2526 return 0;
2527 }
2528}
2529
2530static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
2531 unsigned int y, int inputPitch, gl::Color *outColor)
2532{
2533 switch (format)
2534 {
2535 case DXGI_FORMAT_R8G8B8A8_UNORM:
2536 {
2537 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2538 outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
2539 outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
2540 outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
2541 outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
2542 }
2543 break;
2544
2545 case DXGI_FORMAT_A8_UNORM:
2546 {
2547 outColor->red = 0.0f;
2548 outColor->green = 0.0f;
2549 outColor->blue = 0.0f;
2550 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
2551 }
2552 break;
2553
2554 case DXGI_FORMAT_R32G32B32A32_FLOAT:
2555 {
2556 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
2557 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
2558 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
2559 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
2560 }
2561 break;
2562
2563 case DXGI_FORMAT_R32G32B32_FLOAT:
2564 {
2565 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
2566 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
2567 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
2568 outColor->alpha = 1.0f;
2569 }
2570 break;
2571
2572 case DXGI_FORMAT_R16G16B16A16_FLOAT:
2573 {
2574 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
2575 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
2576 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
2577 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
2578 }
2579 break;
2580
2581 case DXGI_FORMAT_B8G8R8A8_UNORM:
2582 {
2583 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
2584 outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
2585 outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
2586 outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
2587 outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
2588 }
2589 break;
2590
2591 case DXGI_FORMAT_R8_UNORM:
2592 {
2593 outColor->red = *(data + x + y * inputPitch) / 255.0f;
2594 outColor->green = 0.0f;
2595 outColor->blue = 0.0f;
2596 outColor->alpha = 1.0f;
2597 }
2598 break;
2599
2600 case DXGI_FORMAT_R8G8_UNORM:
2601 {
2602 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
2603
2604 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
2605 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
2606 outColor->blue = 0.0f;
2607 outColor->alpha = 1.0f;
2608 }
2609 break;
2610
2611 case DXGI_FORMAT_R16_FLOAT:
2612 {
2613 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
2614 outColor->green = 0.0f;
2615 outColor->blue = 0.0f;
2616 outColor->alpha = 1.0f;
2617 }
2618 break;
2619
2620 case DXGI_FORMAT_R16G16_FLOAT:
2621 {
2622 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
2623 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
2624 outColor->blue = 0.0f;
2625 outColor->alpha = 1.0f;
2626 }
2627 break;
2628
2629 default:
2630 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
2631 UNIMPLEMENTED();
2632 break;
2633 }
2634}
2635
2636static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
2637 unsigned int y, int outputPitch, void *outData)
2638{
2639 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
2640 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
2641
2642 switch (format)
2643 {
2644 case GL_RGBA:
2645 switch (type)
2646 {
2647 case GL_UNSIGNED_BYTE:
2648 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2649 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2650 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2651 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2652 break;
2653
2654 default:
2655 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
2656 UNIMPLEMENTED();
2657 break;
2658 }
2659 break;
2660
2661 case GL_BGRA_EXT:
2662 switch (type)
2663 {
2664 case GL_UNSIGNED_BYTE:
2665 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2666 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2667 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
2668 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2669 break;
2670
2671 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2672 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2673 // this type is packed as follows:
2674 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2675 // --------------------------------------------------------------------------------
2676 // | 4th | 3rd | 2nd | 1st component |
2677 // --------------------------------------------------------------------------------
2678 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2679 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2680 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
2681 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
2682 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
2683 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
2684 break;
2685
2686 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2687 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2688 // this type is packed as follows:
2689 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2690 // --------------------------------------------------------------------------------
2691 // | 4th | 3rd | 2nd | 1st component |
2692 // --------------------------------------------------------------------------------
2693 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2694 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2695 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
2696 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
2697 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
2698 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
2699 break;
2700
2701 default:
2702 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
2703 UNIMPLEMENTED();
2704 break;
2705 }
2706 break;
2707
2708 case GL_RGB:
2709 switch (type)
2710 {
2711 case GL_UNSIGNED_SHORT_5_6_5:
2712 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2713 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
2714 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
2715 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
2716 break;
2717
2718 case GL_UNSIGNED_BYTE:
2719 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2720 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2721 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2722 break;
2723
2724 default:
2725 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
2726 UNIMPLEMENTED();
2727 break;
2728 }
2729 break;
2730
2731 default:
2732 ERR("WritePixelColor not implemented for format 0x%X.", format);
2733 UNIMPLEMENTED();
2734 break;
2735 }
2736}
2737
2738void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2739 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2740 GLint packAlignment, void *pixels)
2741{
2742 D3D11_TEXTURE2D_DESC textureDesc;
2743 texture->GetDesc(&textureDesc);
2744
2745 D3D11_TEXTURE2D_DESC stagingDesc;
2746 stagingDesc.Width = area.width;
2747 stagingDesc.Height = area.height;
2748 stagingDesc.MipLevels = 1;
2749 stagingDesc.ArraySize = 1;
2750 stagingDesc.Format = textureDesc.Format;
2751 stagingDesc.SampleDesc.Count = 1;
2752 stagingDesc.SampleDesc.Quality = 0;
2753 stagingDesc.Usage = D3D11_USAGE_STAGING;
2754 stagingDesc.BindFlags = 0;
2755 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2756 stagingDesc.MiscFlags = 0;
2757
2758 ID3D11Texture2D* stagingTex = NULL;
2759 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2760 if (FAILED(result))
2761 {
2762 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2763 return;
2764 }
2765
2766 ID3D11Texture2D* srcTex = NULL;
2767 if (textureDesc.SampleDesc.Count > 1)
2768 {
2769 D3D11_TEXTURE2D_DESC resolveDesc;
2770 resolveDesc.Width = textureDesc.Width;
2771 resolveDesc.Height = textureDesc.Height;
2772 resolveDesc.MipLevels = 1;
2773 resolveDesc.ArraySize = 1;
2774 resolveDesc.Format = textureDesc.Format;
2775 resolveDesc.SampleDesc.Count = 1;
2776 resolveDesc.SampleDesc.Quality = 0;
2777 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2778 resolveDesc.BindFlags = 0;
2779 resolveDesc.CPUAccessFlags = 0;
2780 resolveDesc.MiscFlags = 0;
2781
2782 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2783 if (FAILED(result))
2784 {
2785 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
2786 stagingTex->Release();
2787 return;
2788 }
2789
2790 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2791 subResource = 0;
2792 }
2793 else
2794 {
2795 srcTex = texture;
2796 srcTex->AddRef();
2797 }
2798
2799 D3D11_BOX srcBox;
2800 srcBox.left = area.x;
2801 srcBox.right = area.x + area.width;
2802 srcBox.top = area.y;
2803 srcBox.bottom = area.y + area.height;
2804 srcBox.front = 0;
2805 srcBox.back = 1;
2806
2807 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2808
2809 srcTex->Release();
2810 srcTex = NULL;
2811
2812 D3D11_MAPPED_SUBRESOURCE mapping;
2813 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
2814
2815 unsigned char *source;
2816 int inputPitch;
2817 if (packReverseRowOrder)
2818 {
2819 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
2820 inputPitch = -static_cast<int>(mapping.RowPitch);
2821 }
2822 else
2823 {
2824 source = static_cast<unsigned char*>(mapping.pData);
2825 inputPitch = static_cast<int>(mapping.RowPitch);
2826 }
2827
2828 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
2829 if (fastPixelSize != 0)
2830 {
2831 unsigned char *dest = static_cast<unsigned char*>(pixels);
2832 for (int j = 0; j < area.height; j++)
2833 {
2834 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
2835 }
2836 }
2837 else
2838 {
2839 gl::Color pixelColor;
2840 for (int j = 0; j < area.height; j++)
2841 {
2842 for (int i = 0; i < area.width; i++)
2843 {
2844 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
2845 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
2846 }
2847 }
2848 }
2849
2850 mDeviceContext->Unmap(stagingTex, 0);
2851
2852 stagingTex->Release();
2853 stagingTex = NULL;
2854}
2855
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002856}