blob: d215e4bbdf149db06e317dc99c63fcfcae6bc812 [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
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000029#include <sstream>
30
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000031namespace rx
32{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000033static const DXGI_FORMAT RenderTargetFormats[] =
34 {
35 DXGI_FORMAT_R8G8B8A8_UNORM
36 };
37
38static const DXGI_FORMAT DepthStencilFormats[] =
39 {
40 DXGI_FORMAT_D24_UNORM_S8_UINT
41 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000042
43Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
44{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000045 mVertexDataManager = NULL;
46 mIndexDataManager = NULL;
47
daniel@transgaming.comc5114302012-12-20 21:11:36 +000048 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000049 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000050
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000051 mD3d11Module = NULL;
52 mDxgiModule = NULL;
53
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000054 mDeviceLost = false;
55
daniel@transgaming.com25072f62012-11-28 19:31:32 +000056 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000057 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000058 mDxgiAdapter = NULL;
59 mDxgiFactory = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000060}
61
62Renderer11::~Renderer11()
63{
64 releaseDeviceResources();
65
daniel@transgaming.com65e65372012-11-28 19:33:50 +000066 if (mDxgiFactory)
67 {
68 mDxgiFactory->Release();
69 mDxgiFactory = NULL;
70 }
71
72 if (mDxgiAdapter)
73 {
74 mDxgiAdapter->Release();
75 mDxgiAdapter = NULL;
76 }
77
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000078 if (mDeviceContext)
79 {
daniel@transgaming.comd5df4e82013-01-11 04:11:21 +000080 mDeviceContext->ClearState();
81 mDeviceContext->Flush();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000082 mDeviceContext->Release();
83 mDeviceContext = NULL;
84 }
85
daniel@transgaming.com25072f62012-11-28 19:31:32 +000086 if (mDevice)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000087 {
daniel@transgaming.com25072f62012-11-28 19:31:32 +000088 mDevice->Release();
89 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000090 }
91
92 if (mD3d11Module)
93 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000094 FreeLibrary(mD3d11Module);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000095 mD3d11Module = NULL;
96 }
97
98 if (mDxgiModule)
99 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000100 FreeLibrary(mDxgiModule);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000101 mDxgiModule = NULL;
102 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000103}
104
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000105Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
106{
107 ASSERT(dynamic_cast<rx::Renderer11*>(renderer) != NULL);
108 return static_cast<rx::Renderer11*>(renderer);
109}
110
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000111EGLint Renderer11::initialize()
112{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000113 if (!initializeCompiler())
114 {
115 return EGL_NOT_INITIALIZED;
116 }
117
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000118 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
119 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000120
121 if (mD3d11Module == NULL || mDxgiModule == NULL)
122 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000123 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000124 return EGL_NOT_INITIALIZED;
125 }
126
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000127 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000128
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000129 if (D3D11CreateDevice == NULL)
130 {
131 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
132 return EGL_NOT_INITIALIZED;
133 }
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000134
135 D3D_FEATURE_LEVEL featureLevel[] =
136 {
137 D3D_FEATURE_LEVEL_11_0,
138 D3D_FEATURE_LEVEL_10_1,
139 D3D_FEATURE_LEVEL_10_0,
140 };
141
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000142 HRESULT result = D3D11CreateDevice(NULL,
143 D3D_DRIVER_TYPE_HARDWARE,
144 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000145 #if defined(_DEBUG)
146 D3D11_CREATE_DEVICE_DEBUG,
147 #else
148 0,
149 #endif
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000150 featureLevel,
151 sizeof(featureLevel)/sizeof(featureLevel[0]),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000152 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000153 &mDevice,
154 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000155 &mDeviceContext);
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000156
157 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000158 {
159 ERR("Could not create D3D11 device - aborting!\n");
160 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
161 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000162
163 IDXGIDevice *dxgiDevice = NULL;
164 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
165
166 if (FAILED(result))
167 {
168 ERR("Could not query DXGI device - aborting!\n");
169 return EGL_NOT_INITIALIZED;
170 }
171
172 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
173
174 if (FAILED(result))
175 {
176 ERR("Could not retrieve DXGI adapter - aborting!\n");
177 return EGL_NOT_INITIALIZED;
178 }
179
180 dxgiDevice->Release();
181
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000182 mDxgiAdapter->GetDesc(&mAdapterDescription);
183 memset(mDescription, 0, sizeof(mDescription));
184 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
185
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000186 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
187
188 if (!mDxgiFactory || FAILED(result))
189 {
190 ERR("Could not create DXGI factory - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000194 initializeDevice();
195
196 return EGL_SUCCESS;
197}
198
199// do any one-time device initialization
200// NOTE: this is also needed after a device lost/reset
201// to reset the scene status and ensure the default states are reset.
202void Renderer11::initializeDevice()
203{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000204 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000205 mInputLayoutCache.initialize(mDevice, mDeviceContext);
206
207 ASSERT(!mVertexDataManager && !mIndexDataManager);
208 mVertexDataManager = new VertexDataManager(this);
209 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000210
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000211 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000212}
213
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000214int Renderer11::generateConfigs(ConfigDesc **configDescList)
215{
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000216 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
217 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000218 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
219 int numConfigs = 0;
220
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000221 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000222 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000223 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000224 {
225 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
226
227 UINT formatSupport = 0;
228 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
229
230 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
231 {
232 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
233
234 UINT formatSupport = 0;
235 HRESULT result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
236
237 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL))
238 {
239 ConfigDesc newConfig;
240 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
241 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
242 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
243 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
244
245 (*configDescList)[numConfigs++] = newConfig;
246 }
247 }
248 }
249 }
250
251 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000252}
253
254void Renderer11::deleteConfigs(ConfigDesc *configDescList)
255{
256 delete [] (configDescList);
257}
258
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000259void Renderer11::sync(bool block)
260{
261 // TODO
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000262 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000263}
264
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000265SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
266{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000267 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000268}
269
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000270void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
271{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000272 if (type == gl::SAMPLER_PIXEL)
273 {
274 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
275 {
276 ERR("Pixel shader sampler index %i is not valid.", index);
277 return;
278 }
279
280 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
281 {
282 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
283
284 if (!dxSamplerState)
285 {
286 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
287 "sampler state for pixel shaders at slot %i.", index);
288 }
289
290 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
291
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000292 mCurPixelSamplerStates[index] = samplerState;
293 }
294
295 mForceSetPixelSamplerStates[index] = false;
296 }
297 else if (type == gl::SAMPLER_VERTEX)
298 {
299 if (index < 0 || index >= gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
300 {
301 ERR("Vertex shader sampler index %i is not valid.", index);
302 return;
303 }
304
305 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
306 {
307 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
308
309 if (!dxSamplerState)
310 {
311 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
312 "sampler state for vertex shaders at slot %i.", index);
313 }
314
315 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
316
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000317 mCurVertexSamplerStates[index] = samplerState;
318 }
319
320 mForceSetVertexSamplerStates[index] = false;
321 }
322 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000323}
324
325void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
326{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000327 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000328 unsigned int serial = 0;
329 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000330
331 if (texture)
332 {
333 TextureStorageInterface *texStorage = texture->getNativeTexture();
334 if (texStorage)
335 {
336 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
337 textureSRV = storage11->getSRV();
338 }
339
340 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
341 // missing the shader resource view
342 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000343
344 serial = texture->getTextureSerial();
345 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000346 }
347
348 if (type == gl::SAMPLER_PIXEL)
349 {
350 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
351 {
352 ERR("Pixel shader sampler index %i is not valid.", index);
353 return;
354 }
355
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000356 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
357 {
358 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
359 }
360
361 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000362 }
363 else if (type == gl::SAMPLER_VERTEX)
364 {
365 if (index < 0 || index >= gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
366 {
367 ERR("Vertex shader sampler index %i is not valid.", index);
368 return;
369 }
370
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000371 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
372 {
373 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
374 }
375
376 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000377 }
378 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000379}
380
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000381void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000382{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000383 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000384 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000385 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
386 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000387 if (!dxRasterState)
388 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000389 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000390 "rasterizer state.");
391 }
392
393 mDeviceContext->RSSetState(dxRasterState);
394
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000395 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000396 }
397
398 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000399}
400
401void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
402 unsigned int sampleMask)
403{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000404 if (mForceSetBlendState ||
405 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
406 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
407 sampleMask != mCurSampleMask)
408 {
409 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
410 if (!dxBlendState)
411 {
412 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
413 "blend state.");
414 }
415
416 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
417 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
418
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000419 mCurBlendState = blendState;
420 mCurBlendColor = blendColor;
421 mCurSampleMask = sampleMask;
422 }
423
424 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000425}
426
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000427void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000428 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000429{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000430 if (mForceSetDepthStencilState ||
431 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
432 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
433 {
434 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
435 stencilRef != stencilBackRef ||
436 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
437 {
438 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
439 "invalid under WebGL.");
440 return error(GL_INVALID_OPERATION);
441 }
442
443 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
444 if (!dxDepthStencilState)
445 {
446 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
447 "setting the default depth stencil state.");
448 }
449
450 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
451
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000452 mCurDepthStencilState = depthStencilState;
453 mCurStencilRef = stencilRef;
454 mCurStencilBackRef = stencilBackRef;
455 }
456
457 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000458}
459
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000460void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000461{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000462 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
463 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000464 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000465 if (enabled)
466 {
467 D3D11_RECT rect;
468 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
469 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
470 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
471 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000472
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000473 mDeviceContext->RSSetScissorRects(1, &rect);
474 }
475
476 if (enabled != mScissorEnabled)
477 {
478 mForceSetRasterState = true;
479 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000480
481 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000482 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000483 }
484
485 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000486}
487
daniel@transgaming.com12985182012-12-20 20:56:31 +0000488bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000489 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000490{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000491 gl::Rectangle actualViewport = viewport;
492 float actualZNear = gl::clamp01(zNear);
493 float actualZFar = gl::clamp01(zFar);
494 if (ignoreViewport)
495 {
496 actualViewport.x = 0;
497 actualViewport.y = 0;
498 actualViewport.width = mRenderTargetDesc.width;
499 actualViewport.height = mRenderTargetDesc.height;
500 actualZNear = 0.0f;
501 actualZFar = 1.0f;
502 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000503
504 D3D11_VIEWPORT dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000505 dxViewport.TopLeftX = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
506 dxViewport.TopLeftY = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
507 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.TopLeftX));
508 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.TopLeftY));
509 dxViewport.MinDepth = actualZNear;
510 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000511
512 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
513 {
514 return false; // Nothing to render
515 }
516
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000517 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
518 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000519
daniel@transgaming.com53670042012-11-28 20:55:51 +0000520 if (viewportChanged)
521 {
522 mDeviceContext->RSSetViewports(1, &dxViewport);
523
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000524 mCurViewport = actualViewport;
525 mCurNear = actualZNear;
526 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000527 }
528
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000529 if (currentProgram)
daniel@transgaming.com53670042012-11-28 20:55:51 +0000530 {
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000531 dx_VertexConstants vc = {0};
532 dx_PixelConstants pc = {0};
daniel@transgaming.com53670042012-11-28 20:55:51 +0000533
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000534 vc.halfPixelSize[0] = 0.0f;
535 vc.halfPixelSize[1] = 0.0f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000536
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000537 pc.coord[0] = actualViewport.width * 0.5f;
538 pc.coord[1] = actualViewport.height * 0.5f;
539 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
540 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000541
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000542 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
543 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
544 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
545
546 vc.depthRange[0] = actualZNear;
547 vc.depthRange[1] = actualZFar;
548 vc.depthRange[2] = actualZFar - actualZNear;
549
550 pc.depthRange[0] = actualZNear;
551 pc.depthRange[1] = actualZFar;
552 pc.depthRange[2] = actualZFar - actualZNear;
553
554 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
555 {
556 mVertexConstants = vc;
557 mDxUniformsDirty = true;
558 }
559
560 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
561 {
562 mPixelConstants = pc;
563 mDxUniformsDirty = true;
564 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000565 }
566
567 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000568 return true;
569}
570
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000571bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
572{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000573 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000574
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000575 switch (mode)
576 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000577 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
578 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000579 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000580 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
581 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
582 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000583 // emulate fans via rewriting index buffer
584 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000585 default:
586 return error(GL_INVALID_ENUM, false);
587 }
588
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000589 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000590
591 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000592}
593
594bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000595{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000596 // Get the color render buffer and serial
597 gl::Renderbuffer *renderbufferObject = NULL;
598 unsigned int renderTargetSerial = 0;
599 if (framebuffer->getColorbufferType() != GL_NONE)
600 {
601 renderbufferObject = framebuffer->getColorbuffer();
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000603 if (!renderbufferObject)
604 {
605 ERR("render target pointer unexpectedly null.");
daniel@transgaming.come9c71b42012-11-28 21:02:23 +0000606 return false;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000607 }
608
609 renderTargetSerial = renderbufferObject->getSerial();
610 }
611
612 // Get the depth stencil render buffer and serials
613 gl::Renderbuffer *depthStencil = NULL;
614 unsigned int depthbufferSerial = 0;
615 unsigned int stencilbufferSerial = 0;
616 if (framebuffer->getDepthbufferType() != GL_NONE)
617 {
618 depthStencil = framebuffer->getDepthbuffer();
619 if (!depthStencil)
620 {
621 ERR("Depth stencil pointer unexpectedly null.");
622 return false;
623 }
624
625 depthbufferSerial = depthStencil->getSerial();
626 }
627 else if (framebuffer->getStencilbufferType() != GL_NONE)
628 {
629 depthStencil = framebuffer->getStencilbuffer();
630 if (!depthStencil)
631 {
632 ERR("Depth stencil pointer unexpectedly null.");
633 return false;
634 }
635
636 stencilbufferSerial = depthStencil->getSerial();
637 }
638
639 // Extract the render target dimensions and view
640 unsigned int renderTargetWidth = 0;
641 unsigned int renderTargetHeight = 0;
642 GLenum renderTargetFormat = 0;
643 ID3D11RenderTargetView* framebufferRTV = NULL;
644 if (renderbufferObject)
645 {
646 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
647 if (!renderTarget)
648 {
649 ERR("render target pointer unexpectedly null.");
650 return false;
651 }
652
653 framebufferRTV = renderTarget->getRenderTargetView();
654 if (!framebufferRTV)
655 {
656 ERR("render target view pointer unexpectedly null.");
657 return false;
658 }
659
660 renderTargetWidth = renderbufferObject->getWidth();
661 renderTargetHeight = renderbufferObject->getHeight();
662 renderTargetFormat = renderbufferObject->getActualFormat();
663 }
664
665 // Extract the depth stencil sizes and view
666 unsigned int depthSize = 0;
667 unsigned int stencilSize = 0;
668 ID3D11DepthStencilView* framebufferDSV = NULL;
669 if (depthStencil)
670 {
671 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
672 if (!depthStencilRenderTarget)
673 {
674 ERR("render target pointer unexpectedly null.");
675 if (framebufferRTV)
676 {
677 framebufferRTV->Release();
678 }
679 return false;
680 }
681
682 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
683 if (!framebufferDSV)
684 {
685 ERR("depth stencil view pointer unexpectedly null.");
686 if (framebufferRTV)
687 {
688 framebufferRTV->Release();
689 }
690 return false;
691 }
692
693 // If there is no render buffer, the width, height and format values come from
694 // the depth stencil
695 if (!renderbufferObject)
696 {
697 renderTargetWidth = depthStencil->getWidth();
698 renderTargetHeight = depthStencil->getHeight();
699 renderTargetFormat = depthStencil->getActualFormat();
700 }
701
702 depthSize = depthStencil->getDepthSize();
703 stencilSize = depthStencil->getStencilSize();
704 }
705
706 // Apply the render target and depth stencil
707 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
708 renderTargetSerial != mAppliedRenderTargetSerial ||
709 depthbufferSerial != mAppliedDepthbufferSerial ||
710 stencilbufferSerial != mAppliedStencilbufferSerial)
711 {
712 mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV);
713
714 mRenderTargetDesc.width = renderTargetWidth;
715 mRenderTargetDesc.height = renderTargetHeight;
716 mRenderTargetDesc.format = renderTargetFormat;
717 mForceSetViewport = true; // TODO: It may not be required to clamp the viewport in D3D11
718 mForceSetScissor = true; // TODO: It may not be required to clamp the scissor in D3D11
719
720 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
721 {
722 mCurDepthSize = depthSize;
723 mForceSetRasterState = true;
724 }
725
726 mCurStencilSize = stencilSize;
727
728 mAppliedRenderTargetSerial = renderTargetSerial;
729 mAppliedDepthbufferSerial = depthbufferSerial;
730 mAppliedStencilbufferSerial = stencilbufferSerial;
731 mRenderTargetDescInitialized = true;
732 mDepthStencilInitialized = true;
733 }
734
735 if (framebufferRTV)
736 {
737 framebufferRTV->Release();
738 }
739 if (framebufferDSV)
740 {
741 framebufferDSV->Release();
742 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000743
744 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000745}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000746
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000747GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000748{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000749 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
750 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
751 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000752 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000753 return err;
754 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000755
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000756 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000757}
758
daniel@transgaming.com31240482012-11-28 21:06:41 +0000759GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000760{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000761 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000762
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000763 if (err == GL_NO_ERROR)
764 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000765 if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000766 {
767 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
768
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000769 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000770 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000771 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000772 }
773 }
774
775 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000776}
777
778void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
779{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000780 if (mode == GL_LINE_LOOP)
781 {
782 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
783 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000784 else if (mode == GL_TRIANGLE_FAN)
785 {
786 drawTriangleFan(count, GL_NONE, NULL, 0, NULL);
787 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000788 else if (instances > 0)
789 {
790 // TODO
791 UNIMPLEMENTED();
792 }
793 else
794 {
795 mDeviceContext->Draw(count, 0);
796 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000797}
798
daniel@transgaming.com31240482012-11-28 21:06:41 +0000799void 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 +0000800{
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000801 if (mode == GL_LINE_LOOP)
802 {
803 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
804 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000805 else if (mode == GL_TRIANGLE_FAN)
806 {
807 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
808 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000809 else
810 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +0000811 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000812 }
813}
814
815void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
816{
817 // Get the raw indices for an indexed draw
818 if (type != GL_NONE && elementArrayBuffer)
819 {
820 gl::Buffer *indexBuffer = elementArrayBuffer;
821 intptr_t offset = reinterpret_cast<intptr_t>(indices);
822 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
823 }
824
825 if (!mLineLoopIB)
826 {
827 mLineLoopIB = new StreamingIndexBufferInterface(this);
828 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
829 {
830 delete mLineLoopIB;
831 mLineLoopIB = NULL;
832
833 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
834 return error(GL_OUT_OF_MEMORY);
835 }
836 }
837
838 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
839 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
840 {
841 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
842 return error(GL_OUT_OF_MEMORY);
843 }
844
845 void* mappedMemory = NULL;
846 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
847 if (offset == -1 || mappedMemory == NULL)
848 {
849 ERR("Could not map index buffer for GL_LINE_LOOP.");
850 return error(GL_OUT_OF_MEMORY);
851 }
852
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000853 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000854 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000855
856 switch (type)
857 {
858 case GL_NONE: // Non-indexed draw
859 for (int i = 0; i < count; i++)
860 {
861 data[i] = i;
862 }
863 data[count] = 0;
864 break;
865 case GL_UNSIGNED_BYTE:
866 for (int i = 0; i < count; i++)
867 {
868 data[i] = static_cast<const GLubyte*>(indices)[i];
869 }
870 data[count] = static_cast<const GLubyte*>(indices)[0];
871 break;
872 case GL_UNSIGNED_SHORT:
873 for (int i = 0; i < count; i++)
874 {
875 data[i] = static_cast<const GLushort*>(indices)[i];
876 }
877 data[count] = static_cast<const GLushort*>(indices)[0];
878 break;
879 case GL_UNSIGNED_INT:
880 for (int i = 0; i < count; i++)
881 {
882 data[i] = static_cast<const GLuint*>(indices)[i];
883 }
884 data[count] = static_cast<const GLuint*>(indices)[0];
885 break;
886 default: UNREACHABLE();
887 }
888
889 if (!mLineLoopIB->unmapBuffer())
890 {
891 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
892 return error(GL_OUT_OF_MEMORY);
893 }
894
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000895 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000896 {
897 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
898
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000899 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000900 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000901 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000902 }
903
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000904 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000905}
906
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000907void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
908{
909 // Get the raw indices for an indexed draw
910 if (type != GL_NONE && elementArrayBuffer)
911 {
912 gl::Buffer *indexBuffer = elementArrayBuffer;
913 intptr_t offset = reinterpret_cast<intptr_t>(indices);
914 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
915 }
916
917 if (!mTriangleFanIB)
918 {
919 mTriangleFanIB = new StreamingIndexBufferInterface(this);
920 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
921 {
922 delete mTriangleFanIB;
923 mTriangleFanIB = NULL;
924
925 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
926 return error(GL_OUT_OF_MEMORY);
927 }
928 }
929
930 const int numTris = count - 2;
931 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
932 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
933 {
934 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
935 return error(GL_OUT_OF_MEMORY);
936 }
937
938 void* mappedMemory = NULL;
939 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
940 if (offset == -1 || mappedMemory == NULL)
941 {
942 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
943 return error(GL_OUT_OF_MEMORY);
944 }
945
946 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
947 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
948
949 switch (type)
950 {
951 case GL_NONE: // Non-indexed draw
952 for (int i = 0; i < numTris; i++)
953 {
954 data[i*3 + 0] = 0;
955 data[i*3 + 1] = i + 1;
956 data[i*3 + 2] = i + 2;
957 }
958 break;
959 case GL_UNSIGNED_BYTE:
960 for (int i = 0; i < numTris; i++)
961 {
962 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
963 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
964 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
965 }
966 break;
967 case GL_UNSIGNED_SHORT:
968 for (int i = 0; i < numTris; i++)
969 {
970 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
971 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
972 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
973 }
974 break;
975 case GL_UNSIGNED_INT:
976 for (int i = 0; i < numTris; i++)
977 {
978 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
979 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
980 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
981 }
982 break;
983 default: UNREACHABLE();
984 }
985
986 if (!mTriangleFanIB->unmapBuffer())
987 {
988 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
989 return error(GL_OUT_OF_MEMORY);
990 }
991
992 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
993 {
994 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
995
996 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
997 mAppliedIBSerial = mTriangleFanIB->getSerial();
998 mAppliedIBOffset = indexBufferOffset;
999 }
1000
1001 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1002}
1003
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001004void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1005{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001006 unsigned int programBinarySerial = programBinary->getSerial();
1007 if (programBinarySerial != mAppliedProgramBinarySerial)
1008 {
1009 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1010 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001011
daniel@transgaming.come4991412012-12-20 20:55:34 +00001012 ID3D11VertexShader *vertexShader = NULL;
1013 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001014
daniel@transgaming.come4991412012-12-20 20:55:34 +00001015 ID3D11PixelShader *pixelShader = NULL;
1016 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001017
daniel@transgaming.come4991412012-12-20 20:55:34 +00001018 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1019 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1020 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001021 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001022
1023 mAppliedProgramBinarySerial = programBinarySerial;
1024 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001025}
1026
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001027void Renderer11::applyUniforms(const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001028{
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001029 D3D11_BUFFER_DESC constantBufferDescription = {0};
1030 constantBufferDescription.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1031 constantBufferDescription.Usage = D3D11_USAGE_DYNAMIC;
1032 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1033 constantBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1034 constantBufferDescription.MiscFlags = 0;
1035 constantBufferDescription.StructureByteStride = 0;
1036
1037 ID3D11Buffer *constantBufferVS = NULL;
1038 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferVS);
1039 ASSERT(SUCCEEDED(result));
1040
1041 ID3D11Buffer *constantBufferPS = NULL;
1042 result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferPS);
1043 ASSERT(SUCCEEDED(result));
1044
1045 D3D11_MAPPED_SUBRESOURCE mapVS = {0};
1046 result = mDeviceContext->Map(constantBufferVS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS);
1047 ASSERT(SUCCEEDED(result));
1048
1049 D3D11_MAPPED_SUBRESOURCE mapPS = {0};
1050 result = mDeviceContext->Map(constantBufferPS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS);
1051 ASSERT(SUCCEEDED(result));
1052
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001053 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001054 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001055 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001056
1057 switch (uniform->type)
1058 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001059 case GL_SAMPLER_2D:
1060 case GL_SAMPLER_CUBE:
1061 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001062 case GL_FLOAT:
1063 case GL_FLOAT_VEC2:
1064 case GL_FLOAT_VEC3:
1065 case GL_FLOAT_VEC4:
1066 case GL_FLOAT_MAT2:
1067 case GL_FLOAT_MAT3:
1068 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001069 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001070 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001071 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS.pData;
1072 float (*f)[4] = (float(*)[4])uniform->data;
1073
1074 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001075 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001076 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1077 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1078 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1079 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001080 }
1081 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001082 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001083 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001084 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS.pData;
1085 float (*f)[4] = (float(*)[4])uniform->data;
1086
1087 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001088 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001089 c[uniform->psRegisterIndex + i][0] = f[i][0];
1090 c[uniform->psRegisterIndex + i][1] = f[i][1];
1091 c[uniform->psRegisterIndex + i][2] = f[i][2];
1092 c[uniform->psRegisterIndex + i][3] = f[i][3];
1093 }
1094 }
1095 break;
1096 case GL_INT:
1097 case GL_INT_VEC2:
1098 case GL_INT_VEC3:
1099 case GL_INT_VEC4:
1100 if (uniform->vsRegisterIndex >= 0)
1101 {
1102 int (*c)[4] = (int(*)[4])mapVS.pData;
1103 GLint *x = (GLint*)uniform->data;
1104 int count = gl::VariableColumnCount(uniform->type);
1105
1106 for (unsigned int i = 0; i < uniform->registerCount; i++)
1107 {
1108 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1109 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1110 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1111 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1112 }
1113 }
1114 if (uniform->psRegisterIndex >= 0)
1115 {
1116 int (*c)[4] = (int(*)[4])mapPS.pData;
1117 GLint *x = (GLint*)uniform->data;
1118 int count = gl::VariableColumnCount(uniform->type);
1119
1120 for (unsigned int i = 0; i < uniform->registerCount; i++)
1121 {
1122 if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0];
1123 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1124 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1125 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1126 }
1127 }
1128 break;
1129 case GL_BOOL:
1130 case GL_BOOL_VEC2:
1131 case GL_BOOL_VEC3:
1132 case GL_BOOL_VEC4:
1133 if (uniform->vsRegisterIndex >= 0)
1134 {
1135 int (*c)[4] = (int(*)[4])mapVS.pData;
1136 GLboolean *b = (GLboolean*)uniform->data;
1137 int count = gl::VariableColumnCount(uniform->type);
1138
1139 for (unsigned int i = 0; i < uniform->registerCount; i++)
1140 {
1141 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1142 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1143 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1144 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1145 }
1146 }
1147 if (uniform->psRegisterIndex >= 0)
1148 {
1149 int (*c)[4] = (int(*)[4])mapPS.pData;
1150 GLboolean *b = (GLboolean*)uniform->data;
1151 int count = gl::VariableColumnCount(uniform->type);
1152
1153 for (unsigned int i = 0; i < uniform->registerCount; i++)
1154 {
1155 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1156 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1157 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1158 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001159 }
1160 }
1161 break;
1162 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001163 UNREACHABLE();
1164 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001165 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001166
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001167 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001168 memcpy(mapVS.pData, &mVertexConstants, sizeof(dx_VertexConstants));
1169 memcpy(mapPS.pData, &mPixelConstants, sizeof(dx_PixelConstants));
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001170
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001171 mDeviceContext->Unmap(constantBufferVS, 0);
1172 mDeviceContext->VSSetConstantBuffers(0, 1, &constantBufferVS);
1173 constantBufferVS->Release();
1174
1175 mDeviceContext->Unmap(constantBufferPS, 0);
1176 mDeviceContext->PSSetConstantBuffers(0, 1, &constantBufferPS);
1177 constantBufferPS->Release();
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001178}
1179
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001180void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001181{
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001182 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1183 {
1184 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1185 if (renderbufferObject)
1186 {
1187 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1188 if (!renderTarget)
1189 {
1190 ERR("render target pointer unexpectedly null.");
1191 return;
1192 }
1193
1194 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1195 if (!framebufferRTV)
1196 {
1197 ERR("render target view pointer unexpectedly null.");
1198 return;
1199 }
1200
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001201 if (mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1202 mCurScissor.x + mCurScissor.width < renderTarget->getWidth() ||
1203 mCurScissor.y + mCurScissor.height < renderTarget->getHeight()))
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001204 {
1205 // TODO: clearing of subregion of render target
1206 UNIMPLEMENTED();
1207 }
1208
1209 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1210 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1211 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1212 clearParams.colorMaskBlue && alphaUnmasked);
1213
1214 if (needMaskedColorClear)
1215 {
1216 // TODO: masked color clearing
1217 UNIMPLEMENTED();
1218 }
1219 else
1220 {
1221 const float clearValues[4] = { clearParams.colorClearValue.red,
1222 clearParams.colorClearValue.green,
1223 clearParams.colorClearValue.blue,
1224 clearParams.colorClearValue.alpha };
1225 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
1226 }
1227
1228 framebufferRTV->Release();
1229 }
1230 }
1231 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
1232 {
1233 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1234 if (renderbufferObject)
1235 {
daniel@transgaming.comd0f82bc2012-12-20 21:11:42 +00001236 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001237 if (!renderTarget)
1238 {
1239 ERR("render target pointer unexpectedly null.");
1240 return;
1241 }
1242
1243 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1244 if (!framebufferDSV)
1245 {
1246 ERR("depth stencil view pointer unexpectedly null.");
1247 return;
1248 }
1249
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001250 if (mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1251 mCurScissor.x + mCurScissor.width < renderTarget->getWidth() ||
1252 mCurScissor.y + mCurScissor.height < renderTarget->getHeight()))
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001253 {
1254 // TODO: clearing of subregion of depth stencil view
1255 UNIMPLEMENTED();
1256 }
1257
1258 unsigned int stencilUnmasked = 0x0;
1259 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1260 {
1261 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1262 stencilUnmasked = (0x1 << stencilSize) - 1;
1263 }
1264
1265 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1266 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1267
1268 if (needMaskedStencilClear)
1269 {
1270 // TODO: masked clearing of depth stencil
1271 UNIMPLEMENTED();
1272 }
1273 else
1274 {
1275 UINT clearFlags = 0;
1276 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1277 {
1278 clearFlags |= D3D11_CLEAR_DEPTH;
1279 }
1280 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1281 {
1282 clearFlags |= D3D11_CLEAR_STENCIL;
1283 }
1284
1285 float depthClear = clearParams.depthClearValue;
1286 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1287
1288 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
1289 }
1290
1291 framebufferDSV->Release();
1292 }
1293 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001294}
1295
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001296void Renderer11::markAllStateDirty()
1297{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001298 mAppliedRenderTargetSerial = 0;
1299 mAppliedDepthbufferSerial = 0;
1300 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001301 mDepthStencilInitialized = false;
1302 mRenderTargetDescInitialized = false;
1303
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001304 for (int i = 0; i < gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; i++)
1305 {
1306 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001307 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001308 }
1309 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1310 {
1311 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001312 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001313 }
1314
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001315 mForceSetBlendState = true;
1316 mForceSetRasterState = true;
1317 mForceSetDepthStencilState = true;
1318 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001319 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001320
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001321 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001322 mAppliedIBOffset = 0;
1323
daniel@transgaming.come4991412012-12-20 20:55:34 +00001324 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001325 mDxUniformsDirty = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001326}
1327
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001328void Renderer11::releaseDeviceResources()
1329{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001330 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001331 mInputLayoutCache.clear();
1332
1333 delete mVertexDataManager;
1334 mVertexDataManager = NULL;
1335
1336 delete mIndexDataManager;
1337 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001338
1339 delete mLineLoopIB;
1340 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001341
1342 delete mTriangleFanIB;
1343 mTriangleFanIB = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001344}
1345
1346void Renderer11::markDeviceLost()
1347{
1348 mDeviceLost = true;
1349}
1350
1351bool Renderer11::isDeviceLost()
1352{
1353 return mDeviceLost;
1354}
1355
1356// set notify to true to broadcast a message to all contexts of the device loss
1357bool Renderer11::testDeviceLost(bool notify)
1358{
1359 bool isLost = false;
1360
1361 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001362 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001363
1364 if (isLost)
1365 {
1366 // ensure we note the device loss --
1367 // we'll probably get this done again by markDeviceLost
1368 // but best to remember it!
1369 // Note that we don't want to clear the device loss status here
1370 // -- this needs to be done by resetDevice
1371 mDeviceLost = true;
1372 if (notify)
1373 {
1374 mDisplay->notifyDeviceLost();
1375 }
1376 }
1377
1378 return isLost;
1379}
1380
1381bool Renderer11::testDeviceResettable()
1382{
1383 HRESULT status = D3D_OK;
1384
1385 // TODO
1386 UNIMPLEMENTED();
1387
1388 switch (status)
1389 {
1390 case D3DERR_DEVICENOTRESET:
1391 case D3DERR_DEVICEHUNG:
1392 return true;
1393 default:
1394 return false;
1395 }
1396}
1397
1398bool Renderer11::resetDevice()
1399{
1400 releaseDeviceResources();
1401
1402 // TODO
1403 UNIMPLEMENTED();
1404
1405 // reset device defaults
1406 initializeDevice();
1407 mDeviceLost = false;
1408
1409 return true;
1410}
1411
1412DWORD Renderer11::getAdapterVendor() const
1413{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001414 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001415}
1416
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001417std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001418{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001419 std::ostringstream rendererString;
1420
1421 rendererString << mDescription;
1422 rendererString << " Direct3D11";
1423
1424 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1425 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1426
1427 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001428}
1429
1430GUID Renderer11::getAdapterIdentifier() const
1431{
1432 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001433 // UNIMPLEMENTED();
1434 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001435 return foo;
1436}
1437
1438bool Renderer11::getDXT1TextureSupport()
1439{
1440 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001441 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001442 return false;
1443}
1444
1445bool Renderer11::getDXT3TextureSupport()
1446{
1447 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001448 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001449 return false;
1450}
1451
1452bool Renderer11::getDXT5TextureSupport()
1453{
1454 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001455 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001456 return false;
1457}
1458
1459bool Renderer11::getDepthTextureSupport() const
1460{
1461 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001462 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001463 return false;
1464}
1465
1466bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1467{
1468 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001469 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001470
1471 *filtering = false;
1472 *renderable = false;
1473 return false;
1474}
1475
1476bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1477{
1478 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001479 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001480
1481 *filtering = false;
1482 *renderable = false;
1483 return false;
1484}
1485
1486bool Renderer11::getLuminanceTextureSupport()
1487{
1488 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001489 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001490 return false;
1491}
1492
1493bool Renderer11::getLuminanceAlphaTextureSupport()
1494{
1495 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001496 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001497 return false;
1498}
1499
1500bool Renderer11::getTextureFilterAnisotropySupport() const
1501{
1502 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001503 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001504 return false;
1505}
1506
1507float Renderer11::getTextureMaxAnisotropy() const
1508{
1509 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001510 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001511 return 1.0f;
1512}
1513
1514bool Renderer11::getEventQuerySupport()
1515{
1516 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001517 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001518 return false;
1519}
1520
1521bool Renderer11::getVertexTextureSupport() const
1522{
1523 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001524 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001525 return false;
1526}
1527
1528bool Renderer11::getNonPower2TextureSupport() const
1529{
1530 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001531 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001532 return false;
1533}
1534
1535bool Renderer11::getOcclusionQuerySupport() const
1536{
1537 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001538 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001539 return false;
1540}
1541
1542bool Renderer11::getInstancingSupport() const
1543{
1544 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001545 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001546 return false;
1547}
1548
1549bool Renderer11::getShareHandleSupport() const
1550{
1551 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001552 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001553
1554 // PIX doesn't seem to support using share handles, so disable them.
1555 return false && !gl::perfActive();
1556}
1557
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001558bool Renderer11::getDerivativeInstructionSupport() const
1559{
1560 // TODO
1561 // UNIMPLEMENTED();
1562 return false;
1563}
1564
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001565int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001566{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001567 switch (mFeatureLevel)
1568 {
1569 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001570 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001571 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1572 default: UNREACHABLE(); return 0;
1573 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001574}
1575
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001576int Renderer11::getMinorShaderModel() const
1577{
1578 switch (mFeatureLevel)
1579 {
1580 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1581 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1582 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1583 default: UNREACHABLE(); return 0;
1584 }
1585}
1586
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001587float Renderer11::getMaxPointSize() const
1588{
1589 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001590 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001591 return 1.0f;
1592}
1593
1594int Renderer11::getMaxTextureWidth() const
1595{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001596 switch (mFeatureLevel)
1597 {
1598 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1599 case D3D_FEATURE_LEVEL_10_1:
1600 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1601 default: UNREACHABLE(); return 0;
1602 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001603}
1604
1605int Renderer11::getMaxTextureHeight() const
1606{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001607 switch (mFeatureLevel)
1608 {
1609 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1610 case D3D_FEATURE_LEVEL_10_1:
1611 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1612 default: UNREACHABLE(); return 0;
1613 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001614}
1615
1616bool Renderer11::get32BitIndexSupport() const
1617{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001618 switch (mFeatureLevel)
1619 {
1620 case D3D_FEATURE_LEVEL_11_0:
1621 case D3D_FEATURE_LEVEL_10_1:
1622 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1623 default: UNREACHABLE(); return false;
1624 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001625}
1626
1627int Renderer11::getMinSwapInterval() const
1628{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001629 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001630}
1631
1632int Renderer11::getMaxSwapInterval() const
1633{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001634 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001635}
1636
1637int Renderer11::getMaxSupportedSamples() const
1638{
1639 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001640 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001641 return 1;
1642}
1643
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001644bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001645{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001646 if (source && dest)
1647 {
1648 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1649 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1650
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001651 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001652 return true;
1653 }
1654
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001655 return false;
1656}
1657
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001658bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001659{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001660 if (source && dest)
1661 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001662 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1663 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001664
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001665 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001666 return true;
1667 }
1668
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001669 return false;
1670}
1671
daniel@transgaming.com38380882012-11-28 19:36:39 +00001672bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001673 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001674{
1675 // TODO
1676 UNIMPLEMENTED();
1677 return false;
1678}
1679
1680bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001681 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001682{
1683 // TODO
1684 UNIMPLEMENTED();
1685 return false;
1686}
1687
daniel@transgaming.comf2423652012-11-28 20:53:50 +00001688RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
1689{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00001690 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00001691 RenderTarget11 *renderTarget = NULL;
1692 if (depth)
1693 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00001694 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL,
1695 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00001696 }
1697 else
1698 {
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00001699 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
1700 swapChain11->getRenderTargetShaderResource(),
1701 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00001702 }
1703 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00001704}
1705
1706RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
1707{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00001708 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
1709 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00001710}
1711
daniel@transgaming.com2275f912012-12-20 21:13:22 +00001712ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00001713{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00001714 ShaderExecutable11 *executable = NULL;
1715
1716 switch (type)
1717 {
1718 case GL_VERTEX_SHADER:
1719 {
1720 ID3D11VertexShader *vshader = NULL;
1721 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
1722 ASSERT(SUCCEEDED(result));
1723
1724 if (vshader)
1725 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001726 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00001727 }
1728 }
1729 break;
1730 case GL_FRAGMENT_SHADER:
1731 {
1732 ID3D11PixelShader *pshader = NULL;
1733 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
1734 ASSERT(SUCCEEDED(result));
1735
1736 if (pshader)
1737 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001738 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00001739 }
1740 }
1741 break;
1742 default:
1743 UNREACHABLE();
1744 break;
1745 }
1746
1747 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00001748}
1749
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001750ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
1751{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00001752 const char *profile = NULL;
1753
1754 switch (type)
1755 {
1756 case GL_VERTEX_SHADER:
1757 profile = "vs_4_0";
1758 break;
1759 case GL_FRAGMENT_SHADER:
1760 profile = "ps_4_0";
1761 break;
1762 default:
1763 UNREACHABLE();
1764 return NULL;
1765 }
1766
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00001767 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00001768 if (!binary)
1769 return NULL;
1770
daniel@transgaming.com2275f912012-12-20 21:13:22 +00001771 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00001772 binary->Release();
1773
1774 return executable;
1775}
1776
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00001777VertexBuffer *Renderer11::createVertexBuffer()
1778{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00001779 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00001780}
1781
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00001782IndexBuffer *Renderer11::createIndexBuffer()
1783{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00001784 return new IndexBuffer11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00001785}
1786
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001787bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
1788 bool blitRenderTarget, bool blitDepthStencil)
1789{
1790 // TODO
1791 UNIMPLEMENTED();
1792 return false;
1793}
1794
1795void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
1796 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
1797{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00001798 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00001799 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00001800
1801 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1802 if (colorbuffer)
1803 {
1804 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1805 if (renderTarget)
1806 {
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00001807 subresourceIndex = renderTarget->getSubresourceIndex();
1808
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00001809 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
1810 if (colorBufferRTV)
1811 {
1812 ID3D11Resource *textureResource = NULL;
1813 colorBufferRTV->GetResource(&textureResource);
1814
1815 if (textureResource)
1816 {
1817 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)&colorBufferTexture);
1818 textureResource->Release();
1819
1820 if (FAILED(result))
1821 {
1822 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
1823 "HRESULT: 0x%X.", result);
1824 return;
1825 }
1826 }
1827 }
1828 }
1829 }
1830
1831 if (colorBufferTexture)
1832 {
1833 gl::Rectangle area;
1834 area.x = x;
1835 area.y = y;
1836 area.width = width;
1837 area.height = height;
1838
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00001839 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
1840 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00001841
1842 colorBufferTexture->Release();
1843 colorBufferTexture = NULL;
1844 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001845}
1846
daniel@transgaming.com244e1832012-12-20 20:52:35 +00001847Image *Renderer11::createImage()
1848{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00001849 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00001850}
1851
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00001852void Renderer11::generateMipmap(Image *dest, Image *src)
1853{
1854 // TODO
1855 UNIMPLEMENTED();
1856 return;
1857}
1858
daniel@transgaming.com413d2712012-12-20 21:11:04 +00001859TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
1860{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00001861 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
1862 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00001863}
1864
1865TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
1866{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00001867 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00001868}
1869
1870TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
1871{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00001872 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00001873}
1874
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00001875static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
1876{
1877 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
1878 destFormat == GL_ALPHA &&
1879 destType == GL_UNSIGNED_BYTE)
1880 {
1881 return 1;
1882 }
1883 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
1884 destFormat == GL_RGBA &&
1885 destType == GL_UNSIGNED_BYTE)
1886 {
1887 return 4;
1888 }
1889 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
1890 destFormat == GL_BGRA_EXT &&
1891 destType == GL_UNSIGNED_BYTE)
1892 {
1893 return 4;
1894 }
1895 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
1896 destFormat == GL_RGBA &&
1897 destType == GL_HALF_FLOAT_OES)
1898 {
1899 return 8;
1900 }
1901 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
1902 destFormat == GL_RGB &&
1903 destType == GL_FLOAT)
1904 {
1905 return 12;
1906 }
1907 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
1908 destFormat == GL_RGBA &&
1909 destType == GL_FLOAT)
1910 {
1911 return 16;
1912 }
1913 else
1914 {
1915 return 0;
1916 }
1917}
1918
1919static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
1920 unsigned int y, int inputPitch, gl::Color *outColor)
1921{
1922 switch (format)
1923 {
1924 case DXGI_FORMAT_R8G8B8A8_UNORM:
1925 {
1926 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
1927 outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
1928 outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
1929 outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
1930 outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
1931 }
1932 break;
1933
1934 case DXGI_FORMAT_A8_UNORM:
1935 {
1936 outColor->red = 0.0f;
1937 outColor->green = 0.0f;
1938 outColor->blue = 0.0f;
1939 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
1940 }
1941 break;
1942
1943 case DXGI_FORMAT_R32G32B32A32_FLOAT:
1944 {
1945 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
1946 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
1947 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
1948 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
1949 }
1950 break;
1951
1952 case DXGI_FORMAT_R32G32B32_FLOAT:
1953 {
1954 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
1955 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
1956 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
1957 outColor->alpha = 1.0f;
1958 }
1959 break;
1960
1961 case DXGI_FORMAT_R16G16B16A16_FLOAT:
1962 {
1963 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
1964 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
1965 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
1966 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
1967 }
1968 break;
1969
1970 case DXGI_FORMAT_B8G8R8A8_UNORM:
1971 {
1972 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
1973 outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
1974 outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
1975 outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
1976 outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
1977 }
1978 break;
1979
1980 case DXGI_FORMAT_R8_UNORM:
1981 {
1982 outColor->red = *(data + x + y * inputPitch) / 255.0f;
1983 outColor->green = 0.0f;
1984 outColor->blue = 0.0f;
1985 outColor->alpha = 1.0f;
1986 }
1987 break;
1988
1989 case DXGI_FORMAT_R8G8_UNORM:
1990 {
1991 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
1992
1993 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
1994 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
1995 outColor->blue = 0.0f;
1996 outColor->alpha = 1.0f;
1997 }
1998 break;
1999
2000 case DXGI_FORMAT_R16_FLOAT:
2001 {
2002 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
2003 outColor->green = 0.0f;
2004 outColor->blue = 0.0f;
2005 outColor->alpha = 1.0f;
2006 }
2007 break;
2008
2009 case DXGI_FORMAT_R16G16_FLOAT:
2010 {
2011 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
2012 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
2013 outColor->blue = 0.0f;
2014 outColor->alpha = 1.0f;
2015 }
2016 break;
2017
2018 default:
2019 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
2020 UNIMPLEMENTED();
2021 break;
2022 }
2023}
2024
2025static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
2026 unsigned int y, int outputPitch, void *outData)
2027{
2028 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
2029 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
2030
2031 switch (format)
2032 {
2033 case GL_RGBA:
2034 switch (type)
2035 {
2036 case GL_UNSIGNED_BYTE:
2037 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2038 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2039 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2040 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2041 break;
2042
2043 default:
2044 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
2045 UNIMPLEMENTED();
2046 break;
2047 }
2048 break;
2049
2050 case GL_BGRA_EXT:
2051 switch (type)
2052 {
2053 case GL_UNSIGNED_BYTE:
2054 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2055 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2056 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
2057 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
2058 break;
2059
2060 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2061 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2062 // this type is packed as follows:
2063 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2064 // --------------------------------------------------------------------------------
2065 // | 4th | 3rd | 2nd | 1st component |
2066 // --------------------------------------------------------------------------------
2067 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2068 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2069 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
2070 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
2071 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
2072 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
2073 break;
2074
2075 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2076 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2077 // this type is packed as follows:
2078 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2079 // --------------------------------------------------------------------------------
2080 // | 4th | 3rd | 2nd | 1st component |
2081 // --------------------------------------------------------------------------------
2082 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2083 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2084 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
2085 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
2086 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
2087 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
2088 break;
2089
2090 default:
2091 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
2092 UNIMPLEMENTED();
2093 break;
2094 }
2095 break;
2096
2097 case GL_RGB:
2098 switch (type)
2099 {
2100 case GL_UNSIGNED_SHORT_5_6_5:
2101 shortData[x + y * outputPitch / sizeof(unsigned short)] =
2102 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
2103 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
2104 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
2105 break;
2106
2107 case GL_UNSIGNED_BYTE:
2108 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
2109 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
2110 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
2111 break;
2112
2113 default:
2114 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
2115 UNIMPLEMENTED();
2116 break;
2117 }
2118 break;
2119
2120 default:
2121 ERR("WritePixelColor not implemented for format 0x%X.", format);
2122 UNIMPLEMENTED();
2123 break;
2124 }
2125}
2126
2127void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2128 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2129 GLint packAlignment, void *pixels)
2130{
2131 D3D11_TEXTURE2D_DESC textureDesc;
2132 texture->GetDesc(&textureDesc);
2133
2134 D3D11_TEXTURE2D_DESC stagingDesc;
2135 stagingDesc.Width = area.width;
2136 stagingDesc.Height = area.height;
2137 stagingDesc.MipLevels = 1;
2138 stagingDesc.ArraySize = 1;
2139 stagingDesc.Format = textureDesc.Format;
2140 stagingDesc.SampleDesc.Count = 1;
2141 stagingDesc.SampleDesc.Quality = 0;
2142 stagingDesc.Usage = D3D11_USAGE_STAGING;
2143 stagingDesc.BindFlags = 0;
2144 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2145 stagingDesc.MiscFlags = 0;
2146
2147 ID3D11Texture2D* stagingTex = NULL;
2148 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2149 if (FAILED(result))
2150 {
2151 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2152 return;
2153 }
2154
2155 ID3D11Texture2D* srcTex = NULL;
2156 if (textureDesc.SampleDesc.Count > 1)
2157 {
2158 D3D11_TEXTURE2D_DESC resolveDesc;
2159 resolveDesc.Width = textureDesc.Width;
2160 resolveDesc.Height = textureDesc.Height;
2161 resolveDesc.MipLevels = 1;
2162 resolveDesc.ArraySize = 1;
2163 resolveDesc.Format = textureDesc.Format;
2164 resolveDesc.SampleDesc.Count = 1;
2165 resolveDesc.SampleDesc.Quality = 0;
2166 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2167 resolveDesc.BindFlags = 0;
2168 resolveDesc.CPUAccessFlags = 0;
2169 resolveDesc.MiscFlags = 0;
2170
2171 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2172 if (FAILED(result))
2173 {
2174 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
2175 stagingTex->Release();
2176 return;
2177 }
2178
2179 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2180 subResource = 0;
2181 }
2182 else
2183 {
2184 srcTex = texture;
2185 srcTex->AddRef();
2186 }
2187
2188 D3D11_BOX srcBox;
2189 srcBox.left = area.x;
2190 srcBox.right = area.x + area.width;
2191 srcBox.top = area.y;
2192 srcBox.bottom = area.y + area.height;
2193 srcBox.front = 0;
2194 srcBox.back = 1;
2195
2196 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2197
2198 srcTex->Release();
2199 srcTex = NULL;
2200
2201 D3D11_MAPPED_SUBRESOURCE mapping;
2202 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
2203
2204 unsigned char *source;
2205 int inputPitch;
2206 if (packReverseRowOrder)
2207 {
2208 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
2209 inputPitch = -static_cast<int>(mapping.RowPitch);
2210 }
2211 else
2212 {
2213 source = static_cast<unsigned char*>(mapping.pData);
2214 inputPitch = static_cast<int>(mapping.RowPitch);
2215 }
2216
2217 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
2218 if (fastPixelSize != 0)
2219 {
2220 unsigned char *dest = static_cast<unsigned char*>(pixels);
2221 for (int j = 0; j < area.height; j++)
2222 {
2223 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
2224 }
2225 }
2226 else
2227 {
2228 gl::Color pixelColor;
2229 for (int j = 0; j < area.height; j++)
2230 {
2231 for (int i = 0; i < area.width; i++)
2232 {
2233 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
2234 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
2235 }
2236 }
2237 }
2238
2239 mDeviceContext->Unmap(stagingTex, 0);
2240
2241 stagingTex->Release();
2242 stagingTex = NULL;
2243}
2244
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002245}