blob: 4c544ce55ec5dc798fda9cc21ea4b2d27092f6c2 [file] [log] [blame]
//
// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include <EGL/eglext.h>
#include <versionhelpers.h>
#include <sstream>
#include "common/tls.h"
#include "common/utilities.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Display.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/FramebufferAttachment.h"
#include "libANGLE/Program.h"
#include "libANGLE/State.h"
#include "libANGLE/Surface.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/histogram_macros.h"
#include "libANGLE/renderer/d3d/CompilerD3D.h"
#include "libANGLE/renderer/d3d/DeviceD3D.h"
#include "libANGLE/renderer/d3d/DisplayD3D.h"
#include "libANGLE/renderer/d3d/FramebufferD3D.h"
#include "libANGLE/renderer/d3d/IndexDataManager.h"
#include "libANGLE/renderer/d3d/ProgramD3D.h"
#include "libANGLE/renderer/d3d/RenderbufferD3D.h"
#include "libANGLE/renderer/d3d/ShaderD3D.h"
#include "libANGLE/renderer/d3d/SurfaceD3D.h"
#include "libANGLE/renderer/d3d/TextureD3D.h"
#include "libANGLE/renderer/d3d/VertexDataManager.h"
#include "libANGLE/renderer/d3d/d3d11/Blit11.h"
#include "libANGLE/renderer/d3d/d3d11/Buffer11.h"
#include "libANGLE/renderer/d3d/d3d11/Clear11.h"
#include "libANGLE/renderer/d3d/d3d11/Context11.h"
#include "libANGLE/renderer/d3d/d3d11/Fence11.h"
#include "libANGLE/renderer/d3d/d3d11/Framebuffer11.h"
#include "libANGLE/renderer/d3d/d3d11/Image11.h"
#include "libANGLE/renderer/d3d/d3d11/IndexBuffer11.h"
#include "libANGLE/renderer/d3d/d3d11/PixelTransfer11.h"
#include "libANGLE/renderer/d3d/d3d11/Query11.h"
#include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
#include "libANGLE/renderer/d3d/d3d11/ShaderExecutable11.h"
#include "libANGLE/renderer/d3d/d3d11/StreamProducerNV12.h"
#include "libANGLE/renderer/d3d/d3d11/SwapChain11.h"
#include "libANGLE/renderer/d3d/d3d11/TextureStorage11.h"
#include "libANGLE/renderer/d3d/d3d11/TransformFeedback11.h"
#include "libANGLE/renderer/d3d/d3d11/Trim11.h"
#include "libANGLE/renderer/d3d/d3d11/VertexArray11.h"
#include "libANGLE/renderer/d3d/d3d11/VertexBuffer11.h"
#include "libANGLE/renderer/d3d/d3d11/dxgi_support_table.h"
#include "libANGLE/renderer/d3d/d3d11/formatutils11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
#include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "third_party/trace_event/trace_event.h"
#ifdef ANGLE_ENABLE_WINDOWS_STORE
#include "libANGLE/renderer/d3d/d3d11/winrt/NativeWindow11WinRT.h"
#else
#include "libANGLE/renderer/d3d/d3d11/win32/NativeWindow11Win32.h"
#endif
// Include the D3D9 debug annotator header for use by the desktop D3D11 renderer
// because the D3D11 interface method ID3DUserDefinedAnnotation::GetStatus
// doesn't work with the Graphics Diagnostics tools in Visual Studio 2013.
#ifdef ANGLE_ENABLE_D3D9
#include "libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h"
#endif
// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
#define ANGLE_SKIP_DXGI_1_2_CHECK 0
#endif
#ifdef _DEBUG
// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
// and conformance tests. to enable all warnings, remove this define.
#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
#endif
namespace rx
{
namespace
{
enum
{
MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
};
enum ANGLEFeatureLevel
{
ANGLE_FEATURE_LEVEL_INVALID,
ANGLE_FEATURE_LEVEL_9_3,
ANGLE_FEATURE_LEVEL_10_0,
ANGLE_FEATURE_LEVEL_10_1,
ANGLE_FEATURE_LEVEL_11_0,
ANGLE_FEATURE_LEVEL_11_1,
NUM_ANGLE_FEATURE_LEVELS
};
ANGLEFeatureLevel GetANGLEFeatureLevel(D3D_FEATURE_LEVEL d3dFeatureLevel)
{
switch (d3dFeatureLevel)
{
case D3D_FEATURE_LEVEL_9_3:
return ANGLE_FEATURE_LEVEL_9_3;
case D3D_FEATURE_LEVEL_10_0:
return ANGLE_FEATURE_LEVEL_10_0;
case D3D_FEATURE_LEVEL_10_1:
return ANGLE_FEATURE_LEVEL_10_1;
case D3D_FEATURE_LEVEL_11_0:
return ANGLE_FEATURE_LEVEL_11_0;
// Note: we don't ever request a 11_1 device, because this gives
// an E_INVALIDARG error on systems that don't have the platform update.
case D3D_FEATURE_LEVEL_11_1:
return ANGLE_FEATURE_LEVEL_11_1;
default:
return ANGLE_FEATURE_LEVEL_INVALID;
}
}
void SetLineLoopIndices(GLuint *dest, size_t count)
{
for (size_t i = 0; i < count; i++)
{
dest[i] = static_cast<GLuint>(i);
}
dest[count] = 0;
}
template <typename T>
void CopyLineLoopIndices(const void *indices, GLuint *dest, size_t count)
{
const T *srcPtr = static_cast<const T *>(indices);
for (size_t i = 0; i < count; ++i)
{
dest[i] = static_cast<GLuint>(srcPtr[i]);
}
dest[count] = static_cast<GLuint>(srcPtr[0]);
}
void SetTriangleFanIndices(GLuint *destPtr, size_t numTris)
{
for (size_t i = 0; i < numTris; i++)
{
destPtr[i * 3 + 0] = 0;
destPtr[i * 3 + 1] = static_cast<GLuint>(i) + 1;
destPtr[i * 3 + 2] = static_cast<GLuint>(i) + 2;
}
}
template <typename T>
void CopyLineLoopIndicesWithRestart(const void *indices,
size_t count,
GLenum indexType,
std::vector<GLuint> *bufferOut)
{
GLuint restartIndex = gl::GetPrimitiveRestartIndex(indexType);
GLuint d3dRestartIndex = static_cast<GLuint>(d3d11::GetPrimitiveRestartIndex());
const T *srcPtr = static_cast<const T *>(indices);
Optional<GLuint> currentLoopStart;
bufferOut->clear();
for (size_t indexIdx = 0; indexIdx < count; ++indexIdx)
{
GLuint value = static_cast<GLuint>(srcPtr[indexIdx]);
if (value == restartIndex)
{
if (currentLoopStart.valid())
{
bufferOut->push_back(currentLoopStart.value());
bufferOut->push_back(d3dRestartIndex);
currentLoopStart.reset();
}
}
else
{
bufferOut->push_back(value);
if (!currentLoopStart.valid())
{
currentLoopStart = value;
}
}
}
if (currentLoopStart.valid())
{
bufferOut->push_back(currentLoopStart.value());
}
}
void GetLineLoopIndices(const void *indices,
GLenum indexType,
GLuint count,
bool usePrimitiveRestartFixedIndex,
std::vector<GLuint> *bufferOut)
{
if (indexType != GL_NONE && usePrimitiveRestartFixedIndex)
{
switch (indexType)
{
case GL_UNSIGNED_BYTE:
CopyLineLoopIndicesWithRestart<GLubyte>(indices, count, indexType, bufferOut);
break;
case GL_UNSIGNED_SHORT:
CopyLineLoopIndicesWithRestart<GLushort>(indices, count, indexType, bufferOut);
break;
case GL_UNSIGNED_INT:
CopyLineLoopIndicesWithRestart<GLuint>(indices, count, indexType, bufferOut);
break;
default:
UNREACHABLE();
break;
}
return;
}
// For non-primitive-restart draws, the index count is static.
bufferOut->resize(static_cast<size_t>(count) + 1);
switch (indexType)
{
// Non-indexed draw
case GL_NONE:
SetLineLoopIndices(&(*bufferOut)[0], count);
break;
case GL_UNSIGNED_BYTE:
CopyLineLoopIndices<GLubyte>(indices, &(*bufferOut)[0], count);
break;
case GL_UNSIGNED_SHORT:
CopyLineLoopIndices<GLushort>(indices, &(*bufferOut)[0], count);
break;
case GL_UNSIGNED_INT:
CopyLineLoopIndices<GLuint>(indices, &(*bufferOut)[0], count);
break;
default:
UNREACHABLE();
break;
}
}
template <typename T>
void CopyTriangleFanIndices(const void *indices, GLuint *destPtr, size_t numTris)
{
const T *srcPtr = static_cast<const T *>(indices);
for (size_t i = 0; i < numTris; i++)
{
destPtr[i * 3 + 0] = static_cast<GLuint>(srcPtr[0]);
destPtr[i * 3 + 1] = static_cast<GLuint>(srcPtr[i + 1]);
destPtr[i * 3 + 2] = static_cast<GLuint>(srcPtr[i + 2]);
}
}
template <typename T>
void CopyTriangleFanIndicesWithRestart(const void *indices,
GLuint indexCount,
GLenum indexType,
std::vector<GLuint> *bufferOut)
{
GLuint restartIndex = gl::GetPrimitiveRestartIndex(indexType);
GLuint d3dRestartIndex = gl::GetPrimitiveRestartIndex(GL_UNSIGNED_INT);
const T *srcPtr = static_cast<const T *>(indices);
Optional<GLuint> vertexA;
Optional<GLuint> vertexB;
bufferOut->clear();
for (size_t indexIdx = 0; indexIdx < indexCount; ++indexIdx)
{
GLuint value = static_cast<GLuint>(srcPtr[indexIdx]);
if (value == restartIndex)
{
bufferOut->push_back(d3dRestartIndex);
vertexA.reset();
vertexB.reset();
}
else
{
if (!vertexA.valid())
{
vertexA = value;
}
else if (!vertexB.valid())
{
vertexB = value;
}
else
{
bufferOut->push_back(vertexA.value());
bufferOut->push_back(vertexB.value());
bufferOut->push_back(value);
vertexB = value;
}
}
}
}
void GetTriFanIndices(const void *indices,
GLenum indexType,
GLuint count,
bool usePrimitiveRestartFixedIndex,
std::vector<GLuint> *bufferOut)
{
if (indexType != GL_NONE && usePrimitiveRestartFixedIndex)
{
switch (indexType)
{
case GL_UNSIGNED_BYTE:
CopyTriangleFanIndicesWithRestart<GLubyte>(indices, count, indexType, bufferOut);
break;
case GL_UNSIGNED_SHORT:
CopyTriangleFanIndicesWithRestart<GLushort>(indices, count, indexType, bufferOut);
break;
case GL_UNSIGNED_INT:
CopyTriangleFanIndicesWithRestart<GLuint>(indices, count, indexType, bufferOut);
break;
default:
UNREACHABLE();
break;
}
return;
}
// For non-primitive-restart draws, the index count is static.
GLuint numTris = count - 2;
bufferOut->resize(numTris * 3);
switch (indexType)
{
// Non-indexed draw
case GL_NONE:
SetTriangleFanIndices(&(*bufferOut)[0], numTris);
break;
case GL_UNSIGNED_BYTE:
CopyTriangleFanIndices<GLubyte>(indices, &(*bufferOut)[0], numTris);
break;
case GL_UNSIGNED_SHORT:
CopyTriangleFanIndices<GLushort>(indices, &(*bufferOut)[0], numTris);
break;
case GL_UNSIGNED_INT:
CopyTriangleFanIndices<GLuint>(indices, &(*bufferOut)[0], numTris);
break;
default:
UNREACHABLE();
break;
}
}
int GetWrapBits(GLenum wrap)
{
switch (wrap)
{
case GL_CLAMP_TO_EDGE:
return 0x1;
case GL_REPEAT:
return 0x2;
case GL_MIRRORED_REPEAT:
return 0x3;
default:
UNREACHABLE();
return 0;
}
}
const uint32_t ScratchMemoryBufferLifetime = 1000;
} // anonymous namespace
Renderer11::Renderer11(egl::Display *display)
: RendererD3D(display),
mStateCache(this),
mStateManager(this),
mLastHistogramUpdateTime(
ANGLEPlatformCurrent()->monotonicallyIncreasingTime(ANGLEPlatformCurrent())),
mDebug(nullptr),
mScratchMemoryBuffer(ScratchMemoryBufferLifetime),
mAnnotator(nullptr)
{
mVertexDataManager = nullptr;
mIndexDataManager = nullptr;
mLineLoopIB = nullptr;
mTriangleFanIB = nullptr;
mAppliedIBChanged = false;
mBlit = nullptr;
mPixelTransfer = nullptr;
mClear = nullptr;
mTrim = nullptr;
mSyncQuery = nullptr;
mRenderer11DeviceCaps.supportsClearView = false;
mRenderer11DeviceCaps.supportsConstantBufferOffsets = false;
mRenderer11DeviceCaps.supportsDXGI1_2 = false;
mRenderer11DeviceCaps.B5G6R5support = 0;
mRenderer11DeviceCaps.B4G4R4A4support = 0;
mRenderer11DeviceCaps.B5G5R5A1support = 0;
mD3d11Module = nullptr;
mDxgiModule = nullptr;
mDCompModule = nullptr;
mCreatedWithDeviceEXT = false;
mEGLDevice = nullptr;
mDevice = nullptr;
mDeviceContext = nullptr;
mDeviceContext1 = nullptr;
mDxgiAdapter = nullptr;
mDxgiFactory = nullptr;
mDriverConstantBufferVS = nullptr;
mDriverConstantBufferPS = nullptr;
mDriverConstantBufferCS = nullptr;
mAppliedVertexShader = angle::DirtyPointer;
mAppliedGeometryShader = angle::DirtyPointer;
mAppliedPixelShader = angle::DirtyPointer;
mAppliedComputeShader = angle::DirtyPointer;
mAppliedTFObject = angle::DirtyPointer;
ZeroMemory(&mAdapterDescription, sizeof(mAdapterDescription));
if (mDisplay->getPlatform() == EGL_PLATFORM_ANGLE_ANGLE)
{
const auto &attributes = mDisplay->getAttributeMap();
EGLint requestedMajorVersion = static_cast<EGLint>(
attributes.get(EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, EGL_DONT_CARE));
EGLint requestedMinorVersion = static_cast<EGLint>(
attributes.get(EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, EGL_DONT_CARE));
// Only allow feature level 10 on starboard.
#if defined(STARBOARD)
mAvailableFeatureLevels.push_back(D3D_FEATURE_LEVEL_10_0);
#else
if (requestedMajorVersion == EGL_DONT_CARE || requestedMajorVersion >= 11)
{
if (requestedMinorVersion == EGL_DONT_CARE || requestedMinorVersion >= 0)
{
mAvailableFeatureLevels.push_back(D3D_FEATURE_LEVEL_11_0);
}
}
if (requestedMajorVersion == EGL_DONT_CARE || requestedMajorVersion >= 10)
{
if (requestedMinorVersion == EGL_DONT_CARE || requestedMinorVersion >= 1)
{
mAvailableFeatureLevels.push_back(D3D_FEATURE_LEVEL_10_1);
}
if (requestedMinorVersion == EGL_DONT_CARE || requestedMinorVersion >= 0)
{
mAvailableFeatureLevels.push_back(D3D_FEATURE_LEVEL_10_0);
}
}
if (requestedMajorVersion == 9 && requestedMinorVersion == 3)
{
mAvailableFeatureLevels.push_back(D3D_FEATURE_LEVEL_9_3);
}
#endif // STARBOARD
EGLint requestedDeviceType = static_cast<EGLint>(attributes.get(
EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE));
switch (requestedDeviceType)
{
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE:
mRequestedDriverType = D3D_DRIVER_TYPE_HARDWARE;
break;
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE:
mRequestedDriverType = D3D_DRIVER_TYPE_WARP;
break;
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_REFERENCE_ANGLE:
mRequestedDriverType = D3D_DRIVER_TYPE_REFERENCE;
break;
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE:
mRequestedDriverType = D3D_DRIVER_TYPE_NULL;
break;
default:
UNREACHABLE();
}
const EGLenum presentPath = static_cast<EGLenum>(attributes.get(
EGL_EXPERIMENTAL_PRESENT_PATH_ANGLE, EGL_EXPERIMENTAL_PRESENT_PATH_COPY_ANGLE));
mPresentPathFastEnabled = (presentPath == EGL_EXPERIMENTAL_PRESENT_PATH_FAST_ANGLE);
}
else if (display->getPlatform() == EGL_PLATFORM_DEVICE_EXT)
{
mEGLDevice = GetImplAs<DeviceD3D>(display->getDevice());
ASSERT(mEGLDevice != nullptr);
mCreatedWithDeviceEXT = true;
// Also set EGL_PLATFORM_ANGLE_ANGLE variables, in case they're used elsewhere in ANGLE
// mAvailableFeatureLevels defaults to empty
mRequestedDriverType = D3D_DRIVER_TYPE_UNKNOWN;
mPresentPathFastEnabled = false;
}
// The D3D11 renderer must choose the D3D9 debug annotator because the D3D11 interface
// method ID3DUserDefinedAnnotation::GetStatus on desktop builds doesn't work with the Graphics
// Diagnostics tools in Visual Studio 2013.
// The D3D9 annotator works properly for both D3D11 and D3D9.
// Incorrect status reporting can cause ANGLE to log unnecessary debug events.
#ifdef ANGLE_ENABLE_D3D9
mAnnotator = new DebugAnnotator9();
#else
mAnnotator = new DebugAnnotator11();
#endif
ASSERT(mAnnotator);
gl::InitializeDebugAnnotations(mAnnotator);
}
Renderer11::~Renderer11()
{
release();
}
#ifndef __d3d11_1_h__
#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
#endif
egl::Error Renderer11::initialize()
{
HRESULT result = S_OK;
ANGLE_TRY(initializeD3DDevice());
#if !defined(ANGLE_ENABLE_WINDOWS_STORE)
#if !ANGLE_SKIP_DXGI_1_2_CHECK
{
TRACE_EVENT0("gpu.angle", "Renderer11::initialize (DXGICheck)");
// In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is
// required.
// The easiest way to check is to query for a IDXGIDevice2.
bool requireDXGI1_2 = false;
HWND hwnd = WindowFromDC(mDisplay->getNativeDisplayId());
if (hwnd)
{
DWORD currentProcessId = GetCurrentProcessId();
DWORD wndProcessId;
GetWindowThreadProcessId(hwnd, &wndProcessId);
requireDXGI1_2 = (currentProcessId != wndProcessId);
}
else
{
requireDXGI1_2 = true;
}
if (requireDXGI1_2)
{
IDXGIDevice2 *dxgiDevice2 = nullptr;
result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void **)&dxgiDevice2);
if (FAILED(result))
{
return egl::Error(
EGL_NOT_INITIALIZED, D3D11_INIT_INCOMPATIBLE_DXGI,
"DXGI 1.2 required to present to HWNDs owned by another process.");
}
SafeRelease(dxgiDevice2);
}
}
#endif
#endif
{
TRACE_EVENT0("gpu.angle", "Renderer11::initialize (ComQueries)");
// Cast the DeviceContext to a DeviceContext1.
// This could fail on Windows 7 without the Platform Update.
// Don't error in this case- just don't use mDeviceContext1.
mDeviceContext1 = d3d11::DynamicCastComObject<ID3D11DeviceContext1>(mDeviceContext);
IDXGIDevice *dxgiDevice = nullptr;
result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&dxgiDevice);
if (FAILED(result))
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_OTHER_ERROR,
"Could not query DXGI device.");
}
result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&mDxgiAdapter);
if (FAILED(result))
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_OTHER_ERROR,
"Could not retrieve DXGI adapter");
}
SafeRelease(dxgiDevice);
IDXGIAdapter2 *dxgiAdapter2 = d3d11::DynamicCastComObject<IDXGIAdapter2>(mDxgiAdapter);
// On D3D_FEATURE_LEVEL_9_*, IDXGIAdapter::GetDesc returns "Software Adapter" for the
// description string.
// If DXGI1.2 is available then IDXGIAdapter2::GetDesc2 can be used to get the actual
// hardware values.
if (mRenderer11DeviceCaps.featureLevel <= D3D_FEATURE_LEVEL_9_3 && dxgiAdapter2 != nullptr)
{
DXGI_ADAPTER_DESC2 adapterDesc2 = {};
result = dxgiAdapter2->GetDesc2(&adapterDesc2);
if (SUCCEEDED(result))
{
// Copy the contents of the DXGI_ADAPTER_DESC2 into mAdapterDescription (a
// DXGI_ADAPTER_DESC).
memcpy(mAdapterDescription.Description, adapterDesc2.Description,
sizeof(mAdapterDescription.Description));
mAdapterDescription.VendorId = adapterDesc2.VendorId;
mAdapterDescription.DeviceId = adapterDesc2.DeviceId;
mAdapterDescription.SubSysId = adapterDesc2.SubSysId;
mAdapterDescription.Revision = adapterDesc2.Revision;
mAdapterDescription.DedicatedVideoMemory = adapterDesc2.DedicatedVideoMemory;
mAdapterDescription.DedicatedSystemMemory = adapterDesc2.DedicatedSystemMemory;
mAdapterDescription.SharedSystemMemory = adapterDesc2.SharedSystemMemory;
mAdapterDescription.AdapterLuid = adapterDesc2.AdapterLuid;
}
}
else
{
result = mDxgiAdapter->GetDesc(&mAdapterDescription);
}
SafeRelease(dxgiAdapter2);
if (FAILED(result))
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_OTHER_ERROR,
"Could not read DXGI adaptor description.");
}
memset(mDescription, 0, sizeof(mDescription));
wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&mDxgiFactory);
if (!mDxgiFactory || FAILED(result))
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_OTHER_ERROR,
"Could not create DXGI factory.");
}
}
// Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
{
TRACE_EVENT0("gpu.angle", "Renderer11::initialize (HideWarnings)");
ID3D11InfoQueue *infoQueue;
result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
if (SUCCEEDED(result))
{
D3D11_MESSAGE_ID hideMessages[] = {
D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET};
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = static_cast<unsigned int>(ArraySize(hideMessages));
filter.DenyList.pIDList = hideMessages;
infoQueue->AddStorageFilterEntries(&filter);
SafeRelease(infoQueue);
}
}
#endif
#if !defined(NDEBUG)
mDebug = d3d11::DynamicCastComObject<ID3D11Debug>(mDevice);
#endif
initializeDevice();
return egl::Error(EGL_SUCCESS);
}
egl::Error Renderer11::initializeD3DDevice()
{
HRESULT result = S_OK;
if (!mCreatedWithDeviceEXT)
{
#if !defined(ANGLE_ENABLE_WINDOWS_STORE)
PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = nullptr;
{
SCOPED_ANGLE_HISTOGRAM_TIMER("GPU.ANGLE.Renderer11InitializeDLLsMS");
TRACE_EVENT0("gpu.angle", "Renderer11::initialize (Load DLLs)");
mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
mDCompModule = LoadLibrary(TEXT("dcomp.dll"));
if (mD3d11Module == nullptr || mDxgiModule == nullptr)
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_MISSING_DEP,
"Could not load D3D11 or DXGI library.");
}
// create the D3D11 device
ASSERT(mDevice == nullptr);
D3D11CreateDevice = reinterpret_cast<PFN_D3D11_CREATE_DEVICE>(
GetProcAddress(mD3d11Module, "D3D11CreateDevice"));
if (D3D11CreateDevice == nullptr)
{
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_MISSING_DEP,
"Could not retrieve D3D11CreateDevice address.");
}
}
#endif
#ifdef _DEBUG
{
TRACE_EVENT0("gpu.angle", "D3D11CreateDevice (Debug)");
result = D3D11CreateDevice(nullptr, mRequestedDriverType, nullptr,
D3D11_CREATE_DEVICE_DEBUG, mAvailableFeatureLevels.data(),
static_cast<unsigned int>(mAvailableFeatureLevels.size()),
D3D11_SDK_VERSION, &mDevice,
&(mRenderer11DeviceCaps.featureLevel), &mDeviceContext);
}
if (!mDevice || FAILED(result))
{
WARN() << "Failed creating Debug D3D11 device - falling back to release runtime.";
}
if (!mDevice || FAILED(result))
#endif
{
SCOPED_ANGLE_HISTOGRAM_TIMER("GPU.ANGLE.D3D11CreateDeviceMS");
TRACE_EVENT0("gpu.angle", "D3D11CreateDevice");
result = D3D11CreateDevice(
nullptr, mRequestedDriverType, nullptr, 0, mAvailableFeatureLevels.data(),
static_cast<unsigned int>(mAvailableFeatureLevels.size()), D3D11_SDK_VERSION,
&mDevice, &(mRenderer11DeviceCaps.featureLevel), &mDeviceContext);
// Cleanup done by destructor
if (!mDevice || FAILED(result))
{
ANGLE_HISTOGRAM_SPARSE_SLOWLY("GPU.ANGLE.D3D11CreateDeviceError",
static_cast<int>(result));
return egl::Error(EGL_NOT_INITIALIZED, D3D11_INIT_CREATEDEVICE_ERROR,
"Could not create D3D11 device.");
}
}
}
else
{
// We should use the inputted D3D11 device instead
void *device = nullptr;
ANGLE_TRY(mEGLDevice->getDevice(&device));
ID3D11Device *d3dDevice = reinterpret_cast<ID3D11Device *>(device);
if (FAILED(d3dDevice->GetDeviceRemovedReason()))
{
return egl::Error(EGL_NOT_INITIALIZED, "Inputted D3D11 device has been lost.");
}
if (d3dDevice->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3)
{
return egl::Error(EGL_NOT_INITIALIZED,
"Inputted D3D11 device must be Feature Level 9_3 or greater.");
}
// The Renderer11 adds a ref to the inputted D3D11 device, like D3D11CreateDevice does.
mDevice = d3dDevice;
mDevice->AddRef();
mDevice->GetImmediateContext(&mDeviceContext);
mRenderer11DeviceCaps.featureLevel = mDevice->GetFeatureLevel();
}
d3d11::SetDebugName(mDeviceContext, "DeviceContext");
return egl::Error(EGL_SUCCESS);
}
// do any one-time device initialization
// NOTE: this is also needed after a device lost/reset
// to reset the scene status and ensure the default states are reset.
void Renderer11::initializeDevice()
{
SCOPED_ANGLE_HISTOGRAM_TIMER("GPU.ANGLE.Renderer11InitializeDeviceMS");
TRACE_EVENT0("gpu.angle", "Renderer11::initializeDevice");
populateRenderer11DeviceCaps();
mStateCache.initialize(mDevice);
mInputLayoutCache.initialize(mDevice, mDeviceContext);
ASSERT(!mVertexDataManager && !mIndexDataManager);
mVertexDataManager = new VertexDataManager(this);
mIndexDataManager = new IndexDataManager(this, getRendererClass());
ASSERT(!mBlit);
mBlit = new Blit11(this);
ASSERT(!mClear);
mClear = new Clear11(this);
const auto &attributes = mDisplay->getAttributeMap();
// If automatic trim is enabled, DXGIDevice3::Trim( ) is called for the application
// automatically when an application is suspended by the OS. This feature is currently
// only supported for Windows Store applications.
EGLint enableAutoTrim = static_cast<EGLint>(
attributes.get(EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_FALSE));
if (enableAutoTrim == EGL_TRUE)
{
ASSERT(!mTrim);
mTrim = new Trim11(this);
}
ASSERT(!mPixelTransfer);
mPixelTransfer = new PixelTransfer11(this);
const gl::Caps &rendererCaps = getNativeCaps();
mStateManager.initialize(rendererCaps);
mForceSetVertexSamplerStates.resize(rendererCaps.maxVertexTextureImageUnits);
mCurVertexSamplerStates.resize(rendererCaps.maxVertexTextureImageUnits);
mSamplerMetadataVS.initData(rendererCaps.maxVertexTextureImageUnits);
mForceSetPixelSamplerStates.resize(rendererCaps.maxTextureImageUnits);
mCurPixelSamplerStates.resize(rendererCaps.maxTextureImageUnits);
mSamplerMetadataPS.initData(rendererCaps.maxTextureImageUnits);
mForceSetComputeSamplerStates.resize(rendererCaps.maxComputeTextureImageUnits);
mCurComputeSamplerStates.resize(rendererCaps.maxComputeTextureImageUnits);
mSamplerMetadataCS.initData(rendererCaps.maxComputeTextureImageUnits);
mStateManager.initialize(rendererCaps);
markAllStateDirty();
// Gather stats on DXGI and D3D feature level
ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.SupportsDXGI1_2", mRenderer11DeviceCaps.supportsDXGI1_2);
ANGLEFeatureLevel angleFeatureLevel = GetANGLEFeatureLevel(mRenderer11DeviceCaps.featureLevel);
// We don't actually request a 11_1 device, because of complications with the platform
// update. Instead we check if the mDeviceContext1 pointer cast succeeded.
// Note: we should support D3D11_0 always, but we aren't guaranteed to be at FL11_0
// because the app can specify a lower version (such as 9_3) on Display creation.
if (mDeviceContext1 != nullptr)
{
angleFeatureLevel = ANGLE_FEATURE_LEVEL_11_1;
}
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.D3D11FeatureLevel", angleFeatureLevel,
NUM_ANGLE_FEATURE_LEVELS);
}
void Renderer11::populateRenderer11DeviceCaps()
{
HRESULT hr = S_OK;
LARGE_INTEGER version;
hr = mDxgiAdapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &version);
if (FAILED(hr))
{
mRenderer11DeviceCaps.driverVersion.reset();
ERR() << "Error querying driver version from DXGI Adapter.";
}
else
{
mRenderer11DeviceCaps.driverVersion = version;
}
if (mDeviceContext1)
{
D3D11_FEATURE_DATA_D3D11_OPTIONS d3d11Options;
HRESULT result = mDevice->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &d3d11Options,
sizeof(D3D11_FEATURE_DATA_D3D11_OPTIONS));
if (SUCCEEDED(result))
{
mRenderer11DeviceCaps.supportsClearView = (d3d11Options.ClearView != FALSE);
mRenderer11DeviceCaps.supportsConstantBufferOffsets =
(d3d11Options.ConstantBufferOffsetting != FALSE);
}
}
if (getWorkarounds().disableB5G6R5Support)
{
mRenderer11DeviceCaps.B5G6R5support = 0;
}
else
{
hr = mDevice->CheckFormatSupport(DXGI_FORMAT_B5G6R5_UNORM,
&(mRenderer11DeviceCaps.B5G6R5support));
if (FAILED(hr))
{
mRenderer11DeviceCaps.B5G6R5support = 0;
}
}
hr = mDevice->CheckFormatSupport(DXGI_FORMAT_B4G4R4A4_UNORM,
&(mRenderer11DeviceCaps.B4G4R4A4support));
if (FAILED(hr))
{
mRenderer11DeviceCaps.B4G4R4A4support = 0;
}
hr = mDevice->CheckFormatSupport(DXGI_FORMAT_B5G5R5A1_UNORM,
&(mRenderer11DeviceCaps.B5G5R5A1support));
if (FAILED(hr))
{
mRenderer11DeviceCaps.B5G5R5A1support = 0;
}
IDXGIAdapter2 *dxgiAdapter2 = d3d11::DynamicCastComObject<IDXGIAdapter2>(mDxgiAdapter);
mRenderer11DeviceCaps.supportsDXGI1_2 = (dxgiAdapter2 != nullptr);
SafeRelease(dxgiAdapter2);
}
gl::SupportedSampleSet Renderer11::generateSampleSetFromCaps(
const gl::TextureCaps &colorBufferFormatCaps,
const gl::TextureCaps &depthStencilBufferFormatCaps) const
{
gl::SupportedSampleSet sampleCounts;
// Generate a new set from the set intersection of sample counts between the color and depth
// format caps.
std::set_intersection(colorBufferFormatCaps.sampleCounts.begin(),
colorBufferFormatCaps.sampleCounts.end(),
depthStencilBufferFormatCaps.sampleCounts.begin(),
depthStencilBufferFormatCaps.sampleCounts.end(),
std::inserter(sampleCounts, sampleCounts.begin()));
// Format of GL_NONE results in no supported sample counts.
// Add back the color sample counts to the supported sample set.
if (depthStencilBufferFormatCaps.sampleCounts.empty())
{
sampleCounts = colorBufferFormatCaps.sampleCounts;
}
else if (colorBufferFormatCaps.sampleCounts.empty())
{
// Likewise, add back the depth sample counts to the supported sample set.
sampleCounts = depthStencilBufferFormatCaps.sampleCounts;
}
// In EGL, no multisampling is 0, in D3D its 1, so if 1 exists, insert 0 for EGL to match.
if (sampleCounts.find(1) != sampleCounts.end())
{
sampleCounts.insert(0);
}
return sampleCounts;
}
egl::ConfigSet Renderer11::generateConfigs()
{
std::vector<GLenum> colorBufferFormats;
// 32-bit supported formats
colorBufferFormats.push_back(GL_BGRA8_EXT);
colorBufferFormats.push_back(GL_RGBA8_OES);
// 24-bit supported formats
colorBufferFormats.push_back(GL_RGB8_OES);
if (mRenderer11DeviceCaps.featureLevel >= D3D_FEATURE_LEVEL_10_0)
{
// Additional high bit depth formats added in D3D 10.0
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb173064.aspx
colorBufferFormats.push_back(GL_RGBA16F);
colorBufferFormats.push_back(GL_RGB10_A2);
}
if (!mPresentPathFastEnabled)
{
// 16-bit supported formats
// These aren't valid D3D11 swapchain formats, so don't expose them as configs
// if present path fast is active
colorBufferFormats.push_back(GL_RGBA4);
colorBufferFormats.push_back(GL_RGB5_A1);
colorBufferFormats.push_back(GL_RGB565);
}
static const GLenum depthStencilBufferFormats[] = {
GL_NONE, GL_DEPTH24_STENCIL8_OES, GL_DEPTH_COMPONENT16,
};
const gl::Caps &rendererCaps = getNativeCaps();
const gl::TextureCapsMap &rendererTextureCaps = getNativeTextureCaps();
const EGLint optimalSurfaceOrientation =
mPresentPathFastEnabled ? 0 : EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE;
egl::ConfigSet configs;
for (GLenum colorBufferInternalFormat : colorBufferFormats)
{
const gl::TextureCaps &colorBufferFormatCaps =
rendererTextureCaps.get(colorBufferInternalFormat);
if (!colorBufferFormatCaps.renderable)
{
continue;
}
for (GLenum depthStencilBufferInternalFormat : depthStencilBufferFormats)
{
const gl::TextureCaps &depthStencilBufferFormatCaps =
rendererTextureCaps.get(depthStencilBufferInternalFormat);
if (!depthStencilBufferFormatCaps.renderable &&
depthStencilBufferInternalFormat != GL_NONE)
{
continue;
}
const gl::InternalFormat &colorBufferFormatInfo =
gl::GetSizedInternalFormatInfo(colorBufferInternalFormat);
const gl::InternalFormat &depthStencilBufferFormatInfo =
gl::GetSizedInternalFormatInfo(depthStencilBufferInternalFormat);
const gl::Version &maxVersion = getMaxSupportedESVersion();
const gl::SupportedSampleSet sampleCounts =
generateSampleSetFromCaps(colorBufferFormatCaps, depthStencilBufferFormatCaps);
for (GLuint sampleCount : sampleCounts)
{
egl::Config config;
config.renderTargetFormat = colorBufferInternalFormat;
config.depthStencilFormat = depthStencilBufferInternalFormat;
config.bufferSize = colorBufferFormatInfo.pixelBytes * 8;
config.redSize = colorBufferFormatInfo.redBits;
config.greenSize = colorBufferFormatInfo.greenBits;
config.blueSize = colorBufferFormatInfo.blueBits;
config.luminanceSize = colorBufferFormatInfo.luminanceBits;
config.alphaSize = colorBufferFormatInfo.alphaBits;
config.alphaMaskSize = 0;
config.bindToTextureRGB =
((colorBufferFormatInfo.format == GL_RGB) && (sampleCount <= 1));
config.bindToTextureRGBA = (((colorBufferFormatInfo.format == GL_RGBA) ||
(colorBufferFormatInfo.format == GL_BGRA_EXT)) &&
(sampleCount <= 1));
config.colorBufferType = EGL_RGB_BUFFER;
config.configCaveat = EGL_NONE;
config.configID = static_cast<EGLint>(configs.size() + 1);
// PresentPathFast may not be conformant
config.conformant = 0;
if (!mPresentPathFastEnabled)
{
// Can only support a conformant ES2 with feature level greater than 10.0.
if (mRenderer11DeviceCaps.featureLevel >= D3D_FEATURE_LEVEL_10_0)
{
config.conformant |= EGL_OPENGL_ES2_BIT;
}
// We can only support conformant ES3 on FL 10.1+
if (maxVersion.major >= 3)
{
config.conformant |= EGL_OPENGL_ES3_BIT_KHR;
}
}
config.depthSize = depthStencilBufferFormatInfo.depthBits;
config.level = 0;
config.matchNativePixmap = EGL_NONE;
config.maxPBufferWidth = rendererCaps.max2DTextureSize;
config.maxPBufferHeight = rendererCaps.max2DTextureSize;
config.maxPBufferPixels =
rendererCaps.max2DTextureSize * rendererCaps.max2DTextureSize;
config.maxSwapInterval = 4;
config.minSwapInterval = 0;
config.nativeRenderable = EGL_FALSE;
config.nativeVisualID = 0;
config.nativeVisualType = EGL_NONE;
// Can't support ES3 at all without feature level 10.1
config.renderableType = EGL_OPENGL_ES2_BIT;
if (maxVersion.major >= 3)
{
config.renderableType |= EGL_OPENGL_ES3_BIT_KHR;
}
config.sampleBuffers = (sampleCount == 0) ? 0 : 1;
config.samples = sampleCount;
config.stencilSize = depthStencilBufferFormatInfo.stencilBits;
config.surfaceType =
EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
config.transparentType = EGL_NONE;
config.transparentRedValue = 0;
config.transparentGreenValue = 0;
config.transparentBlueValue = 0;
config.optimalOrientation = optimalSurfaceOrientation;
config.colorComponentType = gl_egl::GLComponentTypeToEGLColorComponentType(
colorBufferFormatInfo.componentType);
configs.add(config);
}
}
}
ASSERT(configs.size() > 0);
return configs;
}
void Renderer11::generateDisplayExtensions(egl::DisplayExtensions *outExtensions) const
{
outExtensions->createContextRobustness = true;
if (getShareHandleSupport())
{
outExtensions->d3dShareHandleClientBuffer = true;
outExtensions->surfaceD3DTexture2DShareHandle = true;
}
outExtensions->d3dTextureClientBuffer = true;
outExtensions->keyedMutex = true;
outExtensions->querySurfacePointer = true;
outExtensions->windowFixedSize = true;
// If present path fast is active then the surface orientation extension isn't supported
outExtensions->surfaceOrientation = !mPresentPathFastEnabled;
// D3D11 does not support present with dirty rectangles until DXGI 1.2.
outExtensions->postSubBuffer = mRenderer11DeviceCaps.supportsDXGI1_2;
outExtensions->deviceQuery = true;
outExtensions->image = true;
outExtensions->imageBase = true;
outExtensions->glTexture2DImage = true;
outExtensions->glTextureCubemapImage = true;
outExtensions->glRenderbufferImage = true;
outExtensions->stream = true;
outExtensions->streamConsumerGLTexture = true;
outExtensions->streamConsumerGLTextureYUV = true;
// Not all D3D11 devices support NV12 textures
if (getNV12TextureSupport())
{
outExtensions->streamProducerD3DTextureNV12 = true;
}
outExtensions->flexibleSurfaceCompatibility = true;
outExtensions->directComposition = !!mDCompModule;
// Contexts are virtualized so textures can be shared globally
outExtensions->displayTextureShareGroup = true;
outExtensions->createContextRobustResourceInitialization = true;
// getSyncValues requires direct composition.
outExtensions->getSyncValues = outExtensions->directComposition;
}
gl::Error Renderer11::flush()
{
mDeviceContext->Flush();
return gl::NoError();
}
gl::Error Renderer11::finish()
{
HRESULT result;
if (!mSyncQuery)
{
D3D11_QUERY_DESC queryDesc;
queryDesc.Query = D3D11_QUERY_EVENT;
queryDesc.MiscFlags = 0;
result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create event query, result: 0x%X.",
result);
}
}
mDeviceContext->End(mSyncQuery);
unsigned int attempt = 0;
do
{
unsigned int flushFrequency = 100;
UINT flags = (attempt % flushFrequency == 0) ? 0 : D3D11_ASYNC_GETDATA_DONOTFLUSH;
attempt++;
result = mDeviceContext->GetData(mSyncQuery, nullptr, 0, flags);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to get event query data, result: 0x%X.",
result);
}
if (result == S_FALSE)
{
// Keep polling, but allow other threads to do something useful first
ScheduleYield();
}
if (testDeviceLost())
{
mDisplay->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY, "Device was lost while waiting for sync.");
}
} while (result == S_FALSE);
return gl::NoError();
}
bool Renderer11::isValidNativeWindow(EGLNativeWindowType window) const
{
#ifdef ANGLE_ENABLE_WINDOWS_STORE
return NativeWindow11WinRT::IsValidNativeWindow(window);
#else
return NativeWindow11Win32::IsValidNativeWindow(window);
#endif
}
NativeWindowD3D *Renderer11::createNativeWindow(EGLNativeWindowType window,
const egl::Config *config,
const egl::AttributeMap &attribs) const
{
#ifdef ANGLE_ENABLE_WINDOWS_STORE
UNUSED_VARIABLE(attribs);
return new NativeWindow11WinRT(window, config->alphaSize > 0);
#else
return new NativeWindow11Win32(
window, config->alphaSize > 0,
attribs.get(EGL_DIRECT_COMPOSITION_ANGLE, EGL_FALSE) == EGL_TRUE);
#endif
}
egl::Error Renderer11::getD3DTextureInfo(const egl::Config *configuration,
IUnknown *d3dTexture,
EGLint *width,
EGLint *height,
GLenum *fboFormat) const
{
ID3D11Texture2D *texture = d3d11::DynamicCastComObject<ID3D11Texture2D>(d3dTexture);
if (texture == nullptr)
{
return egl::Error(EGL_BAD_PARAMETER, "client buffer is not a ID3D11Texture2D");
}
ID3D11Device *textureDevice = nullptr;
texture->GetDevice(&textureDevice);
if (textureDevice != mDevice)
{
SafeRelease(texture);
return egl::Error(EGL_BAD_PARAMETER, "Texture's device does not match.");
}
SafeRelease(textureDevice);
D3D11_TEXTURE2D_DESC desc = {0};
texture->GetDesc(&desc);
SafeRelease(texture);
if (width)
{
*width = static_cast<EGLint>(desc.Width);
}
if (height)
{
*height = static_cast<EGLint>(desc.Height);
}
if (static_cast<EGLint>(desc.SampleDesc.Count) != configuration->samples)
{
// Both the texture and EGL config sample count may not be the same when multi-sampling
// is disabled. The EGL sample count can be 0 but a D3D texture is always 1. Therefore,
// we must only check for a invalid match when the EGL config is non-zero or the texture is
// not one.
if (configuration->samples != 0 || desc.SampleDesc.Count != 1)
{
return egl::Error(EGL_BAD_PARAMETER, "Texture's sample count does not match.");
}
}
// From table egl.restrictions in EGL_ANGLE_d3d_texture_client_buffer.
switch (desc.Format)
{
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
case DXGI_FORMAT_R16G16B16A16_FLOAT:
case DXGI_FORMAT_R32G32B32A32_FLOAT:
break;
default:
return egl::Error(EGL_BAD_PARAMETER, "Unknown client buffer texture format: %u.",
desc.Format);
}
if (fboFormat)
{
const angle::Format &angleFormat = d3d11_angle::GetFormat(desc.Format);
*fboFormat = angleFormat.fboImplementationInternalFormat;
}
return egl::Error(EGL_SUCCESS);
}
egl::Error Renderer11::validateShareHandle(const egl::Config *config,
HANDLE shareHandle,
const egl::AttributeMap &attribs) const
{
if (shareHandle == nullptr)
{
return egl::Error(EGL_BAD_PARAMETER, "NULL share handle.");
}
ID3D11Resource *tempResource11 = nullptr;
HRESULT result = mDevice->OpenSharedResource(shareHandle, __uuidof(ID3D11Resource),
(void **)&tempResource11);
if (FAILED(result))
{
return egl::Error(EGL_BAD_PARAMETER, "Failed to open share handle, result: 0x%X.", result);
}
ID3D11Texture2D *texture2D = d3d11::DynamicCastComObject<ID3D11Texture2D>(tempResource11);
SafeRelease(tempResource11);
if (texture2D == nullptr)
{
return egl::Error(EGL_BAD_PARAMETER,
"Failed to query ID3D11Texture2D object from share handle.");
}
D3D11_TEXTURE2D_DESC desc = {0};
texture2D->GetDesc(&desc);
SafeRelease(texture2D);
EGLint width = attribs.getAsInt(EGL_WIDTH, 0);
EGLint height = attribs.getAsInt(EGL_HEIGHT, 0);
ASSERT(width != 0 && height != 0);
const d3d11::Format &backbufferFormatInfo =
d3d11::Format::Get(config->renderTargetFormat, getRenderer11DeviceCaps());
if (desc.Width != static_cast<UINT>(width) || desc.Height != static_cast<UINT>(height) ||
desc.Format != backbufferFormatInfo.texFormat || desc.MipLevels != 1 || desc.ArraySize != 1)
{
return egl::Error(EGL_BAD_PARAMETER, "Invalid texture parameters in share handle texture.");
}
return egl::Error(EGL_SUCCESS);
}
SwapChainD3D *Renderer11::createSwapChain(NativeWindowD3D *nativeWindow,
HANDLE shareHandle,
IUnknown *d3dTexture,
GLenum backBufferFormat,
GLenum depthBufferFormat,
EGLint orientation,
EGLint samples)
{
return new SwapChain11(this, GetAs<NativeWindow11>(nativeWindow), shareHandle, d3dTexture,
backBufferFormat, depthBufferFormat, orientation, samples);
}
void *Renderer11::getD3DDevice()
{
return reinterpret_cast<void *>(mDevice);
}
gl::Error Renderer11::generateSwizzle(gl::Texture *texture)
{
if (texture)
{
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
ASSERT(textureD3D);
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(&texStorage));
if (texStorage)
{
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
const gl::TextureState &textureState = texture->getTextureState();
ANGLE_TRY(storage11->generateSwizzles(textureState.getSwizzleState()));
}
}
return gl::NoError();
}
gl::Error Renderer11::generateSwizzles(const gl::ContextState &data, gl::SamplerType type)
{
ProgramD3D *programD3D = GetImplAs<ProgramD3D>(data.getState().getProgram());
unsigned int samplerRange = programD3D->getUsedSamplerRange(type);
for (unsigned int i = 0; i < samplerRange; i++)
{
GLenum textureType = programD3D->getSamplerTextureType(type, i);
GLint textureUnit = programD3D->getSamplerMapping(type, i, data.getCaps());
if (textureUnit != -1)
{
gl::Texture *texture = data.getState().getSamplerTexture(textureUnit, textureType);
ASSERT(texture);
if (texture->getTextureState().swizzleRequired())
{
ANGLE_TRY(generateSwizzle(texture));
}
}
}
return gl::NoError();
}
gl::Error Renderer11::generateSwizzles(const gl::ContextState &data)
{
ANGLE_TRY(generateSwizzles(data, gl::SAMPLER_VERTEX));
ANGLE_TRY(generateSwizzles(data, gl::SAMPLER_PIXEL));
return gl::NoError();
}
gl::Error Renderer11::setSamplerState(gl::SamplerType type,
int index,
gl::Texture *texture,
const gl::SamplerState &samplerState)
{
#if !defined(NDEBUG)
// Make sure to add the level offset for our tiny compressed texture workaround
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
TextureStorage *storage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(&storage));
// Storage should exist, texture should be complete
ASSERT(storage);
#endif // !defined(NDEBUG)
// Sampler metadata that's passed to shaders in uniforms is stored separately from rest of the
// sampler state since having it in contiguous memory makes it possible to memcpy to a constant
// buffer, and it doesn't affect the state set by PSSetSamplers/VSSetSamplers.
SamplerMetadataD3D11 *metadata = nullptr;
if (type == gl::SAMPLER_PIXEL)
{
ASSERT(static_cast<unsigned int>(index) < getNativeCaps().maxTextureImageUnits);
if (mForceSetPixelSamplerStates[index] ||
memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
{
ID3D11SamplerState *dxSamplerState = nullptr;
ANGLE_TRY(mStateCache.getSamplerState(samplerState, &dxSamplerState));
ASSERT(dxSamplerState != nullptr);
mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
mCurPixelSamplerStates[index] = samplerState;
}
mForceSetPixelSamplerStates[index] = false;
metadata = &mSamplerMetadataPS;
}
else if (type == gl::SAMPLER_VERTEX)
{
ASSERT(static_cast<unsigned int>(index) < getNativeCaps().maxVertexTextureImageUnits);
if (mForceSetVertexSamplerStates[index] ||
memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
{
ID3D11SamplerState *dxSamplerState = nullptr;
ANGLE_TRY(mStateCache.getSamplerState(samplerState, &dxSamplerState));
ASSERT(dxSamplerState != nullptr);
mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
mCurVertexSamplerStates[index] = samplerState;
}
mForceSetVertexSamplerStates[index] = false;
metadata = &mSamplerMetadataVS;
}
else if (type == gl::SAMPLER_COMPUTE)
{
ASSERT(static_cast<unsigned int>(index) < getNativeCaps().maxComputeTextureImageUnits);
if (mForceSetComputeSamplerStates[index] ||
memcmp(&samplerState, &mCurComputeSamplerStates[index], sizeof(gl::SamplerState)) != 0)
{
ID3D11SamplerState *dxSamplerState = nullptr;
ANGLE_TRY(mStateCache.getSamplerState(samplerState, &dxSamplerState));
ASSERT(dxSamplerState != nullptr);
mDeviceContext->CSSetSamplers(index, 1, &dxSamplerState);
mCurComputeSamplerStates[index] = samplerState;
}
mForceSetComputeSamplerStates[index] = false;
metadata = &mSamplerMetadataCS;
}
else
UNREACHABLE();
ASSERT(metadata != nullptr);
metadata->update(index, *texture);
return gl::NoError();
}
gl::Error Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
{
ID3D11ShaderResourceView *textureSRV = nullptr;
if (texture)
{
TextureD3D *textureImpl = GetImplAs<TextureD3D>(texture);
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureImpl->getNativeTexture(&texStorage));
// Texture should be complete and have a storage
ASSERT(texStorage);
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
ANGLE_TRY(storage11->getSRV(texture->getTextureState(), &textureSRV));
// If we get NULL back from getSRV here, something went wrong in the texture class and we're
// unexpectedly missing the shader resource view
ASSERT(textureSRV != nullptr);
textureImpl->resetDirty();
}
ASSERT((type == gl::SAMPLER_PIXEL &&
static_cast<unsigned int>(index) < getNativeCaps().maxTextureImageUnits) ||
(type == gl::SAMPLER_VERTEX &&
static_cast<unsigned int>(index) < getNativeCaps().maxVertexTextureImageUnits));
mStateManager.setShaderResource(type, index, textureSRV);
return gl::NoError();
}
gl::Error Renderer11::setUniformBuffers(const gl::ContextState &data,
const std::vector<GLint> &vertexUniformBuffers,
const std::vector<GLint> &fragmentUniformBuffers)
{
for (size_t uniformBufferIndex = 0; uniformBufferIndex < vertexUniformBuffers.size();
uniformBufferIndex++)
{
GLint binding = vertexUniformBuffers[uniformBufferIndex];
if (binding == -1)
{
continue;
}
const OffsetBindingPointer<gl::Buffer> &uniformBuffer =
data.getState().getIndexedUniformBuffer(binding);
GLintptr uniformBufferOffset = uniformBuffer.getOffset();
GLsizeiptr uniformBufferSize = uniformBuffer.getSize();
if (uniformBuffer.get() == nullptr)
{
continue;
}
Buffer11 *bufferStorage = GetImplAs<Buffer11>(uniformBuffer.get());
ID3D11Buffer *constantBuffer = nullptr;
UINT firstConstant = 0;
UINT numConstants = 0;
ANGLE_TRY(bufferStorage->getConstantBufferRange(uniformBufferOffset, uniformBufferSize,
&constantBuffer, &firstConstant,
&numConstants));
if (!constantBuffer)
{
return gl::Error(GL_OUT_OF_MEMORY, "Error retrieving constant buffer");
}
if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial() ||
mCurrentConstantBufferVSOffset[uniformBufferIndex] != uniformBufferOffset ||
mCurrentConstantBufferVSSize[uniformBufferIndex] != uniformBufferSize)
{
if (firstConstant != 0 && uniformBufferSize != 0)
{
ASSERT(numConstants != 0);
mDeviceContext1->VSSetConstantBuffers1(
getReservedVertexUniformBuffers() +
static_cast<unsigned int>(uniformBufferIndex),
1, &constantBuffer, &firstConstant, &numConstants);
}
else
{
mDeviceContext->VSSetConstantBuffers(
getReservedVertexUniformBuffers() +
static_cast<unsigned int>(uniformBufferIndex),
1, &constantBuffer);
}
mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
mCurrentConstantBufferVSOffset[uniformBufferIndex] = uniformBufferOffset;
mCurrentConstantBufferVSSize[uniformBufferIndex] = uniformBufferSize;
}
}
for (size_t uniformBufferIndex = 0; uniformBufferIndex < fragmentUniformBuffers.size();
uniformBufferIndex++)
{
GLint binding = fragmentUniformBuffers[uniformBufferIndex];
if (binding == -1)
{
continue;
}
const OffsetBindingPointer<gl::Buffer> &uniformBuffer =
data.getState().getIndexedUniformBuffer(binding);
GLintptr uniformBufferOffset = uniformBuffer.getOffset();
GLsizeiptr uniformBufferSize = uniformBuffer.getSize();
if (uniformBuffer.get() == nullptr)
{
continue;
}
Buffer11 *bufferStorage = GetImplAs<Buffer11>(uniformBuffer.get());
ID3D11Buffer *constantBuffer = nullptr;
UINT firstConstant = 0;
UINT numConstants = 0;
ANGLE_TRY(bufferStorage->getConstantBufferRange(uniformBufferOffset, uniformBufferSize,
&constantBuffer, &firstConstant,
&numConstants));
if (!constantBuffer)
{
return gl::Error(GL_OUT_OF_MEMORY, "Error retrieving constant buffer");
}
if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial() ||
mCurrentConstantBufferPSOffset[uniformBufferIndex] != uniformBufferOffset ||
mCurrentConstantBufferPSSize[uniformBufferIndex] != uniformBufferSize)
{
if (firstConstant != 0 && uniformBufferSize != 0)
{
mDeviceContext1->PSSetConstantBuffers1(
getReservedFragmentUniformBuffers() +
static_cast<unsigned int>(uniformBufferIndex),
1, &constantBuffer, &firstConstant, &numConstants);
}
else
{
mDeviceContext->PSSetConstantBuffers(
getReservedFragmentUniformBuffers() +
static_cast<unsigned int>(uniformBufferIndex),
1, &constantBuffer);
}
mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
mCurrentConstantBufferPSOffset[uniformBufferIndex] = uniformBufferOffset;
mCurrentConstantBufferPSSize[uniformBufferIndex] = uniformBufferSize;
}
}
return gl::NoError();
}
gl::Error Renderer11::updateState(ContextImpl *contextImpl, GLenum drawMode)
{
const auto &data = contextImpl->getContextState();
const auto &glState = data.getState();
// Applies the render target surface, depth stencil surface, viewport rectangle and
// scissor rectangle to the renderer
gl::Framebuffer *framebuffer = glState.getDrawFramebuffer();
ASSERT(framebuffer && !framebuffer->hasAnyDirtyBit() && framebuffer->cachedComplete());
ANGLE_TRY(mStateManager.syncFramebuffer(contextImpl, framebuffer));
// Set the present path state
auto firstColorAttachment = framebuffer->getFirstColorbuffer();
const bool presentPathFastActive = UsePresentPathFast(this, firstColorAttachment);
mStateManager.updatePresentPath(presentPathFastActive, firstColorAttachment);
// Setting viewport state
mStateManager.setViewport(&data.getCaps(), glState.getViewport(), glState.getNearPlane(),
glState.getFarPlane());
// Setting scissor state
mStateManager.setScissorRectangle(glState.getScissor(), glState.isScissorTestEnabled());
// Applying rasterizer state to D3D11 device
// Since framebuffer->getSamples will return the original samples which may be different with
// the sample counts that we set in render target view, here we use renderTarget->getSamples to
// get the actual samples.
GLsizei samples = 0;
if (firstColorAttachment)
{
ASSERT(firstColorAttachment->isAttached());
RenderTarget11 *renderTarget = nullptr;
ANGLE_TRY(firstColorAttachment->getRenderTarget(&renderTarget));
samples = renderTarget->getSamples();
}
gl::RasterizerState rasterizer = glState.getRasterizerState();
rasterizer.pointDrawMode = (drawMode == GL_POINTS);
rasterizer.multiSample = (samples != 0);
ANGLE_TRY(mStateManager.setRasterizerState(rasterizer));
// Setting blend state
unsigned int mask = GetBlendSampleMask(data, samples);
ANGLE_TRY(mStateManager.setBlendState(framebuffer, glState.getBlendState(),
glState.getBlendColor(), mask));
// Setting depth stencil state
ANGLE_TRY(mStateManager.setDepthStencilState(glState));
return gl::NoError();
}
bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count, bool usesPointSize)
{
D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
GLsizei minCount = 0;
switch (mode)
{
case GL_POINTS:
primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
minCount = 1;
break;
case GL_LINES:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST;
minCount = 2;
break;
case GL_LINE_LOOP:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP;
minCount = 2;
break;
case GL_LINE_STRIP:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP;
minCount = 2;
break;
case GL_TRIANGLES:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
minCount = 3;
break;
case GL_TRIANGLE_STRIP:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
minCount = 3;
break;
// emulate fans via rewriting index buffer
case GL_TRIANGLE_FAN:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
minCount = 3;
break;
default:
UNREACHABLE();
return false;
}
// If instanced pointsprite emulation is being used and If gl_PointSize is used in the shader,
// GL_POINTS mode is expected to render pointsprites.
// Instanced PointSprite emulation requires that the topology to be
// D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.
if (mode == GL_POINTS && usesPointSize && getWorkarounds().useInstancedPointSpriteEmulation)
{
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}
if (primitiveTopology != mCurrentPrimitiveTopology)
{
mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
mCurrentPrimitiveTopology = primitiveTopology;
}
return count >= minCount;
}
gl::Error Renderer11::applyVertexBuffer(const gl::State &state,
GLenum mode,
GLint first,
GLsizei count,
GLsizei instances,
TranslatedIndexData *indexInfo)
{
const auto &vertexArray = state.getVertexArray();
auto *vertexArray11 = GetImplAs<VertexArray11>(vertexArray);
ANGLE_TRY(vertexArray11->updateDirtyAndDynamicAttribs(mVertexDataManager, state, first, count,
instances));
ANGLE_TRY(mStateManager.updateCurrentValueAttribs(state, mVertexDataManager));
// If index information is passed, mark it with the current changed status.
if (indexInfo)
{
indexInfo->srcIndexData.srcIndicesChanged = mAppliedIBChanged;
}
GLsizei numIndicesPerInstance = 0;
if (instances > 0)
{
numIndicesPerInstance = count;
}
const auto &vertexArrayAttribs = vertexArray11->getTranslatedAttribs();
const auto &currentValueAttribs = mStateManager.getCurrentValueAttribs();
ANGLE_TRY(mInputLayoutCache.applyVertexBuffers(state, vertexArrayAttribs, currentValueAttribs,
mode, first, indexInfo, numIndicesPerInstance));
// InputLayoutCache::applyVertexBuffers calls through to the Bufer11 to get the native vertex
// buffer (ID3D11Buffer *). Because we allocate these buffers lazily, this will trigger
// allocation. This in turn will signal that the buffer is dirty. Since we just resolved the
// dirty-ness in VertexArray11::updateDirtyAndDynamicAttribs, this can make us do a needless
// update on the second draw call.
// Hence we clear the flags here, after we've applied vertex data, since we know everything
// is clean. This is a bit of a hack.
vertexArray11->clearDirtyAndPromoteDynamicAttribs(state, count);
return gl::NoError();
}
gl::Error Renderer11::applyIndexBuffer(const gl::ContextState &data,
const void *indices,
GLsizei count,
GLenum mode,
GLenum type,
TranslatedIndexData *indexInfo)
{
const auto &glState = data.getState();
gl::VertexArray *vao = glState.getVertexArray();
gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
ANGLE_TRY(mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices,
indexInfo, glState.isPrimitiveRestartEnabled()));
ID3D11Buffer *buffer = nullptr;
DXGI_FORMAT bufferFormat =
(indexInfo->indexType == GL_UNSIGNED_INT) ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
if (indexInfo->storage)
{
Buffer11 *storage = GetAs<Buffer11>(indexInfo->storage);
ANGLE_TRY_RESULT(storage->getBuffer(BUFFER_USAGE_INDEX), buffer);
}
else
{
IndexBuffer11 *indexBuffer = GetAs<IndexBuffer11>(indexInfo->indexBuffer);
buffer = indexBuffer->getBuffer();
}
mAppliedIBChanged = false;
if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat ||
indexInfo->startOffset != mAppliedIBOffset)
{
mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
mAppliedIB = buffer;
mAppliedIBFormat = bufferFormat;
mAppliedIBOffset = indexInfo->startOffset;
mAppliedIBChanged = true;
}
return gl::NoError();
}
gl::Error Renderer11::applyTransformFeedbackBuffers(const gl::ContextState &data)
{
const auto &state = data.getState();
// If transform feedback is not active, unbind all buffers
if (!state.isTransformFeedbackActiveUnpaused())
{
if (mAppliedTFObject != 0)
{
mDeviceContext->SOSetTargets(0, nullptr, nullptr);
mAppliedTFObject = 0;
}
return gl::NoError();
}
gl::TransformFeedback *transformFeedback = state.getCurrentTransformFeedback();
TransformFeedback11 *transformFeedback11 = GetImplAs<TransformFeedback11>(transformFeedback);
uintptr_t transformFeedbackId = reinterpret_cast<uintptr_t>(transformFeedback11);
if (mAppliedTFObject == transformFeedbackId && !transformFeedback11->isDirty())
{
return gl::NoError();
}
const std::vector<ID3D11Buffer *> *soBuffers = nullptr;
ANGLE_TRY_RESULT(transformFeedback11->getSOBuffers(), soBuffers);
const std::vector<UINT> &soOffsets = transformFeedback11->getSOBufferOffsets();
mDeviceContext->SOSetTargets(transformFeedback11->getNumSOBuffers(), soBuffers->data(),
soOffsets.data());
mAppliedTFObject = transformFeedbackId;
transformFeedback11->onApply();
return gl::NoError();
}
gl::Error Renderer11::drawArraysImpl(const gl::ContextState &data,
GLenum mode,
GLint startVertex,
GLsizei count,
GLsizei instances)
{
const auto &glState = data.getState();
ProgramD3D *programD3D = GetImplAs<ProgramD3D>(glState.getProgram());
if (programD3D->usesGeometryShader(mode) && glState.isTransformFeedbackActiveUnpaused())
{
// Since we use a geometry if-and-only-if we rewrite vertex streams, transform feedback
// won't get the correct output. To work around this, draw with *only* the stream out
// first (no pixel shader) to feed the stream out buffers and then draw again with the
// geometry shader + pixel shader to rasterize the primitives.
mDeviceContext->PSSetShader(nullptr, nullptr, 0);
if (instances > 0)
{
mDeviceContext->DrawInstanced(count, instances, 0, 0);
}
else
{
mDeviceContext->Draw(count, 0);
}
rx::ShaderExecutableD3D *pixelExe = nullptr;
ANGLE_TRY(
programD3D->getPixelExecutableForFramebuffer(glState.getDrawFramebuffer(), &pixelExe));
// Skip the draw call if rasterizer discard is enabled (or no fragment shader).
if (!pixelExe || glState.getRasterizerState().rasterizerDiscard)
{
return gl::NoError();
}
ID3D11PixelShader *pixelShader = GetAs<ShaderExecutable11>(pixelExe)->getPixelShader();
ASSERT(reinterpret_cast<uintptr_t>(pixelShader) == mAppliedPixelShader);
mDeviceContext->PSSetShader(pixelShader, nullptr, 0);
// Retrieve the geometry shader.
rx::ShaderExecutableD3D *geometryExe = nullptr;
ANGLE_TRY(
programD3D->getGeometryExecutableForPrimitiveType(data, mode, &geometryExe, nullptr));
ID3D11GeometryShader *geometryShader =
(geometryExe ? GetAs<ShaderExecutable11>(geometryExe)->getGeometryShader() : nullptr);
mAppliedGeometryShader = reinterpret_cast<uintptr_t>(geometryShader);
ASSERT(geometryShader);
mDeviceContext->GSSetShader(geometryShader, nullptr, 0);
if (instances > 0)
{
mDeviceContext->DrawInstanced(count, instances, 0, 0);
}
else
{
mDeviceContext->Draw(count, 0);
}
return gl::NoError();
}
if (mode == GL_LINE_LOOP)
{
return drawLineLoop(data, count, GL_NONE, nullptr, 0, instances);
}
if (mode == GL_TRIANGLE_FAN)
{
return drawTriangleFan(data, count, GL_NONE, nullptr, 0, instances);
}
bool useInstancedPointSpriteEmulation =
programD3D->usesPointSize() && getWorkarounds().useInstancedPointSpriteEmulation;
if (instances > 0)
{
if (mode == GL_POINTS && useInstancedPointSpriteEmulation)
{
// If pointsprite emulation is used with glDrawArraysInstanced then we need to take a
// less efficent code path.
// Instanced rendering of emulated pointsprites requires a loop to draw each batch of
// points. An offset into the instanced data buffer is calculated and applied on each
// iteration to ensure all instances are rendered correctly.
// Each instance being rendered requires the inputlayout cache to reapply buffers and
// offsets.
for (GLsizei i = 0; i < instances; i++)
{
ANGLE_TRY(
mInputLayoutCache.updateVertexOffsetsForPointSpritesEmulation(startVertex, i));
mDeviceContext->DrawIndexedInstanced(6, count, 0, 0, 0);
}
}
else
{
mDeviceContext->DrawInstanced(count, instances, 0, 0);
}
return gl::NoError();
}
// If the shader is writing to gl_PointSize, then pointsprites are being rendered.
// Emulating instanced point sprites for FL9_3 requires the topology to be
// D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST and DrawIndexedInstanced is called instead.
if (mode == GL_POINTS && useInstancedPointSpriteEmulation)
{
mDeviceContext->DrawIndexedInstanced(6, count, 0, 0, 0);
}
else
{
mDeviceContext->Draw(count, 0);
}
return gl::NoError();
}
gl::Error Renderer11::drawElementsImpl(const gl::ContextState &data,
const TranslatedIndexData &indexInfo,
GLenum mode,
GLsizei count,
GLenum type,
const void *indices,
GLsizei instances)
{
int startVertex = static_cast<int>(indexInfo.indexRange.start);
int baseVertex = -startVertex;
if (mode == GL_LINE_LOOP)
{
return drawLineLoop(data, count, type, indices, baseVertex, instances);
}
if (mode == GL_TRIANGLE_FAN)
{
return drawTriangleFan(data, count, type, indices, baseVertex, instances);
}
const ProgramD3D *programD3D = GetImplAs<ProgramD3D>(data.getState().getProgram());
if (instances > 0)
{
if (mode == GL_POINTS && programD3D->usesInstancedPointSpriteEmulation())
{
// If pointsprite emulation is used with glDrawElementsInstanced then we need to take a
// less efficent code path.
// Instanced rendering of emulated pointsprites requires a loop to draw each batch of
// points. An offset into the instanced data buffer is calculated and applied on each
// iteration to ensure all instances are rendered correctly.
GLsizei elementsToRender = static_cast<GLsizei>(indexInfo.indexRange.vertexCount());
// Each instance being rendered requires the inputlayout cache to reapply buffers and
// offsets.
for (GLsizei i = 0; i < instances; i++)
{
ANGLE_TRY(
mInputLayoutCache.updateVertexOffsetsForPointSpritesEmulation(startVertex, i));
mDeviceContext->DrawIndexedInstanced(6, elementsToRender, 0, 0, 0);
}
}
else
{
mDeviceContext->DrawIndexedInstanced(count, instances, 0, baseVertex, 0);
}
return gl::NoError();
}
// If the shader is writing to gl_PointSize, then pointsprites are being rendered.
// Emulating instanced point sprites for FL9_3 requires the topology to be
// D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST and DrawIndexedInstanced is called instead.
if (mode == GL_POINTS && programD3D->usesInstancedPointSpriteEmulation())
{
// The count parameter passed to drawElements represents the total number of instances
// to be rendered. Each instance is referenced by the bound index buffer from the
// the caller.
//
// Indexed pointsprite emulation replicates data for duplicate entries found
// in the index buffer.
// This is not an efficent rendering mechanism and is only used on downlevel renderers
// that do not support geometry shaders.
mDeviceContext->DrawIndexedInstanced(6, count, 0, 0, 0);
}
else
{
mDeviceContext->DrawIndexed(count, 0, baseVertex);
}
return gl::NoError();
}
bool Renderer11::supportsFastIndirectDraw(const gl::State &state, GLenum mode, GLenum type)
{
const auto &vertexArray = state.getVertexArray();
auto *vertexArray11 = GetImplAs<VertexArray11>(vertexArray);
// Indirect drawing doesn't support dynamic attribute storage since it needs the first and count
// to translate when applyVertexBuffer. GL_LINE_LOOP and GL_TRIANGLE_FAN are not supported
// either since we need to simulate them in D3D.
if (vertexArray11->hasDynamicAttrib(state) || mode == GL_LINE_LOOP || mode == GL_TRIANGLE_FAN)
{
return false;
}
if (type != GL_NONE)
{
gl::Buffer *elementArrayBuffer = vertexArray->getElementArrayBuffer().get();
ASSERT(elementArrayBuffer);
// Only non-streaming index data can be directly used to do indirect draw since they don't
// need the indices and count informations. Here we don't check whether it really has
// primitive restart index in it since it also needs to know the index range and count.
// So, for all other situations, we fall back to normal draw instead of indirect draw.
bool primitiveRestartWorkaround = mIndexDataManager->usePrimitiveRestartWorkaround(
state.isPrimitiveRestartEnabled(), type);
return !mIndexDataManager->isStreamingIndexData(primitiveRestartWorkaround, type,
elementArrayBuffer);
}
return true;
}
gl::Error Renderer11::drawArraysIndirectImpl(const gl::ContextState &data,
GLenum mode,
const void *indirect)
{
if (skipDraw(data, mode))
{
return gl::NoError();
}
const auto &glState = data.getState();
gl::Buffer *drawIndirectBuffer = glState.getDrawIndirectBuffer();
ASSERT(drawIndirectBuffer);
Buffer11 *storage = GetImplAs<Buffer11>(drawIndirectBuffer);
uintptr_t offset = reinterpret_cast<uintptr_t>(indirect);
if (supportsFastIndirectDraw(glState, mode, GL_NONE))
{
applyVertexBuffer(glState, mode, 0, 0, 0, nullptr);
ID3D11Buffer *buffer = nullptr;
ANGLE_TRY_RESULT(storage->getBuffer(BUFFER_USAGE_INDIRECT), buffer);
mDeviceContext->DrawInstancedIndirect(buffer, static_cast<unsigned int>(offset));
return gl::NoError();
}
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(&bufferData));
ASSERT(bufferData);
const gl::DrawArraysIndirectCommand *args =
reinterpret_cast<const gl::DrawArraysIndirectCommand *>(bufferData + offset);
GLuint count = args->count;
GLuint instances = args->instanceCount;
GLuint first = args->first;
ANGLE_TRY(applyVertexBuffer(glState, mode, first, count, instances, nullptr));
if (mode == GL_LINE_LOOP)
{
return drawLineLoop(data, count, GL_NONE, nullptr, 0, instances);
}
if (mode == GL_TRIANGLE_FAN)
{
return drawTriangleFan(data, count, GL_NONE, nullptr, 0, instances);
}
mDeviceContext->DrawInstanced(count, instances, 0, 0);
return gl::NoError();
}
gl::Error Renderer11::drawElementsIndirectImpl(const gl::ContextState &data,
GLenum mode,
GLenum type,
const void *indirect)
{
if (skipDraw(data, mode))
{
return gl::NoError();
}
const auto &glState = data.getState();
gl::Buffer *drawIndirectBuffer = glState.getDrawIndirectBuffer();
ASSERT(drawIndirectBuffer);
Buffer11 *storage = GetImplAs<Buffer11>(drawIndirectBuffer);
uintptr_t offset = reinterpret_cast<uintptr_t>(indirect);
TranslatedIndexData indexInfo;
if (supportsFastIndirectDraw(glState, mode, type))
{
ANGLE_TRY(applyIndexBuffer(data, nullptr, 0, mode, type, &indexInfo));
ANGLE_TRY(applyVertexBuffer(glState, mode, 0, 0, 0, &indexInfo));
ID3D11Buffer *buffer = nullptr;
ANGLE_TRY_RESULT(storage->getBuffer(BUFFER_USAGE_INDIRECT), buffer);
mDeviceContext->DrawIndexedInstancedIndirect(buffer, static_cast<unsigned int>(offset));
return gl::NoError();
}
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(&bufferData));
ASSERT(bufferData);
const gl::DrawElementsIndirectCommand *cmd =
reinterpret_cast<const gl::DrawElementsIndirectCommand *>(bufferData + offset);
GLuint count = cmd->count;
GLuint instances = cmd->primCount;
GLuint firstIndex = cmd->firstIndex;
GLint baseVertex = cmd->baseVertex;
const gl::Type &typeInfo = gl::GetTypeInfo(type);
uint8_t *indices = static_cast<uint8_t *>(0) + firstIndex * typeInfo.bytes;
gl::Buffer *elementArrayBuffer = glState.getVertexArray()->getElementArrayBuffer().get();
ASSERT(elementArrayBuffer);
gl::IndexRange indexRange;
ANGLE_TRY(elementArrayBuffer->getIndexRange(type, reinterpret_cast<size_t>(indices), count,
glState.isPrimitiveRestartEnabled(), &indexRange));
indexInfo.indexRange = indexRange;
ANGLE_TRY(applyIndexBuffer(data, indices, count, mode, type, &indexInfo));
size_t vertexCount = indexRange.vertexCount();
ANGLE_TRY(applyVertexBuffer(glState, mode, static_cast<GLsizei>(indexRange.start) + baseVertex,
static_cast<GLsizei>(vertexCount), instances, &indexInfo));
int baseVertexLocation = -static_cast<int>(indexRange.start);
if (mode == GL_LINE_LOOP)
{
return drawLineLoop(data, count, type, indices, baseVertexLocation, instances);
}
if (mode == GL_TRIANGLE_FAN)
{
return drawTriangleFan(data, count, type, indices, baseVertexLocation, instances);
}
mDeviceContext->DrawIndexedInstanced(count, instances, 0, baseVertexLocation, 0);
return gl::NoError();
}
gl::Error Renderer11::drawLineLoop(const gl::ContextState &data,
GLsizei count,
GLenum type,
const void *indexPointer,
int baseVertex,
int instances)
{
const auto &glState = data.getState();
gl::VertexArray *vao = glState.getVertexArray();
gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
const void *indices = indexPointer;
// Get the raw indices for an indexed draw
if (type != GL_NONE && elementArrayBuffer)
{
BufferD3D *storage = GetImplAs<BufferD3D>(elementArrayBuffer);
intptr_t offset = reinterpret_cast<intptr_t>(indices);
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(&bufferData));
indices = bufferData + offset;
}
if (!mLineLoopIB)
{
mLineLoopIB = new StreamingIndexBufferInterface(this);
gl::Error error =
mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
if (error.isError())
{
SafeDelete(mLineLoopIB);
return error;
}
}
// Checked by Renderer11::applyPrimitiveType
ASSERT(count >= 0);
if (static_cast<unsigned int>(count) + 1 >
(std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create a 32-bit looping index buffer for GL_LINE_LOOP, too "
"many indices required.");
}
GetLineLoopIndices(indices, type, static_cast<GLuint>(count),
glState.isPrimitiveRestartEnabled(), &mScratchIndexDataBuffer);
unsigned int spaceNeeded =
static_cast<unsigned int>(sizeof(GLuint) * mScratchIndexDataBuffer.size());
ANGLE_TRY(mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT));
void *mappedMemory = nullptr;
unsigned int offset;
ANGLE_TRY(mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset));
// Copy over the converted index data.
memcpy(mappedMemory, &mScratchIndexDataBuffer[0],
sizeof(GLuint) * mScratchIndexDataBuffer.size());
ANGLE_TRY(mLineLoopIB->unmapBuffer());
IndexBuffer11 *indexBuffer = GetAs<IndexBuffer11>(mLineLoopIB->getIndexBuffer());
ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat ||
mAppliedIBOffset != offset)
{
mDeviceContext->IASetIndexBuffer(d3dIndexBuffer, indexFormat, offset);
mAppliedIB = d3dIndexBuffer;
mAppliedIBFormat = indexFormat;
mAppliedIBOffset = offset;
}
UINT indexCount = static_cast<UINT>(mScratchIndexDataBuffer.size());
if (instances > 0)
{
mDeviceContext->DrawIndexedInstanced(indexCount, instances, 0, baseVertex, 0);
}
else
{
mDeviceContext->DrawIndexed(indexCount, 0, baseVertex);
}
return gl::NoError();
}
gl::Error Renderer11::drawTriangleFan(const gl::ContextState &data,
GLsizei count,
GLenum type,
const void *indices,
int baseVertex,
int instances)
{
gl::VertexArray *vao = data.getState().getVertexArray();
gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
const void *indexPointer = indices;
// Get the raw indices for an indexed draw
if (type != GL_NONE && elementArrayBuffer)
{
BufferD3D *storage = GetImplAs<BufferD3D>(elementArrayBuffer);
intptr_t offset = reinterpret_cast<intptr_t>(indices);
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(&bufferData));
indexPointer = bufferData + offset;
}
if (!mTriangleFanIB)
{
mTriangleFanIB = new StreamingIndexBufferInterface(this);
gl::Error error =
mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
if (error.isError())
{
SafeDelete(mTriangleFanIB);
return error;
}
}
// Checked by Renderer11::applyPrimitiveType
ASSERT(count >= 3);
const GLuint numTris = count - 2;
if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create a scratch index buffer for GL_TRIANGLE_FAN, too many "
"indices required.");
}
GetTriFanIndices(indexPointer, type, count, data.getState().isPrimitiveRestartEnabled(),
&mScratchIndexDataBuffer);
const unsigned int spaceNeeded =
static_cast<unsigned int>(mScratchIndexDataBuffer.size() * sizeof(unsigned int));
ANGLE_TRY(mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT));
void *mappedMemory = nullptr;
unsigned int offset;
ANGLE_TRY(mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset));
memcpy(mappedMemory, &mScratchIndexDataBuffer[0], spaceNeeded);
ANGLE_TRY(mTriangleFanIB->unmapBuffer());
IndexBuffer11 *indexBuffer = GetAs<IndexBuffer11>(mTriangleFanIB->getIndexBuffer());
ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat ||
mAppliedIBOffset != offset)
{
mDeviceContext->IASetIndexBuffer(d3dIndexBuffer, indexFormat, offset);
mAppliedIB = d3dIndexBuffer;
mAppliedIBFormat = indexFormat;
mAppliedIBOffset = offset;
}
UINT indexCount = static_cast<UINT>(mScratchIndexDataBuffer.size());
if (instances > 0)
{
mDeviceContext->DrawIndexedInstanced(indexCount, instances, 0, baseVertex, 0);
}
else
{
mDeviceContext->DrawIndexed(indexCount, 0, baseVertex);
}
return gl::NoError();
}
gl::Error Renderer11::applyShaders(const gl::ContextState &data, GLenum drawMode)
{
// This method is called single-threaded.
ANGLE_TRY(ensureHLSLCompilerInitialized());
const auto &glState = data.getState();
ProgramD3D *programD3D = GetImplAs<ProgramD3D>(glState.getProgram());
programD3D->updateCachedInputLayout(glState);
const auto &inputLayout = programD3D->getCachedInputLayout();
ShaderExecutableD3D *vertexExe = nullptr;
ANGLE_TRY(programD3D->getVertexExecutableForInputLayout(inputLayout, &vertexExe, nullptr));
const gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
ShaderExecutableD3D *pixelExe = nullptr;
ANGLE_TRY(programD3D->getPixelExecutableForFramebuffer(drawFramebuffer, &pixelExe));
ShaderExecutableD3D *geometryExe = nullptr;
ANGLE_TRY(
programD3D->getGeometryExecutableForPrimitiveType(data, drawMode, &geometryExe, nullptr));
ID3D11VertexShader *vertexShader =
(vertexExe ? GetAs<ShaderExecutable11>(vertexExe)->getVertexShader() : nullptr);
ID3D11PixelShader *pixelShader = nullptr;
// Skip pixel shader if we're doing rasterizer discard.
bool rasterizerDiscard = glState.getRasterizerState().rasterizerDiscard;
if (!rasterizerDiscard)
{
pixelShader = (pixelExe ? GetAs<ShaderExecutable11>(pixelExe)->getPixelShader() : nullptr);
}
ID3D11GeometryShader *geometryShader = nullptr;
bool transformFeedbackActive = glState.isTransformFeedbackActiveUnpaused();
if (transformFeedbackActive)
{
geometryShader =
(vertexExe ? GetAs<ShaderExecutable11>(vertexExe)->getStreamOutShader() : nullptr);
}
else
{
geometryShader =
(geometryExe ? GetAs<ShaderExecutable11>(geometryExe)->getGeometryShader() : nullptr);
}
bool dirtyUniforms = false;
if (reinterpret_cast<uintptr_t>(vertexShader) != mAppliedVertexShader)
{
mDeviceContext->VSSetShader(vertexShader, nullptr, 0);
mAppliedVertexShader = reinterpret_cast<uintptr_t>(vertexShader);
dirtyUniforms = true;
}
if (reinterpret_cast<uintptr_t>(geometryShader) != mAppliedGeometryShader)
{
mDeviceContext->GSSetShader(geometryShader, nullptr, 0);
mAppliedGeometryShader = reinterpret_cast<uintptr_t>(geometryShader);
dirtyUniforms = true;
}
if (reinterpret_cast<uintptr_t>(pixelShader) != mAppliedPixelShader)
{
mDeviceContext->PSSetShader(pixelShader, nullptr, 0);
mAppliedPixelShader = reinterpret_cast<uintptr_t>(pixelShader);
dirtyUniforms = true;
}
if (dirtyUniforms)
{
programD3D->dirtyAllUniforms();
}
return programD3D->applyUniforms(drawMode);
}
gl::Error Renderer11::applyUniforms(const ProgramD3D &programD3D,
GLenum drawMode,
const std::vector<D3DUniform *> &uniformArray)
{
unsigned int totalRegisterCountVS = 0;
unsigned int totalRegisterCountPS = 0;
bool vertexUniformsDirty = false;
bool pixelUniformsDirty = false;
for (const D3DUniform *uniform : uniformArray)
{
if (uniform->isReferencedByVertexShader() && !uniform->isSampler())
{
totalRegisterCountVS += uniform->registerCount;
vertexUniformsDirty = (vertexUniformsDirty || uniform->dirty);
}
if (uniform->isReferencedByFragmentShader() && !uniform->isSampler())
{
totalRegisterCountPS += uniform->registerCount;
pixelUniformsDirty = (pixelUniformsDirty || uniform->dirty);
}
}
const UniformStorage11 *vertexUniformStorage =
GetAs<UniformStorage11>(&programD3D.getVertexUniformStorage());
const UniformStorage11 *fragmentUniformStorage =
GetAs<UniformStorage11>(&programD3D.getFragmentUniformStorage());
ASSERT(vertexUniformStorage);
ASSERT(fragmentUniformStorage);
ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
float(*mapVS)[4] = nullptr;
float(*mapPS)[4] = nullptr;
if (totalRegisterCountVS > 0 && vertexUniformsDirty)
{
D3D11_MAPPED_SUBRESOURCE map = {0};
HRESULT result =
mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
ASSERT(SUCCEEDED(result));
mapVS = (float(*)[4])map.pData;
}
if (totalRegisterCountPS > 0 && pixelUniformsDirty)
{
D3D11_MAPPED_SUBRESOURCE map = {0};
HRESULT result =
mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
ASSERT(SUCCEEDED(result));
mapPS = (float(*)[4])map.pData;
}
for (const D3DUniform *uniform : uniformArray)
{
if (uniform->isSampler())
continue;
unsigned int componentCount = (4 - uniform->registerElement);
// we assume that uniforms from structs are arranged in struct order in our uniforms list.
// otherwise we would overwrite previously written regions of memory.
if (uniform->isReferencedByVertexShader() && mapVS)
{
memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data,
uniform->registerCount * sizeof(float) * componentCount);
}
if (uniform->isReferencedByFragmentShader() && mapPS)
{
memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data,
uniform->registerCount * sizeof(float) * componentCount);
}
}
if (mapVS)
{
mDeviceContext->Unmap(vertexConstantBuffer, 0);
}
if (mapPS)
{
mDeviceContext->Unmap(pixelConstantBuffer, 0);
}
if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
{
mDeviceContext->VSSetConstantBuffers(
d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DEFAULT_UNIFORM_BLOCK, 1, &vertexConstantBuffer);
mCurrentVertexConstantBuffer = vertexConstantBuffer;
}
if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
{
mDeviceContext->PSSetConstantBuffers(
d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DEFAULT_UNIFORM_BLOCK, 1, &pixelConstantBuffer);
mCurrentPixelConstantBuffer = pixelConstantBuffer;
}
if (!mDriverConstantBufferVS)
{
D3D11_BUFFER_DESC constantBufferDescription = {0};
d3d11::InitConstantBufferDesc(
&constantBufferDescription,
sizeof(dx_VertexConstants11) + mSamplerMetadataVS.sizeBytes());
HRESULT result =
mDevice->CreateBuffer(&constantBufferDescription, nullptr, &mDriverConstantBufferVS);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create vertex shader constant buffer, result: 0x%X.",
result);
}
mDeviceContext->VSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&mDriverConstantBufferVS);
}
if (!mDriverConstantBufferPS)
{
D3D11_BUFFER_DESC constantBufferDescription = {0};
d3d11::InitConstantBufferDesc(&constantBufferDescription,
sizeof(dx_PixelConstants11) + mSamplerMetadataPS.sizeBytes());
HRESULT result =
mDevice->CreateBuffer(&constantBufferDescription, nullptr, &mDriverConstantBufferPS);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create pixel shader constant buffer, result: 0x%X.",
result);
}
mDeviceContext->PSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&mDriverConstantBufferPS);
}
// Sampler metadata and driver constants need to coexist in the same constant buffer to conserve
// constant buffer slots. We update both in the constant buffer if needed.
const dx_VertexConstants11 &vertexConstants = mStateManager.getVertexConstants();
size_t samplerMetadataReferencedBytesVS = sizeof(SamplerMetadataD3D11::dx_SamplerMetadata) *
programD3D.getUsedSamplerRange(gl::SAMPLER_VERTEX);
applyDriverConstantsIfNeeded(&mAppliedVertexConstants, vertexConstants, &mSamplerMetadataVS,
samplerMetadataReferencedBytesVS, mDriverConstantBufferVS);
const dx_PixelConstants11 &pixelConstants = mStateManager.getPixelConstants();
size_t samplerMetadataReferencedBytesPS = sizeof(SamplerMetadataD3D11::dx_SamplerMetadata) *
programD3D.getUsedSamplerRange(gl::SAMPLER_PIXEL);
applyDriverConstantsIfNeeded(&mAppliedPixelConstants, pixelConstants, &mSamplerMetadataPS,
samplerMetadataReferencedBytesPS, mDriverConstantBufferPS);
// GSSetConstantBuffers triggers device removal on 9_3, so we should only call it if necessary
if (programD3D.usesGeometryShader(drawMode))
{
// needed for the point sprite geometry shader
if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
{
ASSERT(mDriverConstantBufferPS != nullptr);
mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
}
}
return gl::NoError();
}
// SamplerMetadataD3D11 implementation
Renderer11::SamplerMetadataD3D11::SamplerMetadataD3D11() : mDirty(true)
{
}
Renderer11::SamplerMetadataD3D11::~SamplerMetadataD3D11()
{
}
void Renderer11::SamplerMetadataD3D11::initData(unsigned int samplerCount)
{
mSamplerMetadata.resize(samplerCount);
}
void Renderer11::SamplerMetadataD3D11::update(unsigned int samplerIndex, const gl::Texture &texture)
{
unsigned int baseLevel = texture.getTextureState().getEffectiveBaseLevel();
GLenum sizedFormat =
texture.getFormat(texture.getTarget(), baseLevel).info->sizedInternalFormat;
if (mSamplerMetadata[samplerIndex].baseLevel != static_cast<int>(baseLevel))
{
mSamplerMetadata[samplerIndex].baseLevel = static_cast<int>(baseLevel);
mDirty = true;
}
// Some metadata is needed only for integer textures. We avoid updating the constant buffer
// unnecessarily by changing the data only in case the texture is an integer texture and
// the values have changed.
bool needIntegerTextureMetadata = false;
// internalFormatBits == 0 means a 32-bit texture in the case of integer textures.
int internalFormatBits = 0;
switch (sizedFormat)
{
case GL_RGBA32I:
case GL_RGBA32UI:
case GL_RGB32I:
case GL_RGB32UI:
case GL_RG32I:
case GL_RG32UI:
case GL_R32I:
case GL_R32UI:
needIntegerTextureMetadata = true;
break;
case GL_RGBA16I:
case GL_RGBA16UI:
case GL_RGB16I:
case GL_RGB16UI:
case GL_RG16I:
case GL_RG16UI:
case GL_R16I:
case GL_R16UI:
needIntegerTextureMetadata = true;
internalFormatBits = 16;
break;
case GL_RGBA8I:
case GL_RGBA8UI:
case GL_RGB8I:
case GL_RGB8UI:
case GL_RG8I:
case GL_RG8UI:
case GL_R8I:
case GL_R8UI:
needIntegerTextureMetadata = true;
internalFormatBits = 8;
break;
case GL_RGB10_A2UI:
needIntegerTextureMetadata = true;
internalFormatBits = 10;
break;
default:
break;
}
if (needIntegerTextureMetadata)
{
if (mSamplerMetadata[samplerIndex].internalFormatBits != internalFormatBits)
{
mSamplerMetadata[samplerIndex].internalFormatBits = internalFormatBits;
mDirty = true;
}
// Pack the wrap values into one integer so we can fit all the metadata in one 4-integer
// vector.
GLenum wrapS = texture.getWrapS();
GLenum wrapT = texture.getWrapT();
GLenum wrapR = texture.getWrapR();
int wrapModes = GetWrapBits(wrapS) | (GetWrapBits(wrapT) << 2) | (GetWrapBits(wrapR) << 4);
if (mSamplerMetadata[samplerIndex].wrapModes != wrapModes)
{
mSamplerMetadata[samplerIndex].wrapModes = wrapModes;
mDirty = true;
}
}
}
const Renderer11::SamplerMetadataD3D11::dx_SamplerMetadata *
Renderer11::SamplerMetadataD3D11::getData() const
{
return mSamplerMetadata.data();
}
size_t Renderer11::SamplerMetadataD3D11::sizeBytes() const
{
return sizeof(SamplerMetadataD3D11::dx_SamplerMetadata) * mSamplerMetadata.size();
}
template <class TShaderConstants>
void Renderer11::applyDriverConstantsIfNeeded(TShaderConstants *appliedConstants,
const TShaderConstants &constants,
SamplerMetadataD3D11 *samplerMetadata,
size_t samplerMetadataReferencedBytes,
ID3D11Buffer *driverConstantBuffer)
{
ASSERT(driverConstantBuffer != nullptr);
if (memcmp(appliedConstants, &constants, sizeof(TShaderConstants)) != 0 ||
samplerMetadata->isDirty())
{
memcpy(appliedConstants, &constants, sizeof(TShaderConstants));
D3D11_MAPPED_SUBRESOURCE mapping = {0};
HRESULT result =
mDeviceContext->Map(driverConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapping);
ASSERT(SUCCEEDED(result));
memcpy(mapping.pData, appliedConstants, sizeof(TShaderConstants));
// Previous buffer contents were discarded, so we need to refresh also the area of the
// buffer that isn't used by this program.
memcpy(&reinterpret_cast<uint8_t *>(mapping.pData)[sizeof(TShaderConstants)],
samplerMetadata->getData(), samplerMetadata->sizeBytes());
mDeviceContext->Unmap(driverConstantBuffer, 0);
samplerMetadata->markClean();
}
}
template void Renderer11::applyDriverConstantsIfNeeded<dx_VertexConstants11>(
dx_VertexConstants11 *appliedConstants,
const dx_VertexConstants11 &constants,
SamplerMetadataD3D11 *samplerMetadata,
size_t samplerMetadataReferencedBytes,
ID3D11Buffer *driverConstantBuffer);
template void Renderer11::applyDriverConstantsIfNeeded<dx_PixelConstants11>(
dx_PixelConstants11 *appliedConstants,
const dx_PixelConstants11 &constants,
SamplerMetadataD3D11 *samplerMetadata,
size_t samplerMetadataReferencedBytes,
ID3D11Buffer *driverConstantBuffer);
template void Renderer11::applyDriverConstantsIfNeeded<dx_ComputeConstants11>(
dx_ComputeConstants11 *appliedConstants,
const dx_ComputeConstants11 &constants,
SamplerMetadataD3D11 *samplerMetadata,
size_t samplerMetadataReferencedBytes,
ID3D11Buffer *driverConstantBuffer);
void Renderer11::markAllStateDirty()
{
TRACE_EVENT0("gpu.angle", "Renderer11::markAllStateDirty");
for (size_t vsamplerId = 0; vsamplerId < mForceSetVertexSamplerStates.size(); ++vsamplerId)
{
mForceSetVertexSamplerStates[vsamplerId] = true;
}
for (size_t fsamplerId = 0; fsamplerId < mForceSetPixelSamplerStates.size(); ++fsamplerId)
{
mForceSetPixelSamplerStates[fsamplerId] = true;
}
for (size_t csamplerId = 0; csamplerId < mForceSetComputeSamplerStates.size(); ++csamplerId)
{
mForceSetComputeSamplerStates[csamplerId] = true;
}
mStateManager.invalidateEverything();
mAppliedIB = nullptr;
mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
mAppliedIBOffset = 0;
mAppliedVertexShader = angle::DirtyPointer;
mAppliedGeometryShader = angle::DirtyPointer;
mAppliedPixelShader = angle::DirtyPointer;
mAppliedComputeShader = angle::DirtyPointer;
mAppliedTFObject = angle::DirtyPointer;
memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants11));
memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants11));
mInputLayoutCache.markDirty();
for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
{
mCurrentConstantBufferVS[i] = static_cast<unsigned int>(-1);
mCurrentConstantBufferVSOffset[i] = 0;
mCurrentConstantBufferVSSize[i] = 0;
mCurrentConstantBufferPS[i] = static_cast<unsigned int>(-1);
mCurrentConstantBufferPSOffset[i] = 0;
mCurrentConstantBufferPSSize[i] = 0;
}
mCurrentVertexConstantBuffer = nullptr;
mCurrentPixelConstantBuffer = nullptr;
mCurrentGeometryConstantBuffer = nullptr;
mCurrentComputeConstantBuffer = nullptr;
mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
}
void Renderer11::releaseDeviceResources()
{
mStateManager.deinitialize();
mStateCache.clear();
mInputLayoutCache.clear();
SafeDelete(mVertexDataManager);
SafeDelete(mIndexDataManager);
SafeDelete(mLineLoopIB);
SafeDelete(mTriangleFanIB);
SafeDelete(mBlit);
SafeDelete(mClear);
SafeDelete(mTrim);
SafeDelete(mPixelTransfer);
SafeRelease(mDriverConstantBufferVS);
SafeRelease(mDriverConstantBufferPS);
SafeRelease(mDriverConstantBufferCS);
SafeRelease(mSyncQuery);
}
// set notify to true to broadcast a message to all contexts of the device loss
bool Renderer11::testDeviceLost()
{
bool isLost = false;
if (!mDevice)
{
return true;
}
// GetRemovedReason is used to test if the device is removed
HRESULT result = mDevice->GetDeviceRemovedReason();
isLost = d3d11::isDeviceLostError(result);
if (isLost)
{
ERR() << "The D3D11 device was removed, " << gl::FmtHR(result);
}
return isLost;
}
bool Renderer11::testDeviceResettable()
{
// determine if the device is resettable by creating a dummy device
PFN_D3D11_CREATE_DEVICE D3D11CreateDevice =
(PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
if (D3D11CreateDevice == nullptr)
{
return false;
}
ID3D11Device *dummyDevice;
D3D_FEATURE_LEVEL dummyFeatureLevel;
ID3D11DeviceContext *dummyContext;
ASSERT(mRequestedDriverType != D3D_DRIVER_TYPE_UNKNOWN);
HRESULT result = D3D11CreateDevice(
nullptr, mRequestedDriverType, nullptr,
#if defined(_DEBUG)
D3D11_CREATE_DEVICE_DEBUG,
#else
0,
#endif
mAvailableFeatureLevels.data(), static_cast<unsigned int>(mAvailableFeatureLevels.size()),
D3D11_SDK_VERSION, &dummyDevice, &dummyFeatureLevel, &dummyContext);
if (!mDevice || FAILED(result))
{
return false;
}
SafeRelease(dummyContext);
SafeRelease(dummyDevice);
return true;
}
void Renderer11::release()
{
RendererD3D::cleanup();
mScratchMemoryBuffer.clear();
if (mAnnotator != nullptr)
{
gl::UninitializeDebugAnnotations();
SafeDelete(mAnnotator);
}