blob: 83151643e17521d28be01c13aa8282d769e0c4d3 [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
29#include "libEGL/Config.h"
30#include "libEGL/Display.h"
31
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000032#include <sstream>
33
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000034namespace rx
35{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000036static const DXGI_FORMAT RenderTargetFormats[] =
37 {
38 DXGI_FORMAT_R8G8B8A8_UNORM
39 };
40
41static const DXGI_FORMAT DepthStencilFormats[] =
42 {
43 DXGI_FORMAT_D24_UNORM_S8_UINT
44 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000045
46Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
47{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000048 mVertexDataManager = NULL;
49 mIndexDataManager = NULL;
50
daniel@transgaming.comc5114302012-12-20 21:11:36 +000051 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000052 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000053
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000054 mD3d11Module = NULL;
55 mDxgiModule = NULL;
56
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000057 mDeviceLost = false;
58
daniel@transgaming.com25072f62012-11-28 19:31:32 +000059 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000060 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 mDxgiAdapter = NULL;
62 mDxgiFactory = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000063}
64
65Renderer11::~Renderer11()
66{
67 releaseDeviceResources();
68
daniel@transgaming.com65e65372012-11-28 19:33:50 +000069 if (mDxgiFactory)
70 {
71 mDxgiFactory->Release();
72 mDxgiFactory = NULL;
73 }
74
75 if (mDxgiAdapter)
76 {
77 mDxgiAdapter->Release();
78 mDxgiAdapter = NULL;
79 }
80
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000081 if (mDeviceContext)
82 {
daniel@transgaming.comd5df4e82013-01-11 04:11:21 +000083 mDeviceContext->ClearState();
84 mDeviceContext->Flush();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000085 mDeviceContext->Release();
86 mDeviceContext = NULL;
87 }
88
daniel@transgaming.com25072f62012-11-28 19:31:32 +000089 if (mDevice)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000090 {
daniel@transgaming.com25072f62012-11-28 19:31:32 +000091 mDevice->Release();
92 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000093 }
94
95 if (mD3d11Module)
96 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000097 FreeLibrary(mD3d11Module);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000098 mD3d11Module = NULL;
99 }
100
101 if (mDxgiModule)
102 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000103 FreeLibrary(mDxgiModule);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000104 mDxgiModule = NULL;
105 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000106}
107
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000108Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
109{
110 ASSERT(dynamic_cast<rx::Renderer11*>(renderer) != NULL);
111 return static_cast<rx::Renderer11*>(renderer);
112}
113
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114EGLint Renderer11::initialize()
115{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000116 if (!initializeCompiler())
117 {
118 return EGL_NOT_INITIALIZED;
119 }
120
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000121 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
122 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000123
124 if (mD3d11Module == NULL || mDxgiModule == NULL)
125 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000127 return EGL_NOT_INITIALIZED;
128 }
129
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000130 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 if (D3D11CreateDevice == NULL)
133 {
134 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
135 return EGL_NOT_INITIALIZED;
136 }
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000137
138 D3D_FEATURE_LEVEL featureLevel[] =
139 {
140 D3D_FEATURE_LEVEL_11_0,
141 D3D_FEATURE_LEVEL_10_1,
142 D3D_FEATURE_LEVEL_10_0,
143 };
144
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000145 HRESULT result = D3D11CreateDevice(NULL,
146 D3D_DRIVER_TYPE_HARDWARE,
147 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000148 #if defined(_DEBUG)
149 D3D11_CREATE_DEVICE_DEBUG,
150 #else
151 0,
152 #endif
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000153 featureLevel,
154 sizeof(featureLevel)/sizeof(featureLevel[0]),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000155 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000156 &mDevice,
157 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000158 &mDeviceContext);
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000159
160 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000161 {
162 ERR("Could not create D3D11 device - aborting!\n");
163 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
164 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000165
166 IDXGIDevice *dxgiDevice = NULL;
167 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
168
169 if (FAILED(result))
170 {
171 ERR("Could not query DXGI device - aborting!\n");
172 return EGL_NOT_INITIALIZED;
173 }
174
175 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
176
177 if (FAILED(result))
178 {
179 ERR("Could not retrieve DXGI adapter - aborting!\n");
180 return EGL_NOT_INITIALIZED;
181 }
182
183 dxgiDevice->Release();
184
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000185 mDxgiAdapter->GetDesc(&mAdapterDescription);
186 memset(mDescription, 0, sizeof(mDescription));
187 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
188
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000189 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
190
191 if (!mDxgiFactory || FAILED(result))
192 {
193 ERR("Could not create DXGI factory - aborting!\n");
194 return EGL_NOT_INITIALIZED;
195 }
196
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000197 initializeDevice();
198
199 return EGL_SUCCESS;
200}
201
202// do any one-time device initialization
203// NOTE: this is also needed after a device lost/reset
204// to reset the scene status and ensure the default states are reset.
205void Renderer11::initializeDevice()
206{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000207 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000208 mInputLayoutCache.initialize(mDevice, mDeviceContext);
209
210 ASSERT(!mVertexDataManager && !mIndexDataManager);
211 mVertexDataManager = new VertexDataManager(this);
212 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000213
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000214 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000215}
216
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000217int Renderer11::generateConfigs(ConfigDesc **configDescList)
218{
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000219 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
220 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000221 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
222 int numConfigs = 0;
223
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000224 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000225 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000226 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000227 {
228 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
229
230 UINT formatSupport = 0;
231 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
232
233 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
234 {
235 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
236
237 UINT formatSupport = 0;
238 HRESULT result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
239
240 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL))
241 {
242 ConfigDesc newConfig;
243 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
244 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
245 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
246 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
247
248 (*configDescList)[numConfigs++] = newConfig;
249 }
250 }
251 }
252 }
253
254 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000255}
256
257void Renderer11::deleteConfigs(ConfigDesc *configDescList)
258{
259 delete [] (configDescList);
260}
261
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000262void Renderer11::sync(bool block)
263{
264 // TODO
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000265 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000266}
267
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000268SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
269{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000270 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000271}
272
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000273void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
274{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000275 if (type == gl::SAMPLER_PIXEL)
276 {
277 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
278 {
279 ERR("Pixel shader sampler index %i is not valid.", index);
280 return;
281 }
282
283 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
284 {
285 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
286
287 if (!dxSamplerState)
288 {
289 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
290 "sampler state for pixel shaders at slot %i.", index);
291 }
292
293 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
294
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000295 mCurPixelSamplerStates[index] = samplerState;
296 }
297
298 mForceSetPixelSamplerStates[index] = false;
299 }
300 else if (type == gl::SAMPLER_VERTEX)
301 {
302 if (index < 0 || index >= gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
303 {
304 ERR("Vertex shader sampler index %i is not valid.", index);
305 return;
306 }
307
308 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
309 {
310 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
311
312 if (!dxSamplerState)
313 {
314 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
315 "sampler state for vertex shaders at slot %i.", index);
316 }
317
318 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
319
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000320 mCurVertexSamplerStates[index] = samplerState;
321 }
322
323 mForceSetVertexSamplerStates[index] = false;
324 }
325 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000326}
327
328void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
329{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000330 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000331 unsigned int serial = 0;
332 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000333
334 if (texture)
335 {
336 TextureStorageInterface *texStorage = texture->getNativeTexture();
337 if (texStorage)
338 {
339 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
340 textureSRV = storage11->getSRV();
341 }
342
343 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
344 // missing the shader resource view
345 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000346
347 serial = texture->getTextureSerial();
348 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000349 }
350
351 if (type == gl::SAMPLER_PIXEL)
352 {
353 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
354 {
355 ERR("Pixel shader sampler index %i is not valid.", index);
356 return;
357 }
358
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000359 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
360 {
361 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
362 }
363
364 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000365 }
366 else if (type == gl::SAMPLER_VERTEX)
367 {
368 if (index < 0 || index >= gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
369 {
370 ERR("Vertex shader sampler index %i is not valid.", index);
371 return;
372 }
373
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000374 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
375 {
376 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
377 }
378
379 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000380 }
381 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000382}
383
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000384void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000385{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000386 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000387 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000388 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
389 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000390 if (!dxRasterState)
391 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000392 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000393 "rasterizer state.");
394 }
395
396 mDeviceContext->RSSetState(dxRasterState);
397
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000398 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000399 }
400
401 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000402}
403
404void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
405 unsigned int sampleMask)
406{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000407 if (mForceSetBlendState ||
408 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
409 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
410 sampleMask != mCurSampleMask)
411 {
412 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
413 if (!dxBlendState)
414 {
415 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
416 "blend state.");
417 }
418
419 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
420 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
421
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000422 mCurBlendState = blendState;
423 mCurBlendColor = blendColor;
424 mCurSampleMask = sampleMask;
425 }
426
427 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000428}
429
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000430void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000431 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000432{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000433 if (mForceSetDepthStencilState ||
434 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
435 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
436 {
437 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
438 stencilRef != stencilBackRef ||
439 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
440 {
441 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
442 "invalid under WebGL.");
443 return error(GL_INVALID_OPERATION);
444 }
445
446 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
447 if (!dxDepthStencilState)
448 {
449 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
450 "setting the default depth stencil state.");
451 }
452
453 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
454
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000455 mCurDepthStencilState = depthStencilState;
456 mCurStencilRef = stencilRef;
457 mCurStencilBackRef = stencilBackRef;
458 }
459
460 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000461}
462
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000463void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000464{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000465 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
466 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000467 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000468 if (enabled)
469 {
470 D3D11_RECT rect;
471 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
472 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
473 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
474 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000475
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000476 mDeviceContext->RSSetScissorRects(1, &rect);
477 }
478
479 if (enabled != mScissorEnabled)
480 {
481 mForceSetRasterState = true;
482 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000483
484 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000485 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000486 }
487
488 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000489}
490
daniel@transgaming.com12985182012-12-20 20:56:31 +0000491bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000492 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000493{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000494 gl::Rectangle actualViewport = viewport;
495 float actualZNear = gl::clamp01(zNear);
496 float actualZFar = gl::clamp01(zFar);
497 if (ignoreViewport)
498 {
499 actualViewport.x = 0;
500 actualViewport.y = 0;
501 actualViewport.width = mRenderTargetDesc.width;
502 actualViewport.height = mRenderTargetDesc.height;
503 actualZNear = 0.0f;
504 actualZFar = 1.0f;
505 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000506
507 D3D11_VIEWPORT dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000508 dxViewport.TopLeftX = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
509 dxViewport.TopLeftY = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
510 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.TopLeftX));
511 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.TopLeftY));
512 dxViewport.MinDepth = actualZNear;
513 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000514
515 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
516 {
517 return false; // Nothing to render
518 }
519
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000520 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
521 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000522
daniel@transgaming.com53670042012-11-28 20:55:51 +0000523 if (viewportChanged)
524 {
525 mDeviceContext->RSSetViewports(1, &dxViewport);
526
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000527 mCurViewport = actualViewport;
528 mCurNear = actualZNear;
529 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000530 }
531
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000532 if (currentProgram)
daniel@transgaming.com53670042012-11-28 20:55:51 +0000533 {
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000534 dx_VertexConstants vc = {0};
535 dx_PixelConstants pc = {0};
daniel@transgaming.com53670042012-11-28 20:55:51 +0000536
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000537 vc.halfPixelSize[0] = 0.0f;
538 vc.halfPixelSize[1] = 0.0f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000539
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000540 pc.coord[0] = actualViewport.width * 0.5f;
541 pc.coord[1] = actualViewport.height * 0.5f;
542 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
543 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000544
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000545 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
546 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
547 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
548
549 vc.depthRange[0] = actualZNear;
550 vc.depthRange[1] = actualZFar;
551 vc.depthRange[2] = actualZFar - actualZNear;
552
553 pc.depthRange[0] = actualZNear;
554 pc.depthRange[1] = actualZFar;
555 pc.depthRange[2] = actualZFar - actualZNear;
556
557 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
558 {
559 mVertexConstants = vc;
560 mDxUniformsDirty = true;
561 }
562
563 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
564 {
565 mPixelConstants = pc;
566 mDxUniformsDirty = true;
567 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000568 }
569
570 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000571 return true;
572}
573
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000574bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
575{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000576 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000577
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000578 switch (mode)
579 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000580 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
581 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000582 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000583 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
584 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
585 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000586 // emulate fans via rewriting index buffer
587 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000588 default:
589 return error(GL_INVALID_ENUM, false);
590 }
591
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000592 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000593
594 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000595}
596
597bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000598{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000599 // Get the color render buffer and serial
600 gl::Renderbuffer *renderbufferObject = NULL;
601 unsigned int renderTargetSerial = 0;
602 if (framebuffer->getColorbufferType() != GL_NONE)
603 {
604 renderbufferObject = framebuffer->getColorbuffer();
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000605
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000606 if (!renderbufferObject)
607 {
608 ERR("render target pointer unexpectedly null.");
daniel@transgaming.come9c71b42012-11-28 21:02:23 +0000609 return false;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000610 }
611
612 renderTargetSerial = renderbufferObject->getSerial();
613 }
614
615 // Get the depth stencil render buffer and serials
616 gl::Renderbuffer *depthStencil = NULL;
617 unsigned int depthbufferSerial = 0;
618 unsigned int stencilbufferSerial = 0;
619 if (framebuffer->getDepthbufferType() != GL_NONE)
620 {
621 depthStencil = framebuffer->getDepthbuffer();
622 if (!depthStencil)
623 {
624 ERR("Depth stencil pointer unexpectedly null.");
625 return false;
626 }
627
628 depthbufferSerial = depthStencil->getSerial();
629 }
630 else if (framebuffer->getStencilbufferType() != GL_NONE)
631 {
632 depthStencil = framebuffer->getStencilbuffer();
633 if (!depthStencil)
634 {
635 ERR("Depth stencil pointer unexpectedly null.");
636 return false;
637 }
638
639 stencilbufferSerial = depthStencil->getSerial();
640 }
641
642 // Extract the render target dimensions and view
643 unsigned int renderTargetWidth = 0;
644 unsigned int renderTargetHeight = 0;
645 GLenum renderTargetFormat = 0;
646 ID3D11RenderTargetView* framebufferRTV = NULL;
647 if (renderbufferObject)
648 {
649 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
650 if (!renderTarget)
651 {
652 ERR("render target pointer unexpectedly null.");
653 return false;
654 }
655
656 framebufferRTV = renderTarget->getRenderTargetView();
657 if (!framebufferRTV)
658 {
659 ERR("render target view pointer unexpectedly null.");
660 return false;
661 }
662
663 renderTargetWidth = renderbufferObject->getWidth();
664 renderTargetHeight = renderbufferObject->getHeight();
665 renderTargetFormat = renderbufferObject->getActualFormat();
666 }
667
668 // Extract the depth stencil sizes and view
669 unsigned int depthSize = 0;
670 unsigned int stencilSize = 0;
671 ID3D11DepthStencilView* framebufferDSV = NULL;
672 if (depthStencil)
673 {
674 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
675 if (!depthStencilRenderTarget)
676 {
677 ERR("render target pointer unexpectedly null.");
678 if (framebufferRTV)
679 {
680 framebufferRTV->Release();
681 }
682 return false;
683 }
684
685 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
686 if (!framebufferDSV)
687 {
688 ERR("depth stencil view pointer unexpectedly null.");
689 if (framebufferRTV)
690 {
691 framebufferRTV->Release();
692 }
693 return false;
694 }
695
696 // If there is no render buffer, the width, height and format values come from
697 // the depth stencil
698 if (!renderbufferObject)
699 {
700 renderTargetWidth = depthStencil->getWidth();
701 renderTargetHeight = depthStencil->getHeight();
702 renderTargetFormat = depthStencil->getActualFormat();
703 }
704
705 depthSize = depthStencil->getDepthSize();
706 stencilSize = depthStencil->getStencilSize();
707 }
708
709 // Apply the render target and depth stencil
710 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
711 renderTargetSerial != mAppliedRenderTargetSerial ||
712 depthbufferSerial != mAppliedDepthbufferSerial ||
713 stencilbufferSerial != mAppliedStencilbufferSerial)
714 {
715 mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV);
716
717 mRenderTargetDesc.width = renderTargetWidth;
718 mRenderTargetDesc.height = renderTargetHeight;
719 mRenderTargetDesc.format = renderTargetFormat;
720 mForceSetViewport = true; // TODO: It may not be required to clamp the viewport in D3D11
721 mForceSetScissor = true; // TODO: It may not be required to clamp the scissor in D3D11
722
723 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
724 {
725 mCurDepthSize = depthSize;
726 mForceSetRasterState = true;
727 }
728
729 mCurStencilSize = stencilSize;
730
731 mAppliedRenderTargetSerial = renderTargetSerial;
732 mAppliedDepthbufferSerial = depthbufferSerial;
733 mAppliedStencilbufferSerial = stencilbufferSerial;
734 mRenderTargetDescInitialized = true;
735 mDepthStencilInitialized = true;
736 }
737
738 if (framebufferRTV)
739 {
740 framebufferRTV->Release();
741 }
742 if (framebufferDSV)
743 {
744 framebufferDSV->Release();
745 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000746
747 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000748}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000749
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000750GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000751{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000752 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
753 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
754 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000755 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000756 return err;
757 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000758
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000759 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000760}
761
daniel@transgaming.com31240482012-11-28 21:06:41 +0000762GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000763{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000764 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000765
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000766 if (err == GL_NO_ERROR)
767 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000768 if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000769 {
770 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
771
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000772 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000773 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000774 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000775 }
776 }
777
778 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000779}
780
781void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
782{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000783 if (mode == GL_LINE_LOOP)
784 {
785 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
786 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000787 else if (mode == GL_TRIANGLE_FAN)
788 {
789 drawTriangleFan(count, GL_NONE, NULL, 0, NULL);
790 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000791 else if (instances > 0)
792 {
793 // TODO
794 UNIMPLEMENTED();
795 }
796 else
797 {
798 mDeviceContext->Draw(count, 0);
799 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000800}
801
daniel@transgaming.com31240482012-11-28 21:06:41 +0000802void 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 +0000803{
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000804 if (mode == GL_LINE_LOOP)
805 {
806 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
807 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000808 else if (mode == GL_TRIANGLE_FAN)
809 {
810 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
811 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000812 else
813 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +0000814 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000815 }
816}
817
818void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
819{
820 // Get the raw indices for an indexed draw
821 if (type != GL_NONE && elementArrayBuffer)
822 {
823 gl::Buffer *indexBuffer = elementArrayBuffer;
824 intptr_t offset = reinterpret_cast<intptr_t>(indices);
825 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
826 }
827
828 if (!mLineLoopIB)
829 {
830 mLineLoopIB = new StreamingIndexBufferInterface(this);
831 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
832 {
833 delete mLineLoopIB;
834 mLineLoopIB = NULL;
835
836 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
837 return error(GL_OUT_OF_MEMORY);
838 }
839 }
840
841 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
842 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
843 {
844 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
845 return error(GL_OUT_OF_MEMORY);
846 }
847
848 void* mappedMemory = NULL;
849 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
850 if (offset == -1 || mappedMemory == NULL)
851 {
852 ERR("Could not map index buffer for GL_LINE_LOOP.");
853 return error(GL_OUT_OF_MEMORY);
854 }
855
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000856 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000857 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000858
859 switch (type)
860 {
861 case GL_NONE: // Non-indexed draw
862 for (int i = 0; i < count; i++)
863 {
864 data[i] = i;
865 }
866 data[count] = 0;
867 break;
868 case GL_UNSIGNED_BYTE:
869 for (int i = 0; i < count; i++)
870 {
871 data[i] = static_cast<const GLubyte*>(indices)[i];
872 }
873 data[count] = static_cast<const GLubyte*>(indices)[0];
874 break;
875 case GL_UNSIGNED_SHORT:
876 for (int i = 0; i < count; i++)
877 {
878 data[i] = static_cast<const GLushort*>(indices)[i];
879 }
880 data[count] = static_cast<const GLushort*>(indices)[0];
881 break;
882 case GL_UNSIGNED_INT:
883 for (int i = 0; i < count; i++)
884 {
885 data[i] = static_cast<const GLuint*>(indices)[i];
886 }
887 data[count] = static_cast<const GLuint*>(indices)[0];
888 break;
889 default: UNREACHABLE();
890 }
891
892 if (!mLineLoopIB->unmapBuffer())
893 {
894 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
895 return error(GL_OUT_OF_MEMORY);
896 }
897
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000898 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000899 {
900 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
901
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000902 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000903 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000904 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +0000905 }
906
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000907 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000908}
909
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000910void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
911{
912 // Get the raw indices for an indexed draw
913 if (type != GL_NONE && elementArrayBuffer)
914 {
915 gl::Buffer *indexBuffer = elementArrayBuffer;
916 intptr_t offset = reinterpret_cast<intptr_t>(indices);
917 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
918 }
919
920 if (!mTriangleFanIB)
921 {
922 mTriangleFanIB = new StreamingIndexBufferInterface(this);
923 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
924 {
925 delete mTriangleFanIB;
926 mTriangleFanIB = NULL;
927
928 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
929 return error(GL_OUT_OF_MEMORY);
930 }
931 }
932
933 const int numTris = count - 2;
934 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
935 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
936 {
937 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
938 return error(GL_OUT_OF_MEMORY);
939 }
940
941 void* mappedMemory = NULL;
942 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
943 if (offset == -1 || mappedMemory == NULL)
944 {
945 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
946 return error(GL_OUT_OF_MEMORY);
947 }
948
949 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
950 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
951
952 switch (type)
953 {
954 case GL_NONE: // Non-indexed draw
955 for (int i = 0; i < numTris; i++)
956 {
957 data[i*3 + 0] = 0;
958 data[i*3 + 1] = i + 1;
959 data[i*3 + 2] = i + 2;
960 }
961 break;
962 case GL_UNSIGNED_BYTE:
963 for (int i = 0; i < numTris; i++)
964 {
965 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
966 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
967 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
968 }
969 break;
970 case GL_UNSIGNED_SHORT:
971 for (int i = 0; i < numTris; i++)
972 {
973 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
974 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
975 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
976 }
977 break;
978 case GL_UNSIGNED_INT:
979 for (int i = 0; i < numTris; i++)
980 {
981 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
982 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
983 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
984 }
985 break;
986 default: UNREACHABLE();
987 }
988
989 if (!mTriangleFanIB->unmapBuffer())
990 {
991 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
992 return error(GL_OUT_OF_MEMORY);
993 }
994
995 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
996 {
997 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
998
999 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1000 mAppliedIBSerial = mTriangleFanIB->getSerial();
1001 mAppliedIBOffset = indexBufferOffset;
1002 }
1003
1004 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1005}
1006
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001007void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1008{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001009 unsigned int programBinarySerial = programBinary->getSerial();
1010 if (programBinarySerial != mAppliedProgramBinarySerial)
1011 {
1012 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1013 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001014
daniel@transgaming.come4991412012-12-20 20:55:34 +00001015 ID3D11VertexShader *vertexShader = NULL;
1016 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001017
daniel@transgaming.come4991412012-12-20 20:55:34 +00001018 ID3D11PixelShader *pixelShader = NULL;
1019 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001020
daniel@transgaming.come4991412012-12-20 20:55:34 +00001021 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1022 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1023 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001024 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001025
1026 mAppliedProgramBinarySerial = programBinarySerial;
1027 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001028}
1029
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001030void Renderer11::applyUniforms(const gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001031{
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001032 D3D11_BUFFER_DESC constantBufferDescription = {0};
1033 constantBufferDescription.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]);
1034 constantBufferDescription.Usage = D3D11_USAGE_DYNAMIC;
1035 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1036 constantBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1037 constantBufferDescription.MiscFlags = 0;
1038 constantBufferDescription.StructureByteStride = 0;
1039
1040 ID3D11Buffer *constantBufferVS = NULL;
1041 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferVS);
1042 ASSERT(SUCCEEDED(result));
1043
1044 ID3D11Buffer *constantBufferPS = NULL;
1045 result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &constantBufferPS);
1046 ASSERT(SUCCEEDED(result));
1047
1048 D3D11_MAPPED_SUBRESOURCE mapVS = {0};
1049 result = mDeviceContext->Map(constantBufferVS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS);
1050 ASSERT(SUCCEEDED(result));
1051
1052 D3D11_MAPPED_SUBRESOURCE mapPS = {0};
1053 result = mDeviceContext->Map(constantBufferPS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS);
1054 ASSERT(SUCCEEDED(result));
1055
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001056 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001057 {
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001058 const gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001059
1060 switch (uniform->type)
1061 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001062 case GL_SAMPLER_2D:
1063 case GL_SAMPLER_CUBE:
1064 break;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001065 case GL_FLOAT:
1066 case GL_FLOAT_VEC2:
1067 case GL_FLOAT_VEC3:
1068 case GL_FLOAT_VEC4:
1069 case GL_FLOAT_MAT2:
1070 case GL_FLOAT_MAT3:
1071 case GL_FLOAT_MAT4:
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001072 if (uniform->vsRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001073 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001074 GLfloat (*c)[4] = (GLfloat(*)[4])mapVS.pData;
1075 float (*f)[4] = (float(*)[4])uniform->data;
1076
1077 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001078 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001079 c[uniform->vsRegisterIndex + i][0] = f[i][0];
1080 c[uniform->vsRegisterIndex + i][1] = f[i][1];
1081 c[uniform->vsRegisterIndex + i][2] = f[i][2];
1082 c[uniform->vsRegisterIndex + i][3] = f[i][3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001083 }
1084 }
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001085 if (uniform->psRegisterIndex >= 0)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001086 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001087 GLfloat (*c)[4] = (GLfloat(*)[4])mapPS.pData;
1088 float (*f)[4] = (float(*)[4])uniform->data;
1089
1090 for (unsigned int i = 0; i < uniform->registerCount; i++)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001091 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001092 c[uniform->psRegisterIndex + i][0] = f[i][0];
1093 c[uniform->psRegisterIndex + i][1] = f[i][1];
1094 c[uniform->psRegisterIndex + i][2] = f[i][2];
1095 c[uniform->psRegisterIndex + i][3] = f[i][3];
1096 }
1097 }
1098 break;
1099 case GL_INT:
1100 case GL_INT_VEC2:
1101 case GL_INT_VEC3:
1102 case GL_INT_VEC4:
1103 if (uniform->vsRegisterIndex >= 0)
1104 {
1105 int (*c)[4] = (int(*)[4])mapVS.pData;
1106 GLint *x = (GLint*)uniform->data;
1107 int count = gl::VariableColumnCount(uniform->type);
1108
1109 for (unsigned int i = 0; i < uniform->registerCount; i++)
1110 {
1111 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0];
1112 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1];
1113 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2];
1114 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3];
1115 }
1116 }
1117 if (uniform->psRegisterIndex >= 0)
1118 {
1119 int (*c)[4] = (int(*)[4])mapPS.pData;
1120 GLint *x = (GLint*)uniform->data;
1121 int count = gl::VariableColumnCount(uniform->type);
1122
1123 for (unsigned int i = 0; i < uniform->registerCount; i++)
1124 {
1125 if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0];
1126 if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1];
1127 if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2];
1128 if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3];
1129 }
1130 }
1131 break;
1132 case GL_BOOL:
1133 case GL_BOOL_VEC2:
1134 case GL_BOOL_VEC3:
1135 case GL_BOOL_VEC4:
1136 if (uniform->vsRegisterIndex >= 0)
1137 {
1138 int (*c)[4] = (int(*)[4])mapVS.pData;
1139 GLboolean *b = (GLboolean*)uniform->data;
1140 int count = gl::VariableColumnCount(uniform->type);
1141
1142 for (unsigned int i = 0; i < uniform->registerCount; i++)
1143 {
1144 if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0];
1145 if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1];
1146 if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2];
1147 if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3];
1148 }
1149 }
1150 if (uniform->psRegisterIndex >= 0)
1151 {
1152 int (*c)[4] = (int(*)[4])mapPS.pData;
1153 GLboolean *b = (GLboolean*)uniform->data;
1154 int count = gl::VariableColumnCount(uniform->type);
1155
1156 for (unsigned int i = 0; i < uniform->registerCount; i++)
1157 {
1158 if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0];
1159 if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1];
1160 if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2];
1161 if (count >= 4) c[uniform->psRegisterIndex + i][3] = b[i * count + 3];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001162 }
1163 }
1164 break;
1165 default:
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001166 UNREACHABLE();
1167 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001168 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001169
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001170 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001171 memcpy(mapVS.pData, &mVertexConstants, sizeof(dx_VertexConstants));
1172 memcpy(mapPS.pData, &mPixelConstants, sizeof(dx_PixelConstants));
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001173
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001174 mDeviceContext->Unmap(constantBufferVS, 0);
1175 mDeviceContext->VSSetConstantBuffers(0, 1, &constantBufferVS);
1176 constantBufferVS->Release();
1177
1178 mDeviceContext->Unmap(constantBufferPS, 0);
1179 mDeviceContext->PSSetConstantBuffers(0, 1, &constantBufferPS);
1180 constantBufferPS->Release();
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001181}
1182
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001183void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001184{
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001185 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1186 {
1187 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer();
1188 if (renderbufferObject)
1189 {
1190 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1191 if (!renderTarget)
1192 {
1193 ERR("render target pointer unexpectedly null.");
1194 return;
1195 }
1196
1197 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1198 if (!framebufferRTV)
1199 {
1200 ERR("render target view pointer unexpectedly null.");
1201 return;
1202 }
1203
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001204 if (mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1205 mCurScissor.x + mCurScissor.width < renderTarget->getWidth() ||
1206 mCurScissor.y + mCurScissor.height < renderTarget->getHeight()))
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001207 {
1208 // TODO: clearing of subregion of render target
1209 UNIMPLEMENTED();
1210 }
1211
1212 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1213 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1214 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1215 clearParams.colorMaskBlue && alphaUnmasked);
1216
1217 if (needMaskedColorClear)
1218 {
1219 // TODO: masked color clearing
1220 UNIMPLEMENTED();
1221 }
1222 else
1223 {
1224 const float clearValues[4] = { clearParams.colorClearValue.red,
1225 clearParams.colorClearValue.green,
1226 clearParams.colorClearValue.blue,
1227 clearParams.colorClearValue.alpha };
1228 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
1229 }
1230
1231 framebufferRTV->Release();
1232 }
1233 }
1234 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
1235 {
1236 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1237 if (renderbufferObject)
1238 {
daniel@transgaming.comd0f82bc2012-12-20 21:11:42 +00001239 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001240 if (!renderTarget)
1241 {
1242 ERR("render target pointer unexpectedly null.");
1243 return;
1244 }
1245
1246 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1247 if (!framebufferDSV)
1248 {
1249 ERR("depth stencil view pointer unexpectedly null.");
1250 return;
1251 }
1252
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001253 if (mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1254 mCurScissor.x + mCurScissor.width < renderTarget->getWidth() ||
1255 mCurScissor.y + mCurScissor.height < renderTarget->getHeight()))
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001256 {
1257 // TODO: clearing of subregion of depth stencil view
1258 UNIMPLEMENTED();
1259 }
1260
1261 unsigned int stencilUnmasked = 0x0;
1262 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1263 {
1264 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1265 stencilUnmasked = (0x1 << stencilSize) - 1;
1266 }
1267
1268 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1269 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1270
1271 if (needMaskedStencilClear)
1272 {
1273 // TODO: masked clearing of depth stencil
1274 UNIMPLEMENTED();
1275 }
1276 else
1277 {
1278 UINT clearFlags = 0;
1279 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1280 {
1281 clearFlags |= D3D11_CLEAR_DEPTH;
1282 }
1283 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1284 {
1285 clearFlags |= D3D11_CLEAR_STENCIL;
1286 }
1287
1288 float depthClear = clearParams.depthClearValue;
1289 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1290
1291 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
1292 }
1293
1294 framebufferDSV->Release();
1295 }
1296 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001297}
1298
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001299void Renderer11::markAllStateDirty()
1300{
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001301 mAppliedRenderTargetSerial = 0;
1302 mAppliedDepthbufferSerial = 0;
1303 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001304 mDepthStencilInitialized = false;
1305 mRenderTargetDescInitialized = false;
1306
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001307 for (int i = 0; i < gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; i++)
1308 {
1309 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001310 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001311 }
1312 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1313 {
1314 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001315 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001316 }
1317
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001318 mForceSetBlendState = true;
1319 mForceSetRasterState = true;
1320 mForceSetDepthStencilState = true;
1321 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001322 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001323
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001324 mAppliedIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001325 mAppliedIBOffset = 0;
1326
daniel@transgaming.come4991412012-12-20 20:55:34 +00001327 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001328 mDxUniformsDirty = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001329}
1330
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001331void Renderer11::releaseDeviceResources()
1332{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001333 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001334 mInputLayoutCache.clear();
1335
1336 delete mVertexDataManager;
1337 mVertexDataManager = NULL;
1338
1339 delete mIndexDataManager;
1340 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001341
1342 delete mLineLoopIB;
1343 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001344
1345 delete mTriangleFanIB;
1346 mTriangleFanIB = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001347}
1348
1349void Renderer11::markDeviceLost()
1350{
1351 mDeviceLost = true;
1352}
1353
1354bool Renderer11::isDeviceLost()
1355{
1356 return mDeviceLost;
1357}
1358
1359// set notify to true to broadcast a message to all contexts of the device loss
1360bool Renderer11::testDeviceLost(bool notify)
1361{
1362 bool isLost = false;
1363
1364 // TODO
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001365 //UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001366
1367 if (isLost)
1368 {
1369 // ensure we note the device loss --
1370 // we'll probably get this done again by markDeviceLost
1371 // but best to remember it!
1372 // Note that we don't want to clear the device loss status here
1373 // -- this needs to be done by resetDevice
1374 mDeviceLost = true;
1375 if (notify)
1376 {
1377 mDisplay->notifyDeviceLost();
1378 }
1379 }
1380
1381 return isLost;
1382}
1383
1384bool Renderer11::testDeviceResettable()
1385{
1386 HRESULT status = D3D_OK;
1387
1388 // TODO
1389 UNIMPLEMENTED();
1390
1391 switch (status)
1392 {
1393 case D3DERR_DEVICENOTRESET:
1394 case D3DERR_DEVICEHUNG:
1395 return true;
1396 default:
1397 return false;
1398 }
1399}
1400
1401bool Renderer11::resetDevice()
1402{
1403 releaseDeviceResources();
1404
1405 // TODO
1406 UNIMPLEMENTED();
1407
1408 // reset device defaults
1409 initializeDevice();
1410 mDeviceLost = false;
1411
1412 return true;
1413}
1414
1415DWORD Renderer11::getAdapterVendor() const
1416{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001417 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001418}
1419
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001420std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001421{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001422 std::ostringstream rendererString;
1423
1424 rendererString << mDescription;
1425 rendererString << " Direct3D11";
1426
1427 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1428 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1429
1430 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001431}
1432
1433GUID Renderer11::getAdapterIdentifier() const
1434{
1435 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001436 // UNIMPLEMENTED();
1437 GUID foo = {0};
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001438 return foo;
1439}
1440
1441bool Renderer11::getDXT1TextureSupport()
1442{
1443 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001444 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001445 return false;
1446}
1447
1448bool Renderer11::getDXT3TextureSupport()
1449{
1450 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001451 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001452 return false;
1453}
1454
1455bool Renderer11::getDXT5TextureSupport()
1456{
1457 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001458 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001459 return false;
1460}
1461
1462bool Renderer11::getDepthTextureSupport() const
1463{
1464 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001465 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001466 return false;
1467}
1468
1469bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
1470{
1471 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001472 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001473
1474 *filtering = false;
1475 *renderable = false;
1476 return false;
1477}
1478
1479bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
1480{
1481 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001482 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001483
1484 *filtering = false;
1485 *renderable = false;
1486 return false;
1487}
1488
1489bool Renderer11::getLuminanceTextureSupport()
1490{
1491 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001492 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001493 return false;
1494}
1495
1496bool Renderer11::getLuminanceAlphaTextureSupport()
1497{
1498 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001499 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001500 return false;
1501}
1502
1503bool Renderer11::getTextureFilterAnisotropySupport() const
1504{
1505 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001506 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001507 return false;
1508}
1509
1510float Renderer11::getTextureMaxAnisotropy() const
1511{
1512 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001513 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001514 return 1.0f;
1515}
1516
1517bool Renderer11::getEventQuerySupport()
1518{
1519 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001520 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001521 return false;
1522}
1523
1524bool Renderer11::getVertexTextureSupport() const
1525{
1526 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001527 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001528 return false;
1529}
1530
1531bool Renderer11::getNonPower2TextureSupport() const
1532{
1533 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001534 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001535 return false;
1536}
1537
1538bool Renderer11::getOcclusionQuerySupport() const
1539{
1540 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001541 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001542 return false;
1543}
1544
1545bool Renderer11::getInstancingSupport() const
1546{
1547 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001548 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001549 return false;
1550}
1551
1552bool Renderer11::getShareHandleSupport() const
1553{
1554 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001555 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001556
1557 // PIX doesn't seem to support using share handles, so disable them.
1558 return false && !gl::perfActive();
1559}
1560
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001561bool Renderer11::getDerivativeInstructionSupport() const
1562{
1563 // TODO
1564 // UNIMPLEMENTED();
1565 return false;
1566}
1567
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001568int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001569{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001570 switch (mFeatureLevel)
1571 {
1572 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001573 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001574 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
1575 default: UNREACHABLE(); return 0;
1576 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001577}
1578
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001579int Renderer11::getMinorShaderModel() const
1580{
1581 switch (mFeatureLevel)
1582 {
1583 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
1584 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
1585 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
1586 default: UNREACHABLE(); return 0;
1587 }
1588}
1589
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001590float Renderer11::getMaxPointSize() const
1591{
1592 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001593 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001594 return 1.0f;
1595}
1596
1597int Renderer11::getMaxTextureWidth() const
1598{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001599 switch (mFeatureLevel)
1600 {
1601 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1602 case D3D_FEATURE_LEVEL_10_1:
1603 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1604 default: UNREACHABLE(); return 0;
1605 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001606}
1607
1608int Renderer11::getMaxTextureHeight() const
1609{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001610 switch (mFeatureLevel)
1611 {
1612 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
1613 case D3D_FEATURE_LEVEL_10_1:
1614 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
1615 default: UNREACHABLE(); return 0;
1616 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001617}
1618
1619bool Renderer11::get32BitIndexSupport() const
1620{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00001621 switch (mFeatureLevel)
1622 {
1623 case D3D_FEATURE_LEVEL_11_0:
1624 case D3D_FEATURE_LEVEL_10_1:
1625 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
1626 default: UNREACHABLE(); return false;
1627 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001628}
1629
1630int Renderer11::getMinSwapInterval() const
1631{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001632 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001633}
1634
1635int Renderer11::getMaxSwapInterval() const
1636{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00001637 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001638}
1639
1640int Renderer11::getMaxSupportedSamples() const
1641{
1642 // TODO
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001643 // UNIMPLEMENTED();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001644 return 1;
1645}
1646
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001647bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001648{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001649 if (source && dest)
1650 {
1651 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
1652 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
1653
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001654 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001655 return true;
1656 }
1657
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001658 return false;
1659}
1660
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001661bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001662{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001663 if (source && dest)
1664 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001665 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
1666 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001667
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00001668 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00001669 return true;
1670 }
1671
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00001672 return false;
1673}
1674
daniel@transgaming.com38380882012-11-28 19:36:39 +00001675bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001676 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001677{
1678 // TODO
1679 UNIMPLEMENTED();
1680 return false;
1681}
1682
1683bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00001684 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00001685{
1686 // TODO
1687 UNIMPLEMENTED();
1688 return false;
1689}
1690
daniel@transgaming.comf2423652012-11-28 20:53:50 +00001691RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
1692{
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00001693 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
1694 RenderTarget11 *renderTarget = NULL;
1695 if (depth)
1696 {
1697 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), swapChain11->getWidth(), swapChain11->getHeight());
1698 }
1699 else
1700 {
1701 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(), swapChain11->getWidth(), swapChain11->getHeight());
1702 }
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}