Commit 0654d32a by Geoff Lang

Fix texture formats backed by extra channels.

Fix rendering to, initializing and clearing textures that are backed by more channels in D3D11 than the GL format has. Change-Id: I250c349254acf4e6f85d4eff58e2190edb1782ef Reviewed-on: https://chromium-review.googlesource.com/183994Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarNicolas Capens <nicolascapens@chromium.org> Commit-Queue: Nicolas Capens <nicolascapens@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 3f2c0b38
......@@ -1778,7 +1778,7 @@ void Context::applyState(GLenum drawMode)
{
mask = 0xFFFFFFFF;
}
mRenderer->setBlendState(mState.blend, mState.blendColor, mask);
mRenderer->setBlendState(framebufferObject, mState.blend, mState.blendColor, mask);
mRenderer->setDepthStencilState(mState.depthStencil, mState.stencilRef, mState.stencilBackRef,
mState.rasterizer.frontFace == GL_CCW);
......
......@@ -119,7 +119,7 @@ class Renderer
virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture) = 0;
virtual void setRasterizerState(const gl::RasterizerState &rasterState) = 0;
virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor,
unsigned int sampleMask) = 0;
virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
int stencilBackRef, bool frontFaceCCW) = 0;
......
......@@ -106,9 +106,15 @@ void Image11::generateMipmap(Image11 *dest, Image11 *src)
dest->markDirty();
}
static bool FormatRequiresInitialization(DXGI_FORMAT dxgiFormat, GLenum internalFormat)
{
return (dxgiFormat == DXGI_FORMAT_R8G8B8A8_UNORM && gl::GetAlphaSize(internalFormat) == 0) ||
(dxgiFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && gl::GetAlphaSize(internalFormat) == 0);
}
bool Image11::isDirty() const
{
return (mStagingTexture && mDirty);
return ((mStagingTexture || FormatRequiresInitialization(mDXGIFormat, mInternalFormat)) && mDirty);
}
bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
......@@ -371,6 +377,27 @@ unsigned int Image11::getStagingSubresource()
return mStagingSubresource;
}
template <typename T, size_t N>
static void setDefaultData(ID3D11DeviceContext *deviceContext, ID3D11Texture2D *texture, UINT subresource,
GLsizei width, GLsizei height, const T (&defaultData)[N])
{
D3D11_MAPPED_SUBRESOURCE map;
deviceContext->Map(texture, subresource, D3D11_MAP_WRITE, 0, &map);
unsigned char* ptr = reinterpret_cast<unsigned char*>(map.pData);
size_t pixelSize = sizeof(T) * N;
for (GLsizei y = 0; y < height; y++)
{
for (GLsizei x = 0; x < width; x++)
{
memcpy(ptr + (y * map.RowPitch) + (x * pixelSize), defaultData, pixelSize);
}
}
deviceContext->Unmap(texture, subresource);
}
void Image11::createStagingTexture()
{
if (mStagingTexture)
......@@ -418,6 +445,17 @@ void Image11::createStagingTexture()
mStagingTexture = newTexture;
mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
mDirty = false;
if (mDXGIFormat == DXGI_FORMAT_R8G8B8A8_UNORM && gl::GetAlphaSize(mInternalFormat) == 0)
{
unsigned char defaultPixel[4] = { 0, 0, 0, 255 };
setDefaultData(mRenderer->getDeviceContext(), mStagingTexture, mStagingSubresource, mWidth, mHeight, defaultPixel);
}
else if (mDXGIFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && gl::GetAlphaSize(mInternalFormat) == 0)
{
float defaultPixel[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
setDefaultData(mRenderer->getDeviceContext(), mStagingTexture, mStagingSubresource, mWidth, mHeight, defaultPixel);
}
}
HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
......
......@@ -11,6 +11,9 @@
#include "libGLESv2/renderer/d3d11/RenderStateCache.h"
#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
#include "libGLESv2/Framebuffer.h"
#include "libGLESv2/Renderbuffer.h"
#include "libGLESv2/utilities.h"
#include "common/debug.h"
#include "third_party/murmurhash/MurmurHash3.h"
......@@ -71,21 +74,21 @@ void RenderStateCache::clear()
mSamplerStateCache.clear();
}
std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState)
std::size_t RenderStateCache::hashBlendState(const BlendStateKey &blendState)
{
static const unsigned int seed = 0xABCDEF98;
std::size_t hash = 0;
MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
MurmurHash3_x86_32(&blendState, sizeof(BlendStateKey), seed, &hash);
return hash;
}
bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b)
bool RenderStateCache::compareBlendStates(const BlendStateKey &a, const BlendStateKey &b)
{
return memcmp(&a, &b, sizeof(gl::BlendState)) == 0;
}
ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState)
ID3D11BlendState *RenderStateCache::getBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState)
{
if (!mDevice)
{
......@@ -93,7 +96,36 @@ ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendSta
return NULL;
}
BlendStateMap::iterator i = mBlendStateCache.find(blendState);
bool mrt = false;
BlendStateKey key = { 0 };
key.blendState = blendState;
for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
{
gl::Renderbuffer *renderBuffer = framebuffer->getColorbuffer(i);
if (renderBuffer)
{
if (i > 0)
{
mrt = true;
}
GLenum internalFormat = renderBuffer->getInternalFormat();
key.rtChannels[i][0] = gl::GetRedSize(internalFormat) > 0;
key.rtChannels[i][1] = gl::GetGreenSize(internalFormat) > 0;
key.rtChannels[i][2] = gl::GetBlueSize(internalFormat) > 0;;
key.rtChannels[i][3] = gl::GetAlphaSize(internalFormat) > 0;
}
else
{
key.rtChannels[i][0] = false;
key.rtChannels[i][1] = false;
key.rtChannels[i][2] = false;
key.rtChannels[i][3] = false;
}
}
BlendStateMap::iterator i = mBlendStateCache.find(key);
if (i != mBlendStateCache.end())
{
BlendStateCounterPair &state = i->second;
......@@ -122,7 +154,7 @@ ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendSta
// Create a new blend state and insert it into the cache
D3D11_BLEND_DESC blendDesc = { 0 };
blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
blendDesc.IndependentBlendEnable = FALSE;
blendDesc.IndependentBlendEnable = mrt ? TRUE : FALSE;
for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
{
......@@ -140,10 +172,10 @@ ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendSta
rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
}
rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed,
blendState.colorMaskGreen,
blendState.colorMaskBlue,
blendState.colorMaskAlpha);
rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(key.rtChannels[i][0] && blendState.colorMaskRed,
key.rtChannels[i][1] && blendState.colorMaskGreen,
key.rtChannels[i][2] && blendState.colorMaskBlue,
key.rtChannels[i][3] && blendState.colorMaskAlpha);
}
ID3D11BlendState *dx11BlendState = NULL;
......@@ -154,7 +186,7 @@ ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendSta
return NULL;
}
mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++)));
mBlendStateCache.insert(std::make_pair(key, std::make_pair(dx11BlendState, mCounter++)));
return dx11BlendState;
}
......
......@@ -13,6 +13,11 @@
#include "libGLESv2/angletypes.h"
#include "common/angleutils.h"
namespace gl
{
class Framebuffer;
}
namespace rx
{
......@@ -26,7 +31,7 @@ class RenderStateCache
void clear();
// Increments refcount on the returned blend state, Release() must be called.
ID3D11BlendState *getBlendState(const gl::BlendState &blendState);
ID3D11BlendState *getBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState);
ID3D11RasterizerState *getRasterizerState(const gl::RasterizerState &rasterState,
bool scissorEnabled, unsigned int depthSize);
ID3D11DepthStencilState *getDepthStencilState(const gl::DepthStencilState &dsState);
......@@ -38,14 +43,19 @@ class RenderStateCache
unsigned long long mCounter;
// Blend state cache
static std::size_t hashBlendState(const gl::BlendState &blendState);
static bool compareBlendStates(const gl::BlendState &a, const gl::BlendState &b);
struct BlendStateKey
{
gl::BlendState blendState;
bool rtChannels[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT][4];
};
static std::size_t hashBlendState(const BlendStateKey &blendState);
static bool compareBlendStates(const BlendStateKey &a, const BlendStateKey &b);
static const unsigned int kMaxBlendStates;
typedef std::size_t (*BlendStateHashFunction)(const gl::BlendState &);
typedef bool (*BlendStateEqualityFunction)(const gl::BlendState &, const gl::BlendState &);
typedef std::size_t (*BlendStateHashFunction)(const BlendStateKey &);
typedef bool (*BlendStateEqualityFunction)(const BlendStateKey &, const BlendStateKey &);
typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
typedef std::unordered_map<gl::BlendState, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
typedef std::unordered_map<BlendStateKey, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
BlendStateMap mBlendStateCache;
// Rasterizer state cache
......
......@@ -58,7 +58,7 @@ class Renderer11 : public Renderer
virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
virtual void setRasterizerState(const gl::RasterizerState &rasterState);
virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor,
unsigned int sampleMask);
virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
int stencilBackRef, bool frontFaceCCW);
......@@ -191,10 +191,10 @@ class Renderer11 : public Renderer
void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances);
void readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
GLenum sourceFormat, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
GLint packAlignment, void *pixels);
void maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers);
void maskedClear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
rx::Range getViewportBounds() const;
bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
......
......@@ -852,7 +852,7 @@ void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
mForceSetRasterState = false;
}
void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
void Renderer9::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
{
bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
......
......@@ -72,7 +72,7 @@ class Renderer9 : public Renderer
virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
virtual void setRasterizerState(const gl::RasterizerState &rasterState);
virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor,
unsigned int sampleMask);
virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
int stencilBackRef, bool frontFaceCCW);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment