From e6d51e8ff41e5b3c0718a44f85d7c880a86e144f Mon Sep 17 00:00:00 2001 From: donkopunchstania Date: Wed, 2 Dec 2009 04:17:18 +0000 Subject: [PATCH] Fixed 24 bit depth copies to RAM. 24 and 16 bit depth copies are now more accurate. Added an offset to DX copies to RAM and made half sized copies to a texture linearly filtered. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4635 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Src/TextureConversionShader.cpp | 76 +++++++++++++++---- .../Core/VideoCommon/Src/TextureDecoder.cpp | 2 +- .../Plugin_VideoDX9/Src/TextureCache.cpp | 18 ++--- .../Plugin_VideoDX9/Src/TextureConverter.cpp | 20 ++--- .../Plugin_VideoSoftware/Src/DebugUtil.cpp | 2 +- .../Plugins/Plugin_VideoSoftware/Src/Tev.cpp | 10 ++- 6 files changed, 88 insertions(+), 40 deletions(-) diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index c38504c829..4db14bdeaa 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -611,19 +611,32 @@ void WriteZ16Encoder(char* p,bool HLSL) { WriteSwizzler(p, GX_TF_Z16,HLSL); - WRITE(p, " float depth;\n"); + WRITE(p, " float depth;\n"); + WRITE(p, " float3 expanded;\n"); // byte order is reversed WriteSampleColor(p, "b", "depth",HLSL); - WRITE(p, " ocol0.b = frac(depth * 256.0f);\n"); - WRITE(p, " ocol0.g = depth;\n"); + + WRITE(p, " depth *= 16777215.0f;\n"); + WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); + WRITE(p, " depth -= expanded.r * 256 * 256;\n"); + WRITE(p, " expanded.g = floor(depth / 256);\n"); + + WRITE(p, " ocol0.b = expanded.g / 255;\n"); + WRITE(p, " ocol0.g = expanded.r / 255;\n"); WriteIncrementSampleX(p,HLSL); WriteSampleColor(p, "b", "depth",HLSL); - WRITE(p, " ocol0.r = frac(depth * 256.0f);\n"); - WRITE(p, " ocol0.a = depth;\n"); + + WRITE(p, " depth *= 16777215.0f;\n"); + WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); + WRITE(p, " depth -= expanded.r * 256 * 256;\n"); + WRITE(p, " expanded.g = floor(depth / 256);\n"); + + WRITE(p, " ocol0.r = expanded.g / 255;\n"); + WRITE(p, " ocol0.a = expanded.r / 255;\n"); WriteEncoderEnd(p); } @@ -632,19 +645,36 @@ void WriteZ16LEncoder(char* p,bool HLSL) { WriteSwizzler(p, GX_CTF_Z16L,HLSL); - WRITE(p, " float depth;\n"); + WRITE(p, " float depth;\n"); + WRITE(p, " float3 expanded;\n"); // byte order is reversed WriteSampleColor(p, "b", "depth",HLSL); - WRITE(p, " ocol0.b = frac(depth * 65536.0f);\n"); - WRITE(p, " ocol0.g = frac(depth * 256.0f);\n"); + + WRITE(p, " depth *= 16777215.0f;\n"); + WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); + WRITE(p, " depth -= expanded.r * 256 * 256;\n"); + WRITE(p, " expanded.g = floor(depth / 256);\n"); + WRITE(p, " depth -= expanded.g * 256;\n"); + WRITE(p, " expanded.b = depth;\n"); + + WRITE(p, " ocol0.b = expanded.b / 255;\n"); + WRITE(p, " ocol0.g = expanded.g / 255;\n"); WriteIncrementSampleX(p,HLSL); WriteSampleColor(p, "b", "depth",HLSL); - WRITE(p, " ocol0.r = frac(depth * 65536.0f);\n"); - WRITE(p, " ocol0.a = frac(depth * 256.0f);\n"); + + WRITE(p, " depth *= 16777215.0f;\n"); + WRITE(p, " expanded.r = floor(depth / (256 * 256));\n"); + WRITE(p, " depth -= expanded.r * 256 * 256;\n"); + WRITE(p, " expanded.g = floor(depth / 256);\n"); + WRITE(p, " depth -= expanded.g * 256;\n"); + WRITE(p, " expanded.b = depth;\n"); + + WRITE(p, " ocol0.r = expanded.b;\n"); + WRITE(p, " ocol0.a = expanded.g;\n"); WriteEncoderEnd(p); } @@ -657,24 +687,38 @@ void WriteZ24Encoder(char* p, bool HLSL) WRITE(p, " float depth0;\n"); WRITE(p, " float depth1;\n"); + WRITE(p, " float3 expanded0;\n"); + WRITE(p, " float3 expanded1;\n"); WriteSampleColor(p, "b", "depth0",HLSL); WriteIncrementSampleX(p,HLSL); WriteSampleColor(p, "b", "depth1",HLSL); + for (int i = 0; i < 2; i++) + { + WRITE(p, " depth%i *= 16777215.0f;\n", i); + + WRITE(p, " expanded%i.r = floor(depth%i / (256 * 256));\n", i, i); + WRITE(p, " depth%i -= expanded%i.r * 256 * 256;\n", i, i); + WRITE(p, " expanded%i.g = floor(depth%i / 256);\n", i, i); + WRITE(p, " depth%i -= expanded%i.g * 256;\n", i, i); + WRITE(p, " expanded%i.b = depth%i;\n", i, i); + } + WRITE(p, " if(cl > 0.5f) {\n"); // upper 16 - WRITE(p, " ocol0.b = frac(depth0 * 256.0f);\n"); - WRITE(p, " ocol0.g = depth0;\n"); - WRITE(p, " ocol0.r = frac(depth1 * 256.0f);\n"); - WRITE(p, " ocol0.a = depth1;\n"); + WRITE(p, " ocol0.b = expanded0.g / 255;\n"); + WRITE(p, " ocol0.g = expanded0.b / 255;\n"); + WRITE(p, " ocol0.r = expanded1.g / 255;\n"); + WRITE(p, " ocol0.a = expanded1.b / 255;\n"); WRITE(p, " } else {\n"); // lower 8 WRITE(p, " ocol0.b = 1.0f;\n"); - WRITE(p, " ocol0.g = frac(depth0 * 65536.0f);\n"); + WRITE(p, " ocol0.g = expanded0.r / 255;\n"); WRITE(p, " ocol0.r = 1.0f;\n"); - WRITE(p, " ocol0.a = frac(depth0 * 65536.0f);\n"); + WRITE(p, " ocol0.a = expanded1.r / 255;\n"); WRITE(p, " }\n"); + WriteEncoderEnd(p); } diff --git a/Source/Core/VideoCommon/Src/TextureDecoder.cpp b/Source/Core/VideoCommon/Src/TextureDecoder.cpp index c6214850a7..bbed1cc3cf 100644 --- a/Source/Core/VideoCommon/Src/TextureDecoder.cpp +++ b/Source/Core/VideoCommon/Src/TextureDecoder.cpp @@ -180,7 +180,7 @@ int TexDecoder_GetBlockHeightInTexels(u32 format) case GX_CTF_GB8: return 4; case GX_TF_Z8: return 4; case GX_TF_Z16: return 4; - case GX_TF_Z24X8: return 1; + case GX_TF_Z24X8: return 4; case GX_CTF_Z4: return 8; case GX_CTF_Z8M: return 4; case GX_CTF_Z8L: return 4; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp index a103a8c747..9325b0cf64 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp @@ -474,20 +474,16 @@ have_texture: sourcerect.left = targetSource.left; sourcerect.right = targetSource.right; sourcerect.top = targetSource.top; - if(bFromZBuffer) - { + + if(bScaleByHalf) + D3D::dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); + else D3D::dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); - D3D::dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); - D3D::dev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); - } + D3DFORMAT bformat = FBManager::GetEFBDepthRTSurfaceFormat(); D3D::drawShadedTexQuad(read_texture,&sourcerect, Renderer::GetTargetWidth() , Renderer::GetTargetHeight(),&destrect,((bformat != FOURCC_RAWZ && bformat != D3DFMT_D24X8) && bFromZBuffer)? PixelShaderCache::GetDepthMatrixProgram(): PixelShaderCache::GetColorMatrixProgram(),VertexShaderCache::GetSimpleVertexShader()); - if(bFromZBuffer) - { - D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER); - D3D::RefreshSamplerState(0, D3DSAMP_MAGFILTER); - D3D::RefreshSamplerState(0, D3DSAMP_MIPFILTER); - } + + D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER); D3D::dev->SetRenderTarget(0, FBManager::GetEFBColorRTSurface()); D3D::dev->SetDepthStencilSurface(FBManager::GetEFBDepthRTSurface()); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TextureConverter.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TextureConverter.cpp index 188a91aea1..96fbf390fc 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/TextureConverter.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/TextureConverter.cpp @@ -176,7 +176,7 @@ void Shutdown() s_texConvFrameBuffer = NULL;*/ } -void EncodeToRamUsingShader(LPDIRECT3DPIXELSHADER9 shader, LPDIRECT3DTEXTURE9 srcTexture,int srcTextureWidth,int srcTextureHeight, const TargetRectangle& sourceRc, +void EncodeToRamUsingShader(LPDIRECT3DPIXELSHADER9 shader, LPDIRECT3DTEXTURE9 srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight, int readStride, bool toTexture, bool linearFilter) { HRESULT hr; @@ -227,7 +227,7 @@ void EncodeToRamUsingShader(LPDIRECT3DPIXELSHADER9 shader, LPDIRECT3DTEXTURE9 sr // Draw... - D3D::drawShadedTexQuad(srcTexture,&SrcRect,srcTextureWidth,srcTextureHeight,&DstRect,shader,VertexShaderCache::GetSimpleVertexShader()); + D3D::drawShadedTexQuad(srcTexture,&SrcRect,1,1,&DstRect,shader,VertexShaderCache::GetSimpleVertexShader()); hr = D3D::dev->SetRenderTarget(0, FBManager::GetEFBColorRTSurface()); hr = D3D::dev->SetDepthStencilSurface(FBManager::GetEFBDepthRTSurface()); Renderer::RestoreAPIState(); @@ -258,20 +258,20 @@ void EncodeToRamUsingShader(LPDIRECT3DPIXELSHADER9 shader, LPDIRECT3DTEXTURE9 sr // writing to a texture of a different size int readHeight = readStride / dstWidth; - //readHeight /= 4; // 4 bytes per pixel int readStart = 0; - int readLoops = dstHeight / (readHeight/4); + int readLoops = dstHeight / (readHeight/4); // 4 bytes per pixel u8 *Source = (u8*)drect.pBits; for (int i = 0; i < readLoops; i++) { - memcpy(destAddr,Source,dstWidth*readHeight); - Source += readHeight; + int readDist = dstWidth*readHeight; + memcpy(destAddr,Source,readDist); + Source += readDist; destAddr += writeStride; } } else - memcpy(destAddr,drect.pBits,dstWidth*dstHeight*4); + memcpy(destAddr,drect.pBits,dstWidth*dstHeight*4);// 4 bytes per pixel hr = s_tempConvReadSurface->UnlockRect(); } @@ -330,8 +330,8 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf TextureConversionShader::SetShaderParameters( (float)expandedWidth, expandedHeight * MValueY, - source.left * MValueX, - top, + source.left * MValueX + 0.5f, + top + 0.5f, sampleStride * MValueX, sampleStride * MValueY, (float)Renderer::GetTargetWidth(), @@ -347,7 +347,7 @@ void EncodeToRam(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyf cacheBytes = 64; int readStride = (expandedWidth * cacheBytes) / TexDecoder_GetBlockWidthInTexels(format); - EncodeToRamUsingShader(texconv_shader, source_texture,1,1, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight,readStride, true, bScaleByHalf > 0); + EncodeToRamUsingShader(texconv_shader, source_texture, scaledSource, dest_ptr, expandedWidth / samples, expandedHeight,readStride, true, bScaleByHalf > 0); } /*void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, diff --git a/Source/Plugins/Plugin_VideoSoftware/Src/DebugUtil.cpp b/Source/Plugins/Plugin_VideoSoftware/Src/DebugUtil.cpp index 3b07a9faac..13dc38d419 100644 --- a/Source/Plugins/Plugin_VideoSoftware/Src/DebugUtil.cpp +++ b/Source/Plugins/Plugin_VideoSoftware/Src/DebugUtil.cpp @@ -68,7 +68,7 @@ void SaveTexture(const char* filename, u32 texmap) TexImage0& ti0 = texUnit.texImage0[subTexmap]; - SaveTexture(filename, texmap, ti0.width, ti0.height); + SaveTexture(filename, texmap, ti0.width + 1, ti0.height + 1); } void GetTextureBGRA(u8 *dst, u32 texmap, int width, int height) diff --git a/Source/Plugins/Plugin_VideoSoftware/Src/Tev.cpp b/Source/Plugins/Plugin_VideoSoftware/Src/Tev.cpp index f4e764b920..83c095c972 100644 --- a/Source/Plugins/Plugin_VideoSoftware/Src/Tev.cpp +++ b/Source/Plugins/Plugin_VideoSoftware/Src/Tev.cpp @@ -583,7 +583,15 @@ void Tev::Draw() float scaleS = bpmem.texscale[stageNum2].getScaleS(stageOdd); float scaleT = bpmem.texscale[stageNum2].getScaleT(stageOdd); - TextureSampler::Sample(Uv[texcoordSel][0] * scaleS, Uv[texcoordSel][1] * scaleT, Lod[texcoordSel], texmap, IndirectTex[stageNum]); + TextureSampler::Sample(Uv[texcoordSel][0] * scaleS, Uv[texcoordSel][1] * scaleT, Lod[texcoordSel], texmap, IndirectTex[stageNum]); + +#ifdef _DEBUG + if (g_Config.bDumpTevStages) + { + u8 stage[4] = {(u8)IndirectTex[stageNum][3], (u8)IndirectTex[stageNum][2], (u8)IndirectTex[stageNum][1], 255}; + DebugUtil::DrawObjectBuffer(Position[0], Position[1], stage, 16+stageNum, "Ind"); + } +#endif } for (unsigned int stageNum = 0; stageNum <= bpmem.genMode.numtevstages; stageNum++)