From 762ce28977725bedb87af3e2ced49aa3563b541a Mon Sep 17 00:00:00 2001 From: NeoBrainX Date: Wed, 16 Jun 2010 13:36:40 +0000 Subject: [PATCH] DX11: Disable D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY when compiling pixel shaders in Debug configurations as well. Properly support centered text drawing, even though it's not used, yet. Credits go to xsacha for this one. Found an awesome hacky way to free the buffer memory used by ReplaceTexture2D. At least it gets freed at all now... Various other tweaks to texture conversion. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5724 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Plugin_VideoDX11/Src/D3DShader.cpp | 2 +- .../Plugin_VideoDX11/Src/D3DTexture.cpp | 46 +++++----- .../Plugins/Plugin_VideoDX11/Src/D3DUtil.cpp | 87 ++++++++----------- .../Plugin_VideoDX11/Src/TextureCache.cpp | 4 +- 4 files changed, 63 insertions(+), 76 deletions(-) diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DShader.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DShader.cpp index 699cdbe982..e06b53d378 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DShader.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DShader.cpp @@ -88,7 +88,7 @@ bool CompilePixelShader(const char* code, unsigned int len, ID3D10Blob** blob) ID3D10Blob* errorBuffer = NULL; #if defined(_DEBUG) || defined(DEBUGFAST) - UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY|D3D10_SHADER_DEBUG|D3D10_SHADER_WARNINGS_ARE_ERRORS; + UINT flags = D3D10_SHADER_DEBUG|D3D10_SHADER_WARNINGS_ARE_ERRORS; #else UINT flags = D3D10_SHADER_OPTIMIZATION_LEVEL3; #endif diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp index d1e7c066f7..e4c86cce91 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DTexture.cpp @@ -23,12 +23,23 @@ namespace D3D { // buffers for storing the data for DEFAULT textures -const char* texbuf = NULL; +u8* texbuf = NULL; unsigned int texbufsize = 0; +// TODO: Remove this class and properly clean up texbuf! +struct TexbufDeleter +{ + ~TexbufDeleter() + { + if (texbuf) delete[] texbuf; + texbuf = NULL; + texbufsize = 0; + } +} texbufdeleter; + void ReplaceTexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int width, unsigned int height, unsigned int pitch, DXGI_FORMAT fmt, PC_TexFormat pcfmt, unsigned int level, D3D11_USAGE usage) { - void* outptr; + u8* outptr; unsigned int destPitch; bool bExpand = false; @@ -37,19 +48,18 @@ void ReplaceTexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int if (level != 0) PanicAlert("Dynamic textures don't support mipmaps, but given level is not 0 at %s %d\n", __FILE__, __LINE__); D3D11_MAPPED_SUBRESOURCE map; D3D::context->Map(pTexture, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); - outptr = map.pData; + outptr = (u8*)map.pData; destPitch = map.RowPitch; } else if (usage == D3D11_USAGE_DEFAULT && pcfmt != PC_TEX_FMT_BGRA32) { if (texbufsize < 4*width*height) { - // TODO: This memory needs to be freed as well.. if (texbuf) delete[] texbuf; - texbuf = new char[4*width*height]; + texbuf = new u8[4*width*height]; texbufsize = 4*width*height; } - outptr = (void*)texbuf; + outptr = texbuf; destPitch = width * 4; } else if (usage == D3D11_USAGE_DEFAULT && pcfmt == PC_TEX_FMT_BGRA32) @@ -73,12 +83,12 @@ void ReplaceTexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int for (unsigned int y = 0; y < height; y++) { u16* in = (u16*)buffer + y * pitch; - u32* pBits = (u32*)((u8*)outptr + y * destPitch); + u32* pBits = (u32*)(outptr + y * destPitch); for (unsigned int x = 0; x < width; x++) { const u8 I = (*in & 0xFF); const u8 A = (*in & 0xFF00) >> 8; - *(pBits++) = (A << 24) | (I << 16) | (I << 8) | I; + *pBits++ = (A << 24) | (I << 16) | (I << 8) | I; in++; } } @@ -87,32 +97,26 @@ void ReplaceTexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int case PC_TEX_FMT_I4_AS_I8: for (unsigned int y = 0; y < height; y++) { - const u8* in = buffer + (y * pitch); - u32* pBits = (u32*)((u8*)outptr + (y * destPitch)); + const u8* in = buffer + y * pitch; + u32* pBits = (u32*)(outptr + y * destPitch); for(unsigned int i = 0; i < width; i++) - { - const u8 I = *(in++); - memset( pBits++, I, 4 ); - } + memset( pBits++, *in++, 4 ); } break; case PC_TEX_FMT_BGRA32: for (unsigned int y = 0; y < height; y++) - { - u32* in = (u32*)buffer + y * pitch; - u32* pBits = (u32*)((u8*)outptr + y * destPitch); - memcpy( pBits, in, destPitch ); - } + memcpy( outptr + y * destPitch, (u32*)buffer + y * pitch, destPitch ); + break; case PC_TEX_FMT_RGB565: for (unsigned int y = 0; y < height; y++) { u16* in = (u16*)buffer + y * pitch; - u32* pBits = (u32*)((u8*)outptr + y * destPitch); + u32* pBits = (u32*)(outptr + y * destPitch); for (unsigned int x = 0; x < width; x++) { // we can't simply shift here, since e.g. 11111 must map to 11111111 and not 11111000 - const u16 col = *(in++); + const u16 col = *in++; *(pBits++) = 0xFF000000 | // alpha ((((col&0xF800) << 5) * 255 / 31) & 0xFF0000) | // red ((((col& 0x7e0) << 3) * 255 / 63) & 0xFF00) | // green diff --git a/Source/Plugins/Plugin_VideoDX11/Src/D3DUtil.cpp b/Source/Plugins/Plugin_VideoDX11/Src/D3DUtil.cpp index 4526cb47e9..ba2a87d582 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/D3DUtil.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/D3DUtil.cpp @@ -269,12 +269,11 @@ int CD3DFont::DrawTextScaled(float x, float y, float scale, float spacing, u32 d UINT stride = sizeof(FONT2DVERTEX); UINT bufoffset = 0; - float sx = x; - float sy = y; + // translate starting positions + float sx = x / m_dwTexWidth - 1; + float sy = 1 - y / m_dwTexHeight; + char c; - float fStartX = sx; - - float invLineHeight = 1.0f / ((m_fTexCoords[0][3] - m_fTexCoords[0][1]) * m_dwTexHeight); // fill vertex buffer FONT2DVERTEX* pVertices; int dwNumTriangles = 0L; @@ -284,42 +283,30 @@ int CD3DFont::DrawTextScaled(float x, float y, float scale, float spacing, u32 d if (FAILED(hr)) PanicAlert("Mapping vertex buffer failed, %s %d\n", __FILE__, __LINE__); pVertices = (D3D::FONT2DVERTEX*)vbmap.pData; - const char* oldstrText = strText; - // first, let's measure the text - float tw=0; - float mx=0; - float maxx=0; - - while (*strText) - { - char c = *strText++; - - if (c == ('\n')) - mx = 0; - if (c < (' ')) - continue; - - float tx1 = m_fTexCoords[c-32][0]; - float tx2 = m_fTexCoords[c-32][2]; - - float w = (tx2-tx1)*m_dwTexWidth; - w *= scale*invLineHeight; - mx += w + spacing*scale; - if (mx > maxx) maxx = mx; - } - - float offset = -maxx/2; - strText = oldstrText; - //Then let's draw it + // if center was requested, set current position as centre + // this is currently never used if (center) { - sx += offset; - fStartX += offset; + const char *oldText = strText; + float mx=0; + float maxx=0; + + while (c = *strText++) + { + if (c == ('\n')) + mx = 0; + if (c < (' ')) + continue; + c -= 32; + mx += (m_fTexCoords[c][2]-m_fTexCoords[c][0])/(m_fTexCoords[0][3] - m_fTexCoords[0][1]) + + spacing; + if (mx > maxx) maxx = mx; + } + sx -= scale*maxx/(2*m_dwTexWidth); + strText = oldText; } - - float wScale = scale*invLineHeight; - float hScale = scale*invLineHeight; - + // we now have a starting point + float fStartX = sx; // set general pipeline state D3D::context->OMSetBlendState(m_blendstate, NULL, 0xFFFFFFFF); D3D::context->RSSetState(m_raststate); @@ -330,33 +317,29 @@ int CD3DFont::DrawTextScaled(float x, float y, float scale, float spacing, u32 d D3D::context->IASetInputLayout(m_InputLayout); D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); D3D::context->PSSetShaderResources(0, 1, &m_pTexture); - - while (*strText) + while (c = *strText++) { - char c = *strText++; - if (c == ('\n')) { sx = fStartX; - sy += scale; + sy -= scale / m_dwTexHeight; } if (c < (' ')) continue; - - c-=32; + c -= 32; float tx1 = m_fTexCoords[c][0]; float ty1 = m_fTexCoords[c][1]; float tx2 = m_fTexCoords[c][2]; float ty2 = m_fTexCoords[c][3]; - float w = (tx2-tx1)*m_dwTexWidth/2; - float h = (ty2-ty1)*m_dwTexHeight/2; + float w = (tx2-tx1)/2; + float h = (ty1-ty2)/2; FONT2DVERTEX v[6]; - v[0] = InitFont2DVertex( sx /m_dwTexWidth-1.f, 1-((sy+h)/m_dwTexHeight), dwColor, tx1, ty2); - v[1] = InitFont2DVertex( sx /m_dwTexWidth-1.f, 1-( sy /m_dwTexHeight), dwColor, tx1, ty1); - v[2] = InitFont2DVertex((sx+w)/m_dwTexWidth-1.f, 1-((sy+h)/m_dwTexHeight), dwColor, tx2, ty2); - v[3] = InitFont2DVertex((sx+w)/m_dwTexWidth-1.f, 1-( sy /m_dwTexHeight), dwColor, tx2, ty1); + v[0] = InitFont2DVertex( sx, h+sy, dwColor, tx1, ty2); + v[1] = InitFont2DVertex( sx, sy, dwColor, tx1, ty1); + v[2] = InitFont2DVertex(w+sx, h+sy, dwColor, tx2, ty2); + v[3] = InitFont2DVertex(w+sx, sy, dwColor, tx2, ty1); v[4] = v[2]; v[5] = v[1]; @@ -378,7 +361,7 @@ int CD3DFont::DrawTextScaled(float x, float y, float scale, float spacing, u32 d if (FAILED(hr)) PanicAlert("Mapping vertex buffer failed, %s %d\n", __FILE__, __LINE__); pVertices = (D3D::FONT2DVERTEX*)vbmap.pData; } - sx += w + spacing*scale; + sx += w + spacing*scale/m_dwTexWidth; } // Unlock and render the vertex buffer diff --git a/Source/Plugins/Plugin_VideoDX11/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoDX11/Src/TextureCache.cpp index 87bf54189d..9e408c3bd5 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/TextureCache.cpp @@ -111,7 +111,7 @@ void TextureCache::Init() void TextureCache::Invalidate(bool shutdown) { - for (TexCache::iterator iter = textures.begin(); iter != textures.end(); iter++) + for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter) iter->second.Destroy(shutdown); textures.clear(); HiresTextures::Shutdown(); @@ -167,7 +167,7 @@ void TextureCache::Cleanup() } else { - iter++; + ++iter; } } }