(view as text)
diff --git a/Source/Core/Common/MathUtil.h b/Source/Core/Common/MathUtil.h
index da12822..14b9309 100644
--- a/Source/Core/Common/MathUtil.h
+++ b/Source/Core/Common/MathUtil.h
@@ -150,7 +150,7 @@ float MathFloatVectorSum(const std::vector<float>&);
 #define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
 
 // Rounds down. 0 -> undefined
-inline u64 Log2(u64 val)
+inline int Log2(u64 val)
 {
 #if defined(__GNUC__)
 	return 63 - __builtin_clzll(val);
@@ -161,7 +161,7 @@ inline u64 Log2(u64 val)
 	return result;
 
 #else
-	u64 result = -1;
+	int result = -1;
 	while (val != 0)
 	{
 		val >>= 1;
diff --git a/Source/Core/DolphinWX/GameListCtrl.cpp b/Source/Core/DolphinWX/GameListCtrl.cpp
index bd420f8..6029c74 100644
--- a/Source/Core/DolphinWX/GameListCtrl.cpp
+++ b/Source/Core/DolphinWX/GameListCtrl.cpp
@@ -413,12 +413,12 @@ wxString NiceSizeFormat(u64 _size)
 {
 	const char* const unit_symbols[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
 
-	auto const unit = Log2(std::max<u64>(_size, 1)) / 10;
-	auto const unit_size = (1 << (unit * 10));
+	const u64 unit = Log2(std::max<u64>(_size, 1)) / 10;
+	const u64 unit_size = (1 << (unit * 10));
 
 	// ugly rounding integer math
-	auto const value = (_size + unit_size / 2) / unit_size;
-	auto const frac = (_size % unit_size * 10 + unit_size / 2) / unit_size % 10;
+	const u64 value = (_size + unit_size / 2) / unit_size;
+	const u64 frac = (_size % unit_size * 10 + unit_size / 2) / unit_size % 10;
 
 	return StrToWxStr(StringFromFormat("%" PRIu64 ".%" PRIu64 " %s", value, frac, unit_symbols[unit]));
 }
diff --git a/Source/Core/VideoBackends/OGL/TextureConverter.cpp b/Source/Core/VideoBackends/OGL/TextureConverter.cpp
index 753bd64..dbbb605 100644
--- a/Source/Core/VideoBackends/OGL/TextureConverter.cpp
+++ b/Source/Core/VideoBackends/OGL/TextureConverter.cpp
@@ -117,7 +117,7 @@ void CreatePrograms()
 		"	ivec2 uv = ivec2(gl_FragCoord.xy);\n"
 			// We switch top/bottom here. TODO: move this to screen blit.
 		"	ivec2 ts = textureSize(samp9, 0);\n"
-		"	vec4 c0 = texelFetch(samp9, ivec2(uv.x/2, ts.y-uv.y-1), 0);\n"
+		"	vec4 c0 = texelFetch(samp9, ivec2(uv.x>>1, ts.y-uv.y-1), 0);\n"
 		"	float y = mix(c0.b, c0.r, (uv.x & 1) == 1);\n"
 		"	float yComp = 1.164 * (y - 0.0625);\n"
 		"	float uComp = c0.g - 0.5;\n"
diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp
index 5abce6f..d48c03c 100644
--- a/Source/Core/VideoCommon/TextureCacheBase.cpp
+++ b/Source/Core/VideoCommon/TextureCacheBase.cpp
@@ -886,4 +886,7 @@ void TextureCache::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat
 	entry->frameCount = frameCount;
 
 	entry->FromRenderTarget(dstAddr, dstFormat, srcFormat, srcRect, isIntensity, scaleByHalf, cbufid, colmat);
+
+	delete entry;
+	textures.erase(dstAddr);
 }
diff --git a/Source/Core/VideoCommon/TextureConversionShader.cpp b/Source/Core/VideoCommon/TextureConversionShader.cpp
index a7db334..9054eac 100644
--- a/Source/Core/VideoCommon/TextureConversionShader.cpp
+++ b/Source/Core/VideoCommon/TextureConversionShader.cpp
@@ -14,6 +14,7 @@
 #include "VideoCommon/TextureConversionShader.h"
 #include "VideoCommon/TextureDecoder.h"
 #include "VideoCommon/VideoConfig.h"
+#include "Common/MathUtil.h"
 
 #define WRITE p+=sprintf
 
@@ -65,8 +66,7 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
 	int blkW = TexDecoder_GetBlockWidthInTexels(format);
 	int blkH = TexDecoder_GetBlockHeightInTexels(format);
 	int samples = GetEncodedSampleCount(format);
-	// 32 bit textures (RGBA8 and Z24) are store in 2 cache line increments
-	int factor = samples == 1 ? 2 : 1;
+
 	if (ApiType == API_OPENGL)
 	{
 		WRITE(p, "#define samp0 samp9\n");
@@ -90,21 +90,21 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
 	"  float2 uv0 = float2(0.0, 0.0);\n"
 	);
 
-	WRITE(p, "  uv1.x = uv1.x * %d;\n", samples);
-
-	WRITE(p, "  int yl = uv1.y / %d;\n", blkH);
-	WRITE(p, "  int yb = yl * %d;\n", blkH);
-	WRITE(p, "  int yoff = uv1.y - yb;\n");
-	WRITE(p, "  int xp = uv1.x + yoff * position.z;\n");
-	WRITE(p, "  int xel = xp / %d;\n", samples == 1 ? factor : blkW);
-	WRITE(p, "  int xb = xel / %d;\n", blkH);
-	WRITE(p, "  int xoff = xel - xb * %d;\n", blkH);
-	WRITE(p, "  int xl =  uv1.x * %d / %d;\n", factor, blkW);
-	WRITE(p, "  int xib = uv1.x * %d - xl * %d;\n", factor, blkW);
-	WRITE(p, "  int halfxb = xb / %d;\n", factor);
-
-	WRITE(p, "  sampleUv.x = xib + halfxb * %d;\n", blkW);
-	WRITE(p, "  sampleUv.y = yb + xoff;\n");
+	WRITE(p, "  int y_block_position = uv1.y & ~(%d - 1);\n", blkH);
+	WRITE(p, "  int y_offset_in_block = uv1.y & (%d - 1);\n", blkH);
+	WRITE(p, "  int x_virtual_position = (uv1.x << %d) + y_offset_in_block * position.z;\n", Log2(samples));
+	WRITE(p, "  int x_block_position = (x_virtual_position >> %d) & ~(%d - 1);\n", Log2(blkH), blkW);
+	if (samples == 1)
+	{
+		// 32 bit textures (RGBA8 and Z24) are store in 2 cache line increments
+		WRITE(p, "  x_virtual_position = x_virtual_position << 1;\n");
+		WRITE(p, "  bool first = 0 == (uv1.x & 8);\n"); // first cache line, used in the encoders
+	}
+	WRITE(p, "  int x_offset_in_block = x_virtual_position & (%d - 1);\n", blkW);
+	WRITE(p, "  int y_offset = (x_virtual_position >> %d) & (%d - 1);\n", Log2(blkW), blkH);
+
+	WRITE(p, "  sampleUv.x = x_offset_in_block + x_block_position;\n");
+	WRITE(p, "  sampleUv.y = y_block_position + y_offset;\n");
 }
 
 void WriteSampleColor(char*& p, const char* colorComp, const char* dest, int xoffset, API_TYPE ApiType)
@@ -373,8 +373,6 @@ void WriteRGBA8Encoder(char* p,API_TYPE ApiType)
 {
 	WriteSwizzler(p, GX_TF_RGBA8, ApiType);
 
-	WRITE(p, "  bool first = xb == (halfxb * 2);\n");
-
 	WRITE(p, "  float4 texSample;\n");
 	WRITE(p, "  float4 color0;\n");
 	WRITE(p, "  float4 color1;\n");
@@ -563,8 +561,6 @@ void WriteZ24Encoder(char* p, API_TYPE ApiType)
 {
 	WriteSwizzler(p, GX_TF_Z24X8, ApiType);
 
-	WRITE(p, "  bool first = xb == (halfxb * 2);\n");
-
 	WRITE(p, "  float depth0;\n");
 	WRITE(p, "  float depth1;\n");
 	WRITE(p, "  float3 expanded0;\n");