Example #1
0
static void TexMtx_ReadDirect_UByte(VertexLoader* loader)
{
	loader->m_curtexmtx[loader->m_texmtxread] = DataReadU8() & 0x3f;

	PRIM_LOG("texmtx%d: %d, ", loader->m_texmtxread, loader->m_curtexmtx[loader->m_texmtxread]);
	loader->m_texmtxread++;
}
Example #2
0
static void LOADERDECL TexMtx_ReadDirect_UByte(VertexLoader* loader)
{
	BoundingBox::texMtxIdx[loader->m_texmtxread] = loader->m_curtexmtx[loader->m_texmtxread] = DataReadU8() & 0x3f;

	PRIM_LOG("texmtx%d: %d, ", loader->m_texmtxread, loader->m_curtexmtx[loader->m_texmtxread]);
	loader->m_texmtxread++;
}
void PixelShaderManager::SetTevKonstColor(int index, int component, s32 value)
{
	auto& c = constants.kcolors[index];
	c[component] = value;
	dirty = true;

	PRIM_LOG("tev konst color%d: %d %d %d %d\n", index, c[0], c[1], c[2], c[3]);
}
Example #4
0
static void PosMtx_ReadDirect_UByte(VertexLoader* loader)
{
	u32 posmtx = DataReadU8() & 0x3f;
	if (loader->m_counter < 3)
		VertexLoaderManager::position_matrix_index[loader->m_counter] = posmtx;
	DataWrite<u32>(posmtx);
	PRIM_LOG("posmtx: %d, ", posmtx);
}
Example #5
0
int VertexLoader::RunVertices(DataReader src, DataReader dst, int count)
{
	g_vertex_manager_write_ptr = dst.GetPointer();
	g_video_buffer_read_ptr = src.GetPointer();

	m_numLoadedVertices += count;
	m_skippedVertices = 0;

	for (m_counter = count - 1; m_counter >= 0; m_counter--)
	{
		m_tcIndex = 0;
		m_colIndex = 0;
		m_texmtxwrite = m_texmtxread = 0;
		for (int i = 0; i < m_numPipelineStages; i++)
			m_PipelineStages[i](this);
		PRIM_LOG("\n");
	}

	return count - m_skippedVertices;
}
void PixelShaderManager::SetIndMatrixChanged(int matrixidx)
{
  int scale = ((u32)bpmem.indmtx[matrixidx].col0.s0 << 0) |
              ((u32)bpmem.indmtx[matrixidx].col1.s1 << 2) |
              ((u32)bpmem.indmtx[matrixidx].col2.s2 << 4);

  // xyz - static matrix
  // w - dynamic matrix scale / 128
  constants.indtexmtx[2 * matrixidx][0] = bpmem.indmtx[matrixidx].col0.ma;
  constants.indtexmtx[2 * matrixidx][1] = bpmem.indmtx[matrixidx].col1.mc;
  constants.indtexmtx[2 * matrixidx][2] = bpmem.indmtx[matrixidx].col2.me;
  constants.indtexmtx[2 * matrixidx][3] = 17 - scale;
  constants.indtexmtx[2 * matrixidx + 1][0] = bpmem.indmtx[matrixidx].col0.mb;
  constants.indtexmtx[2 * matrixidx + 1][1] = bpmem.indmtx[matrixidx].col1.md;
  constants.indtexmtx[2 * matrixidx + 1][2] = bpmem.indmtx[matrixidx].col2.mf;
  constants.indtexmtx[2 * matrixidx + 1][3] = 17 - scale;
  dirty = true;

  PRIM_LOG("indmtx%d: scale=%d, mat=(%d %d %d; %d %d %d)\n", matrixidx, scale,
           bpmem.indmtx[matrixidx].col0.ma, bpmem.indmtx[matrixidx].col1.mc,
           bpmem.indmtx[matrixidx].col2.me, bpmem.indmtx[matrixidx].col0.mb,
           bpmem.indmtx[matrixidx].col1.md, bpmem.indmtx[matrixidx].col2.mf);
}
Example #7
0
int VertexLoader::RunVertices(DataReader src, DataReader dst, int count, int primitive)
{
	g_vertex_manager_write_ptr = dst.GetPointer();
	g_video_buffer_read_ptr = src.GetPointer();

	m_numLoadedVertices += count;
	m_skippedVertices = 0;

	// Prepare bounding box
	if (!g_ActiveConfig.backend_info.bSupportsBBox)
		BoundingBox::Prepare(m_vat, primitive, m_VtxDesc, m_native_vtx_decl);

	for (int s = 0; s < count; s++)
	{
		m_tcIndex = 0;
		m_colIndex = 0;
		m_texmtxwrite = m_texmtxread = 0;
		for (int i = 0; i < m_numPipelineStages; i++)
			m_PipelineStages[i](this);
		PRIM_LOG("\n");
	}

	return count - m_skippedVertices;
}
Example #8
0
void VertexManager::Flush()
{
	if (IsFlushed) return;

	// loading a state will invalidate BP, so check for it
	g_video_backend->CheckInvalidState();

	VideoFifo_CheckEFBAccess();

#if defined(_DEBUG) || defined(DEBUGFAST)
	PRIM_LOG("frame%d:\n texgen=%d, numchan=%d, dualtex=%d, ztex=%d, cole=%d, alpe=%d, ze=%d", g_ActiveConfig.iSaveTargetId, xfregs.numTexGen.numTexGens,
		xfregs.numChan.numColorChans, xfregs.dualTexTrans.enabled, bpmem.ztex2.op,
		bpmem.blendmode.colorupdate, bpmem.blendmode.alphaupdate, bpmem.zmode.updateenable);

	for (unsigned int i = 0; i < xfregs.numChan.numColorChans; ++i)
	{
		LitChannel* ch = &xfregs.color[i];
		PRIM_LOG("colchan%d: matsrc=%d, light=0x%x, ambsrc=%d, diffunc=%d, attfunc=%d", i, ch->matsource, ch->GetFullLightMask(), ch->ambsource, ch->diffusefunc, ch->attnfunc);
		ch = &xfregs.alpha[i];
		PRIM_LOG("alpchan%d: matsrc=%d, light=0x%x, ambsrc=%d, diffunc=%d, attfunc=%d", i, ch->matsource, ch->GetFullLightMask(), ch->ambsource, ch->diffusefunc, ch->attnfunc);
	}

	for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i)
	{
		TexMtxInfo tinfo = xfregs.texMtxInfo[i];
		if (tinfo.texgentype != XF_TEXGEN_EMBOSS_MAP) tinfo.hex &= 0x7ff;
		if (tinfo.texgentype != XF_TEXGEN_REGULAR) tinfo.projection = 0;

		PRIM_LOG("txgen%d: proj=%d, input=%d, gentype=%d, srcrow=%d, embsrc=%d, emblght=%d, postmtx=%d, postnorm=%d",
			i, tinfo.projection, tinfo.inputform, tinfo.texgentype, tinfo.sourcerow, tinfo.embosssourceshift, tinfo.embosslightshift,
			xfregs.postMtxInfo[i].index, xfregs.postMtxInfo[i].normalize);
	}

	PRIM_LOG("pixel: tev=%d, ind=%d, texgen=%d, dstalpha=%d, alphatest=0x%x", bpmem.genMode.numtevstages+1, bpmem.genMode.numindstages,
		bpmem.genMode.numtexgens, (u32)bpmem.dstalpha.enable, (bpmem.alpha_test.hex>>16)&0xff);
#endif

	u32 usedtextures = 0;
	for (u32 i = 0; i < bpmem.genMode.numtevstages + 1u; ++i)
		if (bpmem.tevorders[i / 2].getEnable(i & 1))
			usedtextures |= 1 << bpmem.tevorders[i/2].getTexMap(i & 1);

	if (bpmem.genMode.numindstages > 0)
		for (unsigned int i = 0; i < bpmem.genMode.numtevstages + 1u; ++i)
			if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < bpmem.genMode.numindstages)
				usedtextures |= 1 << bpmem.tevindref.getTexMap(bpmem.tevind[i].bt);

	for (unsigned int i = 0; i < 8; i++)
	{
		if (usedtextures & (1 << i))
		{
			g_renderer->SetSamplerState(i & 3, i >> 2);
			const FourTexUnits &tex = bpmem.tex[i >> 2];
			const TextureCache::TCacheEntryBase* tentry = TextureCache::Load(i,
				(tex.texImage3[i&3].image_base/* & 0x1FFFFF*/) << 5,
				tex.texImage0[i&3].width + 1, tex.texImage0[i&3].height + 1,
				tex.texImage0[i&3].format, tex.texTlut[i&3].tmem_offset<<9,
				tex.texTlut[i&3].tlut_format,
				((tex.texMode0[i&3].min_filter & 3) != 0),
				(tex.texMode1[i&3].max_lod + 0xf) / 0x10,
				(tex.texImage1[i&3].image_type != 0));

			if (tentry)
			{
				// 0s are probably for no manual wrapping needed.
				PixelShaderManager::SetTexDims(i, tentry->native_width, tentry->native_height, 0, 0);
			}
			else
				ERROR_LOG(VIDEO, "error loading texture");
		}
	}
// Syncs the shader constant buffers with xfmem
// TODO: A cleaner way to control the matrices without making a mess in the parameters field
void VertexShaderManager::SetConstants()
{
	if (nTransformMatricesChanged[0] >= 0)
	{
		int startn = nTransformMatricesChanged[0] / 4;
		int endn = (nTransformMatricesChanged[1] + 3) / 4;
		memcpy(constants.transformmatrices[startn], &xfmem.posMatrices[startn * 4], (endn - startn) * 16);
		dirty = true;
		nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1;
	}

	if (nNormalMatricesChanged[0] >= 0)
	{
		int startn = nNormalMatricesChanged[0] / 3;
		int endn = (nNormalMatricesChanged[1] + 2) / 3;
		for (int i=startn; i<endn; i++)
		{
			memcpy(constants.normalmatrices[i], &xfmem.normalMatrices[3*i], 12);
		}
		dirty = true;
		nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
	}

	if (nPostTransformMatricesChanged[0] >= 0)
	{
		int startn = nPostTransformMatricesChanged[0] / 4;
		int endn = (nPostTransformMatricesChanged[1] + 3 ) / 4;
		memcpy(constants.posttransformmatrices[startn], &xfmem.postMatrices[startn * 4], (endn - startn) * 16);
		dirty = true;
		nPostTransformMatricesChanged[0] = nPostTransformMatricesChanged[1] = -1;
	}

	if (nLightsChanged[0] >= 0)
	{
		// TODO: Outdated comment
		// lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats
		int istart = nLightsChanged[0] / 0x10;
		int iend = (nLightsChanged[1] + 15) / 0x10;

		for (int i = istart; i < iend; ++i)
		{
			const Light& light = xfmem.lights[i];
			VertexShaderConstants::Light& dstlight = constants.lights[i];

			// xfmem.light.color is packed as abgr in u8[4], so we have to swap the order
			dstlight.color[0] = light.color[3];
			dstlight.color[1] = light.color[2];
			dstlight.color[2] = light.color[1];
			dstlight.color[3] = light.color[0];

			dstlight.cosatt[0] = light.cosatt[0];
			dstlight.cosatt[1] = light.cosatt[1];
			dstlight.cosatt[2] = light.cosatt[2];

			if (fabs(light.distatt[0]) < 0.00001f &&
			    fabs(light.distatt[1]) < 0.00001f &&
			    fabs(light.distatt[2]) < 0.00001f)
			{
				// dist attenuation, make sure not equal to 0!!!
				dstlight.distatt[0] = .00001f;
			}
			else
			{
				dstlight.distatt[0] = light.distatt[0];
			}
			dstlight.distatt[1] = light.distatt[1];
			dstlight.distatt[2] = light.distatt[2];

			dstlight.pos[0] = light.dpos[0];
			dstlight.pos[1] = light.dpos[1];
			dstlight.pos[2] = light.dpos[2];

			double norm = double(light.ddir[0]) * double(light.ddir[0]) +
			              double(light.ddir[1]) * double(light.ddir[1]) +
			              double(light.ddir[2]) * double(light.ddir[2]);
			norm = 1.0 / sqrt(norm);
			float norm_float = static_cast<float>(norm);
			dstlight.dir[0] = light.ddir[0] * norm_float;
			dstlight.dir[1] = light.ddir[1] * norm_float;
			dstlight.dir[2] = light.ddir[2] * norm_float;
		}
		dirty = true;

		nLightsChanged[0] = nLightsChanged[1] = -1;
	}

	for (int i : nMaterialsChanged)
	{
		u32 data = i >= 2 ? xfmem.matColor[i - 2] : xfmem.ambColor[i];
		constants.materials[i][0] = (data >> 24) & 0xFF;
		constants.materials[i][1] = (data >> 16) & 0xFF;
		constants.materials[i][2] = (data >>  8) & 0xFF;
		constants.materials[i][3] =  data        & 0xFF;
		dirty = true;
	}
	nMaterialsChanged = BitSet32(0);

	if (bPosNormalMatrixChanged)
	{
		bPosNormalMatrixChanged = false;

		const float *pos = (const float *)xfmem.posMatrices + g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4;
		const float *norm = (const float *)xfmem.normalMatrices + 3 * (g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31);

		memcpy(constants.posnormalmatrix, pos, 3*16);
		memcpy(constants.posnormalmatrix[3], norm, 12);
		memcpy(constants.posnormalmatrix[4], norm+3, 12);
		memcpy(constants.posnormalmatrix[5], norm+6, 12);
		dirty = true;
	}

	if (bTexMatricesChanged[0])
	{
		bTexMatricesChanged[0] = false;
		const float *fptrs[] =
		{
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4]
		};

		for (int i = 0; i < 4; ++i)
		{
			memcpy(constants.texmatrices[3*i], fptrs[i], 3*16);
		}
		dirty = true;
	}

	if (bTexMatricesChanged[1])
	{
		bTexMatricesChanged[1] = false;
		const float *fptrs[] = {
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4],
			(const float *)&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4]
		};

		for (int i = 0; i < 4; ++i)
		{
			memcpy(constants.texmatrices[3*i+12], fptrs[i], 3*16);
		}
		dirty = true;
	}

	if (bViewportChanged)
	{
		bViewportChanged = false;

		// The console GPU places the pixel center at 7/12 unless antialiasing
		// is enabled, while D3D and OpenGL place it at 0.5. See the comment
		// in VertexShaderGen.cpp for details.
		// NOTE: If we ever emulate antialiasing, the sample locations set by
		// BP registers 0x01-0x04 need to be considered here.
		const float pixel_center_correction = 7.0f / 12.0f - 0.5f;
		const float pixel_size_x = 2.f / Renderer::EFBToScaledXf(2.f * xfmem.viewport.wd);
		const float pixel_size_y = 2.f / Renderer::EFBToScaledXf(2.f * xfmem.viewport.ht);
		constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x;
		constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y;
		dirty = true;
		// This is so implementation-dependent that we can't have it here.
		g_renderer->SetViewport();

		// Update projection if the viewport isn't 1:1 useable
		if (!g_ActiveConfig.backend_info.bSupportsOversizedViewports)
		{
			ViewportCorrectionMatrix(s_viewportCorrection);
			bProjectionChanged = true;
		}
	}

	if (bProjectionChanged)
	{
		bProjectionChanged = false;

		float *rawProjection = xfmem.projection.rawProjection;

		switch (xfmem.projection.type)
		{
		case GX_PERSPECTIVE:

			g_fProjectionMatrix[0] = rawProjection[0] * g_ActiveConfig.fAspectRatioHackW;
			g_fProjectionMatrix[1] = 0.0f;
			g_fProjectionMatrix[2] = rawProjection[1];
			g_fProjectionMatrix[3] = 0.0f;

			g_fProjectionMatrix[4] = 0.0f;
			g_fProjectionMatrix[5] = rawProjection[2] * g_ActiveConfig.fAspectRatioHackH;
			g_fProjectionMatrix[6] = rawProjection[3];
			g_fProjectionMatrix[7] = 0.0f;

			g_fProjectionMatrix[8] = 0.0f;
			g_fProjectionMatrix[9] = 0.0f;
			g_fProjectionMatrix[10] = rawProjection[4];

			g_fProjectionMatrix[11] = rawProjection[5];

			g_fProjectionMatrix[12] = 0.0f;
			g_fProjectionMatrix[13] = 0.0f;
			// donkopunchstania suggested the GC GPU might round differently
			// He had thus changed this to -(1 + epsilon) to fix clipping issues.
			// I (neobrain) don't think his conjecture is true and thus reverted his change.
			g_fProjectionMatrix[14] = -1.0f;
			g_fProjectionMatrix[15] = 0.0f;

			SETSTAT_FT(stats.gproj_0, g_fProjectionMatrix[0]);
			SETSTAT_FT(stats.gproj_1, g_fProjectionMatrix[1]);
			SETSTAT_FT(stats.gproj_2, g_fProjectionMatrix[2]);
			SETSTAT_FT(stats.gproj_3, g_fProjectionMatrix[3]);
			SETSTAT_FT(stats.gproj_4, g_fProjectionMatrix[4]);
			SETSTAT_FT(stats.gproj_5, g_fProjectionMatrix[5]);
			SETSTAT_FT(stats.gproj_6, g_fProjectionMatrix[6]);
			SETSTAT_FT(stats.gproj_7, g_fProjectionMatrix[7]);
			SETSTAT_FT(stats.gproj_8, g_fProjectionMatrix[8]);
			SETSTAT_FT(stats.gproj_9, g_fProjectionMatrix[9]);
			SETSTAT_FT(stats.gproj_10, g_fProjectionMatrix[10]);
			SETSTAT_FT(stats.gproj_11, g_fProjectionMatrix[11]);
			SETSTAT_FT(stats.gproj_12, g_fProjectionMatrix[12]);
			SETSTAT_FT(stats.gproj_13, g_fProjectionMatrix[13]);
			SETSTAT_FT(stats.gproj_14, g_fProjectionMatrix[14]);
			SETSTAT_FT(stats.gproj_15, g_fProjectionMatrix[15]);
			break;

		case GX_ORTHOGRAPHIC:

			g_fProjectionMatrix[0] = rawProjection[0];
			g_fProjectionMatrix[1] = 0.0f;
			g_fProjectionMatrix[2] = 0.0f;
			g_fProjectionMatrix[3] = rawProjection[1];

			g_fProjectionMatrix[4] = 0.0f;
			g_fProjectionMatrix[5] = rawProjection[2];
			g_fProjectionMatrix[6] = 0.0f;
			g_fProjectionMatrix[7] = rawProjection[3];

			g_fProjectionMatrix[8] = 0.0f;
			g_fProjectionMatrix[9] = 0.0f;
			g_fProjectionMatrix[10] = (g_ProjHack1.value + rawProjection[4]) * ((g_ProjHack1.sign == 0) ? 1.0f : g_ProjHack1.sign);
			g_fProjectionMatrix[11] = (g_ProjHack2.value + rawProjection[5]) * ((g_ProjHack2.sign == 0) ? 1.0f : g_ProjHack2.sign);

			g_fProjectionMatrix[12] = 0.0f;
			g_fProjectionMatrix[13] = 0.0f;

			g_fProjectionMatrix[14] = 0.0f;
			g_fProjectionMatrix[15] = 1.0f + FLT_EPSILON; // hack to fix depth clipping precision issues (such as Sonic Unleashed UI)

			SETSTAT_FT(stats.g2proj_0, g_fProjectionMatrix[0]);
			SETSTAT_FT(stats.g2proj_1, g_fProjectionMatrix[1]);
			SETSTAT_FT(stats.g2proj_2, g_fProjectionMatrix[2]);
			SETSTAT_FT(stats.g2proj_3, g_fProjectionMatrix[3]);
			SETSTAT_FT(stats.g2proj_4, g_fProjectionMatrix[4]);
			SETSTAT_FT(stats.g2proj_5, g_fProjectionMatrix[5]);
			SETSTAT_FT(stats.g2proj_6, g_fProjectionMatrix[6]);
			SETSTAT_FT(stats.g2proj_7, g_fProjectionMatrix[7]);
			SETSTAT_FT(stats.g2proj_8, g_fProjectionMatrix[8]);
			SETSTAT_FT(stats.g2proj_9, g_fProjectionMatrix[9]);
			SETSTAT_FT(stats.g2proj_10, g_fProjectionMatrix[10]);
			SETSTAT_FT(stats.g2proj_11, g_fProjectionMatrix[11]);
			SETSTAT_FT(stats.g2proj_12, g_fProjectionMatrix[12]);
			SETSTAT_FT(stats.g2proj_13, g_fProjectionMatrix[13]);
			SETSTAT_FT(stats.g2proj_14, g_fProjectionMatrix[14]);
			SETSTAT_FT(stats.g2proj_15, g_fProjectionMatrix[15]);
			SETSTAT_FT(stats.proj_0, rawProjection[0]);
			SETSTAT_FT(stats.proj_1, rawProjection[1]);
			SETSTAT_FT(stats.proj_2, rawProjection[2]);
			SETSTAT_FT(stats.proj_3, rawProjection[3]);
			SETSTAT_FT(stats.proj_4, rawProjection[4]);
			SETSTAT_FT(stats.proj_5, rawProjection[5]);
			break;

		default:
			ERROR_LOG(VIDEO, "Unknown projection type: %d", xfmem.projection.type);
		}

		PRIM_LOG("Projection: %f %f %f %f %f %f\n", rawProjection[0], rawProjection[1], rawProjection[2], rawProjection[3], rawProjection[4], rawProjection[5]);

		if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE)
		{
			Matrix44 mtxA;
			Matrix44 mtxB;
			Matrix44 viewMtx;

			Matrix44::Translate(mtxA, s_fViewTranslationVector);
			Matrix44::LoadMatrix33(mtxB, s_viewRotationMatrix);
			Matrix44::Multiply(mtxB, mtxA, viewMtx); // view = rotation x translation
			Matrix44::Set(mtxB, g_fProjectionMatrix);
			Matrix44::Multiply(mtxB, viewMtx, mtxA); // mtxA = projection x view
			Matrix44::Multiply(s_viewportCorrection, mtxA, mtxB); // mtxB = viewportCorrection x mtxA
			memcpy(constants.projection, mtxB.data, 4*16);
		}
		else
		{
			Matrix44 projMtx;
			Matrix44::Set(projMtx, g_fProjectionMatrix);

			Matrix44 correctedMtx;
			Matrix44::Multiply(s_viewportCorrection, projMtx, correctedMtx);
			memcpy(constants.projection, correctedMtx.data, 4*16);
		}

		dirty = true;
	}
}
Example #10
0
static void LOADERDECL PosMtx_ReadDirect_UByte(VertexLoader* loader)
{
	u8 posmtx = BoundingBox::posMtxIdx = DataReadU8() & 0x3f;
	DataWrite<u32>(posmtx);
	PRIM_LOG("posmtx: %d, ", posmtx);
}
Example #11
0
// Syncs the shader constant buffers with xfmem
// TODO: A cleaner way to control the matrices without making a mess in the parameters field
void VertexShaderManager::SetConstants()
{
  if (nTransformMatricesChanged[0] >= 0)
  {
    int startn = nTransformMatricesChanged[0] / 4;
    int endn = (nTransformMatricesChanged[1] + 3) / 4;
    memcpy(constants.transformmatrices[startn].data(), &xfmem.posMatrices[startn * 4],
           (endn - startn) * sizeof(float4));
    dirty = true;
    nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1;
  }

  if (nNormalMatricesChanged[0] >= 0)
  {
    int startn = nNormalMatricesChanged[0] / 3;
    int endn = (nNormalMatricesChanged[1] + 2) / 3;
    for (int i = startn; i < endn; i++)
    {
      memcpy(constants.normalmatrices[i].data(), &xfmem.normalMatrices[3 * i], 12);
    }
    dirty = true;
    nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
  }

  if (nPostTransformMatricesChanged[0] >= 0)
  {
    int startn = nPostTransformMatricesChanged[0] / 4;
    int endn = (nPostTransformMatricesChanged[1] + 3) / 4;
    memcpy(constants.posttransformmatrices[startn].data(), &xfmem.postMatrices[startn * 4],
           (endn - startn) * sizeof(float4));
    dirty = true;
    nPostTransformMatricesChanged[0] = nPostTransformMatricesChanged[1] = -1;
  }

  if (nLightsChanged[0] >= 0)
  {
    // TODO: Outdated comment
    // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats
    int istart = nLightsChanged[0] / 0x10;
    int iend = (nLightsChanged[1] + 15) / 0x10;

    for (int i = istart; i < iend; ++i)
    {
      const Light& light = xfmem.lights[i];
      VertexShaderConstants::Light& dstlight = constants.lights[i];

      // xfmem.light.color is packed as abgr in u8[4], so we have to swap the order
      dstlight.color[0] = light.color[3];
      dstlight.color[1] = light.color[2];
      dstlight.color[2] = light.color[1];
      dstlight.color[3] = light.color[0];

      dstlight.cosatt[0] = light.cosatt[0];
      dstlight.cosatt[1] = light.cosatt[1];
      dstlight.cosatt[2] = light.cosatt[2];

      if (fabs(light.distatt[0]) < 0.00001f && fabs(light.distatt[1]) < 0.00001f &&
          fabs(light.distatt[2]) < 0.00001f)
      {
        // dist attenuation, make sure not equal to 0!!!
        dstlight.distatt[0] = .00001f;
      }
      else
      {
        dstlight.distatt[0] = light.distatt[0];
      }
      dstlight.distatt[1] = light.distatt[1];
      dstlight.distatt[2] = light.distatt[2];

      dstlight.pos[0] = light.dpos[0];
      dstlight.pos[1] = light.dpos[1];
      dstlight.pos[2] = light.dpos[2];

      double norm = double(light.ddir[0]) * double(light.ddir[0]) +
                    double(light.ddir[1]) * double(light.ddir[1]) +
                    double(light.ddir[2]) * double(light.ddir[2]);
      norm = 1.0 / sqrt(norm);
      float norm_float = static_cast<float>(norm);
      dstlight.dir[0] = light.ddir[0] * norm_float;
      dstlight.dir[1] = light.ddir[1] * norm_float;
      dstlight.dir[2] = light.ddir[2] * norm_float;
    }
    dirty = true;

    nLightsChanged[0] = nLightsChanged[1] = -1;
  }

  for (int i : nMaterialsChanged)
  {
    u32 data = i >= 2 ? xfmem.matColor[i - 2] : xfmem.ambColor[i];
    constants.materials[i][0] = (data >> 24) & 0xFF;
    constants.materials[i][1] = (data >> 16) & 0xFF;
    constants.materials[i][2] = (data >> 8) & 0xFF;
    constants.materials[i][3] = data & 0xFF;
    dirty = true;
  }
  nMaterialsChanged = BitSet32(0);

  if (bPosNormalMatrixChanged)
  {
    bPosNormalMatrixChanged = false;

    const float* pos = &xfmem.posMatrices[g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4];
    const float* norm =
        &xfmem.normalMatrices[3 * (g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31)];

    memcpy(constants.posnormalmatrix.data(), pos, 3 * sizeof(float4));
    memcpy(constants.posnormalmatrix[3].data(), norm, 3 * sizeof(float));
    memcpy(constants.posnormalmatrix[4].data(), norm + 3, 3 * sizeof(float));
    memcpy(constants.posnormalmatrix[5].data(), norm + 6, 3 * sizeof(float));
    dirty = true;
  }

  if (bTexMatricesChanged[0])
  {
    bTexMatricesChanged[0] = false;
    const float* pos_matrix_ptrs[] = {
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4]};

    for (size_t i = 0; i < ArraySize(pos_matrix_ptrs); ++i)
    {
      memcpy(constants.texmatrices[3 * i].data(), pos_matrix_ptrs[i], 3 * sizeof(float4));
    }
    dirty = true;
  }

  if (bTexMatricesChanged[1])
  {
    bTexMatricesChanged[1] = false;
    const float* pos_matrix_ptrs[] = {
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4]};

    for (size_t i = 0; i < ArraySize(pos_matrix_ptrs); ++i)
    {
      memcpy(constants.texmatrices[3 * i + 12].data(), pos_matrix_ptrs[i], 3 * sizeof(float4));
    }
    dirty = true;
  }

  if (bViewportChanged)
  {
    bViewportChanged = false;

    // The console GPU places the pixel center at 7/12 unless antialiasing
    // is enabled, while D3D and OpenGL place it at 0.5. See the comment
    // in VertexShaderGen.cpp for details.
    // NOTE: If we ever emulate antialiasing, the sample locations set by
    // BP registers 0x01-0x04 need to be considered here.
    const float pixel_center_correction = 7.0f / 12.0f - 0.5f;
    const bool bUseVertexRounding = g_ActiveConfig.bVertexRounding && g_ActiveConfig.iEFBScale != 1;
    const float viewport_width = bUseVertexRounding ?
                                     (2.f * xfmem.viewport.wd) :
                                     g_renderer->EFBToScaledXf(2.f * xfmem.viewport.wd);
    const float viewport_height = bUseVertexRounding ?
                                      (2.f * xfmem.viewport.ht) :
                                      g_renderer->EFBToScaledXf(2.f * xfmem.viewport.ht);
    const float pixel_size_x = 2.f / viewport_width;
    const float pixel_size_y = 2.f / viewport_height;
    constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x;
    constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y;

    // By default we don't change the depth value at all in the vertex shader.
    constants.pixelcentercorrection[2] = 1.0f;
    constants.pixelcentercorrection[3] = 0.0f;

    constants.viewport[0] = (2.f * xfmem.viewport.wd);
    constants.viewport[1] = (2.f * xfmem.viewport.ht);

    if (g_renderer->UseVertexDepthRange())
    {
      // Oversized depth ranges are handled in the vertex shader. We need to reverse
      // the far value to use the reversed-Z trick.
      if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
      {
        // Sometimes the console also tries to use the reversed-Z trick. We can only do
        // that with the expected accuracy if the backend can reverse the depth range.
        constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f;
        if (xfmem.viewport.zRange < 0.0f)
          constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f;
        else
          constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
      }
      else
      {
        // For backends that don't support reversing the depth range we can still render
        // cases where the console uses the reversed-Z trick. But we simply can't provide
        // the expected accuracy, which might result in z-fighting.
        constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f;
        constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
      }
    }

    dirty = true;
    BPFunctions::SetViewport();

    // Update projection if the viewport isn't 1:1 useable
    if (!g_ActiveConfig.backend_info.bSupportsOversizedViewports)
    {
      ViewportCorrectionMatrix(s_viewportCorrection);
      bProjectionChanged = true;
    }
  }

  if (bProjectionChanged)
  {
    bProjectionChanged = false;

    float* rawProjection = xfmem.projection.rawProjection;

    switch (xfmem.projection.type)
    {
    case GX_PERSPECTIVE:

      g_fProjectionMatrix[0] = rawProjection[0] * g_ActiveConfig.fAspectRatioHackW;
      g_fProjectionMatrix[1] = 0.0f;
      g_fProjectionMatrix[2] = rawProjection[1] * g_ActiveConfig.fAspectRatioHackW;
      g_fProjectionMatrix[3] = 0.0f;

      g_fProjectionMatrix[4] = 0.0f;
      g_fProjectionMatrix[5] = rawProjection[2] * g_ActiveConfig.fAspectRatioHackH;
      g_fProjectionMatrix[6] = rawProjection[3] * g_ActiveConfig.fAspectRatioHackH;
      g_fProjectionMatrix[7] = 0.0f;

      g_fProjectionMatrix[8] = 0.0f;
      g_fProjectionMatrix[9] = 0.0f;
      g_fProjectionMatrix[10] = rawProjection[4];
      g_fProjectionMatrix[11] = rawProjection[5];

      g_fProjectionMatrix[12] = 0.0f;
      g_fProjectionMatrix[13] = 0.0f;

      g_fProjectionMatrix[14] = -1.0f;
      g_fProjectionMatrix[15] = 0.0f;

      SETSTAT_FT(stats.gproj_0, g_fProjectionMatrix[0]);
      SETSTAT_FT(stats.gproj_1, g_fProjectionMatrix[1]);
      SETSTAT_FT(stats.gproj_2, g_fProjectionMatrix[2]);
      SETSTAT_FT(stats.gproj_3, g_fProjectionMatrix[3]);
      SETSTAT_FT(stats.gproj_4, g_fProjectionMatrix[4]);
      SETSTAT_FT(stats.gproj_5, g_fProjectionMatrix[5]);
      SETSTAT_FT(stats.gproj_6, g_fProjectionMatrix[6]);
      SETSTAT_FT(stats.gproj_7, g_fProjectionMatrix[7]);
      SETSTAT_FT(stats.gproj_8, g_fProjectionMatrix[8]);
      SETSTAT_FT(stats.gproj_9, g_fProjectionMatrix[9]);
      SETSTAT_FT(stats.gproj_10, g_fProjectionMatrix[10]);
      SETSTAT_FT(stats.gproj_11, g_fProjectionMatrix[11]);
      SETSTAT_FT(stats.gproj_12, g_fProjectionMatrix[12]);
      SETSTAT_FT(stats.gproj_13, g_fProjectionMatrix[13]);
      SETSTAT_FT(stats.gproj_14, g_fProjectionMatrix[14]);
      SETSTAT_FT(stats.gproj_15, g_fProjectionMatrix[15]);
      break;

    case GX_ORTHOGRAPHIC:

      g_fProjectionMatrix[0] = rawProjection[0];
      g_fProjectionMatrix[1] = 0.0f;
      g_fProjectionMatrix[2] = 0.0f;
      g_fProjectionMatrix[3] = rawProjection[1];

      g_fProjectionMatrix[4] = 0.0f;
      g_fProjectionMatrix[5] = rawProjection[2];
      g_fProjectionMatrix[6] = 0.0f;
      g_fProjectionMatrix[7] = rawProjection[3];

      g_fProjectionMatrix[8] = 0.0f;
      g_fProjectionMatrix[9] = 0.0f;
      g_fProjectionMatrix[10] = rawProjection[4];
      g_fProjectionMatrix[11] = rawProjection[5];

      g_fProjectionMatrix[12] = 0.0f;
      g_fProjectionMatrix[13] = 0.0f;

      g_fProjectionMatrix[14] = 0.0f;
      g_fProjectionMatrix[15] = 1.0f;

      SETSTAT_FT(stats.g2proj_0, g_fProjectionMatrix[0]);
      SETSTAT_FT(stats.g2proj_1, g_fProjectionMatrix[1]);
      SETSTAT_FT(stats.g2proj_2, g_fProjectionMatrix[2]);
      SETSTAT_FT(stats.g2proj_3, g_fProjectionMatrix[3]);
      SETSTAT_FT(stats.g2proj_4, g_fProjectionMatrix[4]);
      SETSTAT_FT(stats.g2proj_5, g_fProjectionMatrix[5]);
      SETSTAT_FT(stats.g2proj_6, g_fProjectionMatrix[6]);
      SETSTAT_FT(stats.g2proj_7, g_fProjectionMatrix[7]);
      SETSTAT_FT(stats.g2proj_8, g_fProjectionMatrix[8]);
      SETSTAT_FT(stats.g2proj_9, g_fProjectionMatrix[9]);
      SETSTAT_FT(stats.g2proj_10, g_fProjectionMatrix[10]);
      SETSTAT_FT(stats.g2proj_11, g_fProjectionMatrix[11]);
      SETSTAT_FT(stats.g2proj_12, g_fProjectionMatrix[12]);
      SETSTAT_FT(stats.g2proj_13, g_fProjectionMatrix[13]);
      SETSTAT_FT(stats.g2proj_14, g_fProjectionMatrix[14]);
      SETSTAT_FT(stats.g2proj_15, g_fProjectionMatrix[15]);
      SETSTAT_FT(stats.proj_0, rawProjection[0]);
      SETSTAT_FT(stats.proj_1, rawProjection[1]);
      SETSTAT_FT(stats.proj_2, rawProjection[2]);
      SETSTAT_FT(stats.proj_3, rawProjection[3]);
      SETSTAT_FT(stats.proj_4, rawProjection[4]);
      SETSTAT_FT(stats.proj_5, rawProjection[5]);
      break;

    default:
      ERROR_LOG(VIDEO, "Unknown projection type: %d", xfmem.projection.type);
    }

    PRIM_LOG("Projection: %f %f %f %f %f %f", rawProjection[0], rawProjection[1], rawProjection[2],
             rawProjection[3], rawProjection[4], rawProjection[5]);

    if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE)
    {
      Matrix44 mtxA;
      Matrix44 mtxB;
      Matrix44 viewMtx;

      Matrix44::Translate(mtxA, s_fViewTranslationVector);
      Matrix44::LoadMatrix33(mtxB, s_viewRotationMatrix);
      Matrix44::Multiply(mtxB, mtxA, viewMtx);  // view = rotation x translation
      Matrix44::Set(mtxB, g_fProjectionMatrix);
      Matrix44::Multiply(mtxB, viewMtx, mtxA);               // mtxA = projection x view
      Matrix44::Multiply(s_viewportCorrection, mtxA, mtxB);  // mtxB = viewportCorrection x mtxA
      memcpy(constants.projection.data(), mtxB.data, 4 * sizeof(float4));
    }
    else
    {
      Matrix44 projMtx;
      Matrix44::Set(projMtx, g_fProjectionMatrix);

      Matrix44 correctedMtx;
      Matrix44::Multiply(s_viewportCorrection, projMtx, correctedMtx);
      memcpy(constants.projection.data(), correctedMtx.data, 4 * sizeof(float4));
    }

    dirty = true;
  }

  if (bTexMtxInfoChanged)
  {
    bTexMtxInfoChanged = false;
    constants.xfmem_dualTexInfo = xfmem.dualTexTrans.enabled;
    for (size_t i = 0; i < ArraySize(xfmem.texMtxInfo); i++)
      constants.xfmem_pack1[i][0] = xfmem.texMtxInfo[i].hex;
    for (size_t i = 0; i < ArraySize(xfmem.postMtxInfo); i++)
      constants.xfmem_pack1[i][1] = xfmem.postMtxInfo[i].hex;

    dirty = true;
  }

  if (bLightingConfigChanged)
  {
    bLightingConfigChanged = false;

    for (size_t i = 0; i < 2; i++)
    {
      constants.xfmem_pack1[i][2] = xfmem.color[i].hex;
      constants.xfmem_pack1[i][3] = xfmem.alpha[i].hex;
    }
    constants.xfmem_numColorChans = xfmem.numChan.numColorChans;

    dirty = true;
  }
}
Example #12
0
// Syncs the shader constant buffers with xfmem
// TODO: A cleaner way to control the matrices without making a mess in the parameters field
void VertexShaderManager::SetConstants()
{
  if (nTransformMatricesChanged[0] >= 0)
  {
    int startn = nTransformMatricesChanged[0] / 4;
    int endn = (nTransformMatricesChanged[1] + 3) / 4;
    memcpy(constants.transformmatrices[startn], &xfmem.posMatrices[startn * 4],
           (endn - startn) * sizeof(float4));
    dirty = true;
    nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1;
  }

  if (nNormalMatricesChanged[0] >= 0)
  {
    int startn = nNormalMatricesChanged[0] / 3;
    int endn = (nNormalMatricesChanged[1] + 2) / 3;
    for (int i = startn; i < endn; i++)
    {
      memcpy(constants.normalmatrices[i], &xfmem.normalMatrices[3 * i], 12);
    }
    dirty = true;
    nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
  }

  if (nPostTransformMatricesChanged[0] >= 0)
  {
    int startn = nPostTransformMatricesChanged[0] / 4;
    int endn = (nPostTransformMatricesChanged[1] + 3) / 4;
    memcpy(constants.posttransformmatrices[startn], &xfmem.postMatrices[startn * 4],
           (endn - startn) * sizeof(float4));
    dirty = true;
    nPostTransformMatricesChanged[0] = nPostTransformMatricesChanged[1] = -1;
  }

  if (nLightsChanged[0] >= 0)
  {
    // TODO: Outdated comment
    // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats
    int istart = nLightsChanged[0] / 0x10;
    int iend = (nLightsChanged[1] + 15) / 0x10;

    for (int i = istart; i < iend; ++i)
    {
      const Light& light = xfmem.lights[i];
      VertexShaderConstants::Light& dstlight = constants.lights[i];

      // xfmem.light.color is packed as abgr in u8[4], so we have to swap the order
      dstlight.color[0] = light.color[3];
      dstlight.color[1] = light.color[2];
      dstlight.color[2] = light.color[1];
      dstlight.color[3] = light.color[0];

      dstlight.cosatt[0] = light.cosatt[0];
      dstlight.cosatt[1] = light.cosatt[1];
      dstlight.cosatt[2] = light.cosatt[2];

      if (fabs(light.distatt[0]) < 0.00001f && fabs(light.distatt[1]) < 0.00001f &&
          fabs(light.distatt[2]) < 0.00001f)
      {
        // dist attenuation, make sure not equal to 0!!!
        dstlight.distatt[0] = .00001f;
      }
      else
      {
        dstlight.distatt[0] = light.distatt[0];
      }
      dstlight.distatt[1] = light.distatt[1];
      dstlight.distatt[2] = light.distatt[2];

      dstlight.pos[0] = light.dpos[0];
      dstlight.pos[1] = light.dpos[1];
      dstlight.pos[2] = light.dpos[2];

      double norm = double(light.ddir[0]) * double(light.ddir[0]) +
                    double(light.ddir[1]) * double(light.ddir[1]) +
                    double(light.ddir[2]) * double(light.ddir[2]);
      norm = 1.0 / sqrt(norm);
      float norm_float = static_cast<float>(norm);
      dstlight.dir[0] = light.ddir[0] * norm_float;
      dstlight.dir[1] = light.ddir[1] * norm_float;
      dstlight.dir[2] = light.ddir[2] * norm_float;
    }
    dirty = true;

    nLightsChanged[0] = nLightsChanged[1] = -1;
  }

  for (int i : nMaterialsChanged)
  {
    u32 data = i >= 2 ? xfmem.matColor[i - 2] : xfmem.ambColor[i];
    constants.materials[i][0] = (data >> 24) & 0xFF;
    constants.materials[i][1] = (data >> 16) & 0xFF;
    constants.materials[i][2] = (data >> 8) & 0xFF;
    constants.materials[i][3] = data & 0xFF;
    dirty = true;
  }
  nMaterialsChanged = BitSet32(0);

  if (bPosNormalMatrixChanged)
  {
    bPosNormalMatrixChanged = false;

    const float* pos = &xfmem.posMatrices[g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4];
    const float* norm =
        &xfmem.normalMatrices[3 * (g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31)];

    memcpy(constants.posnormalmatrix, pos, 3 * sizeof(float4));
    memcpy(constants.posnormalmatrix[3], norm, 3 * sizeof(float));
    memcpy(constants.posnormalmatrix[4], norm + 3, 3 * sizeof(float));
    memcpy(constants.posnormalmatrix[5], norm + 6, 3 * sizeof(float));
    dirty = true;
  }

  if (bTexMatricesChanged[0])
  {
    bTexMatricesChanged[0] = false;
    const float* pos_matrix_ptrs[] = {
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4]};

    for (size_t i = 0; i < ArraySize(pos_matrix_ptrs); ++i)
    {
      memcpy(constants.texmatrices[3 * i], pos_matrix_ptrs[i], 3 * sizeof(float4));
    }
    dirty = true;
  }

  if (bTexMatricesChanged[1])
  {
    bTexMatricesChanged[1] = false;
    const float* pos_matrix_ptrs[] = {
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4],
        &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4]};

    for (size_t i = 0; i < ArraySize(pos_matrix_ptrs); ++i)
    {
      memcpy(constants.texmatrices[3 * i + 12], pos_matrix_ptrs[i], 3 * sizeof(float4));
    }
    dirty = true;
  }

  if (bViewportChanged)
  {
    bViewportChanged = false;

    // The console GPU places the pixel center at 7/12 unless antialiasing
    // is enabled, while D3D and OpenGL place it at 0.5. See the comment
    // in VertexShaderGen.cpp for details.
    // NOTE: If we ever emulate antialiasing, the sample locations set by
    // BP registers 0x01-0x04 need to be considered here.
    const float pixel_center_correction = 7.0f / 12.0f - 0.5f;
    const float pixel_size_x = 2.f / Renderer::EFBToScaledXf(2.f * xfmem.viewport.wd);
    const float pixel_size_y = 2.f / Renderer::EFBToScaledXf(2.f * xfmem.viewport.ht);
    constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x;
    constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y;

    // By default we don't change the depth value at all in the vertex shader.
    constants.pixelcentercorrection[2] = 1.0f;
    constants.pixelcentercorrection[3] = 0.0f;

    if (g_ActiveConfig.backend_info.bSupportsDepthClamp)
    {
      // Oversized depth ranges are handled in the vertex shader. We need to reverse
      // the far value to get a reversed depth range mapping. This is necessary
      // because the standard depth range equation pushes all depth values towards
      // the back of the depth buffer where conventionally depth buffers have the
      // least precision.
      if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
      {
        if (fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f)
        {
          // For backends that support reversing the depth range we also support cases
          // where the console also uses reversed depth with the same accuracy. We need
          // to make sure the depth range is positive here and then reverse the depth in
          // the backend viewport.
          constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f;
          if (xfmem.viewport.zRange < 0.0f)
            constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f;
          else
            constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
        }
      }
      else
      {
        if (xfmem.viewport.zRange < 0.0f || xfmem.viewport.zRange > 16777215.0f ||
            fabs(xfmem.viewport.farZ) > 16777215.0f)
        {
          // For backends that don't support reversing the depth range we can still render
          // cases where the console uses reversed depth correctly. But we simply can't
          // provide the same accuracy as the console.
          constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f;
          constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
        }
      }
    }

    dirty = true;
    // This is so implementation-dependent that we can't have it here.
    g_renderer->SetViewport();

    // Update projection if the viewport isn't 1:1 useable
    if (!g_ActiveConfig.backend_info.bSupportsOversizedViewports)
    {
      ViewportCorrectionMatrix(s_viewportCorrection);
      bProjectionChanged = true;
    }
  }

  if (bProjectionChanged)
  {
    bProjectionChanged = false;

    float* rawProjection = xfmem.projection.rawProjection;

    switch (xfmem.projection.type)
    {
    case GX_PERSPECTIVE:

      g_fProjectionMatrix[0] = rawProjection[0] * g_ActiveConfig.fAspectRatioHackW;
      g_fProjectionMatrix[1] = 0.0f;
      g_fProjectionMatrix[2] = rawProjection[1];
      g_fProjectionMatrix[3] = 0.0f;

      g_fProjectionMatrix[4] = 0.0f;
      g_fProjectionMatrix[5] = rawProjection[2] * g_ActiveConfig.fAspectRatioHackH;
      g_fProjectionMatrix[6] = rawProjection[3];
      g_fProjectionMatrix[7] = 0.0f;

      g_fProjectionMatrix[8] = 0.0f;
      g_fProjectionMatrix[9] = 0.0f;
      g_fProjectionMatrix[10] = rawProjection[4];

      g_fProjectionMatrix[11] = rawProjection[5];

      g_fProjectionMatrix[12] = 0.0f;
      g_fProjectionMatrix[13] = 0.0f;

      g_fProjectionMatrix[14] = -1.0f;
      g_fProjectionMatrix[15] = 0.0f;

      // Heuristic to detect if a GameCube game is in 16:9 anamorphic widescreen mode.
      if (!SConfig::GetInstance().bWii)
      {
        bool viewport_is_4_3 = AspectIs4_3(xfmem.viewport.wd, xfmem.viewport.ht);
        if (AspectIs16_9(rawProjection[2], rawProjection[0]) && viewport_is_4_3)
          Core::g_aspect_wide = true;  // Projection is 16:9 and viewport is 4:3, we are rendering
                                       // an anamorphic widescreen picture
        else if (AspectIs4_3(rawProjection[2], rawProjection[0]) && viewport_is_4_3)
          Core::g_aspect_wide =
              false;  // Project and viewports are both 4:3, we are rendering a normal image.
      }

      SETSTAT_FT(stats.gproj_0, g_fProjectionMatrix[0]);
      SETSTAT_FT(stats.gproj_1, g_fProjectionMatrix[1]);
      SETSTAT_FT(stats.gproj_2, g_fProjectionMatrix[2]);
      SETSTAT_FT(stats.gproj_3, g_fProjectionMatrix[3]);
      SETSTAT_FT(stats.gproj_4, g_fProjectionMatrix[4]);
      SETSTAT_FT(stats.gproj_5, g_fProjectionMatrix[5]);
      SETSTAT_FT(stats.gproj_6, g_fProjectionMatrix[6]);
      SETSTAT_FT(stats.gproj_7, g_fProjectionMatrix[7]);
      SETSTAT_FT(stats.gproj_8, g_fProjectionMatrix[8]);
      SETSTAT_FT(stats.gproj_9, g_fProjectionMatrix[9]);
      SETSTAT_FT(stats.gproj_10, g_fProjectionMatrix[10]);
      SETSTAT_FT(stats.gproj_11, g_fProjectionMatrix[11]);
      SETSTAT_FT(stats.gproj_12, g_fProjectionMatrix[12]);
      SETSTAT_FT(stats.gproj_13, g_fProjectionMatrix[13]);
      SETSTAT_FT(stats.gproj_14, g_fProjectionMatrix[14]);
      SETSTAT_FT(stats.gproj_15, g_fProjectionMatrix[15]);
      break;

    case GX_ORTHOGRAPHIC:

      g_fProjectionMatrix[0] = rawProjection[0];
      g_fProjectionMatrix[1] = 0.0f;
      g_fProjectionMatrix[2] = 0.0f;
      g_fProjectionMatrix[3] = rawProjection[1];

      g_fProjectionMatrix[4] = 0.0f;
      g_fProjectionMatrix[5] = rawProjection[2];
      g_fProjectionMatrix[6] = 0.0f;
      g_fProjectionMatrix[7] = rawProjection[3];

      g_fProjectionMatrix[8] = 0.0f;
      g_fProjectionMatrix[9] = 0.0f;
      g_fProjectionMatrix[10] = (g_ProjHack1.value + rawProjection[4]) *
                                ((g_ProjHack1.sign == 0) ? 1.0f : g_ProjHack1.sign);
      g_fProjectionMatrix[11] = (g_ProjHack2.value + rawProjection[5]) *
                                ((g_ProjHack2.sign == 0) ? 1.0f : g_ProjHack2.sign);

      g_fProjectionMatrix[12] = 0.0f;
      g_fProjectionMatrix[13] = 0.0f;

      g_fProjectionMatrix[14] = 0.0f;
      g_fProjectionMatrix[15] = 1.0f;

      SETSTAT_FT(stats.g2proj_0, g_fProjectionMatrix[0]);
      SETSTAT_FT(stats.g2proj_1, g_fProjectionMatrix[1]);
      SETSTAT_FT(stats.g2proj_2, g_fProjectionMatrix[2]);
      SETSTAT_FT(stats.g2proj_3, g_fProjectionMatrix[3]);
      SETSTAT_FT(stats.g2proj_4, g_fProjectionMatrix[4]);
      SETSTAT_FT(stats.g2proj_5, g_fProjectionMatrix[5]);
      SETSTAT_FT(stats.g2proj_6, g_fProjectionMatrix[6]);
      SETSTAT_FT(stats.g2proj_7, g_fProjectionMatrix[7]);
      SETSTAT_FT(stats.g2proj_8, g_fProjectionMatrix[8]);
      SETSTAT_FT(stats.g2proj_9, g_fProjectionMatrix[9]);
      SETSTAT_FT(stats.g2proj_10, g_fProjectionMatrix[10]);
      SETSTAT_FT(stats.g2proj_11, g_fProjectionMatrix[11]);
      SETSTAT_FT(stats.g2proj_12, g_fProjectionMatrix[12]);
      SETSTAT_FT(stats.g2proj_13, g_fProjectionMatrix[13]);
      SETSTAT_FT(stats.g2proj_14, g_fProjectionMatrix[14]);
      SETSTAT_FT(stats.g2proj_15, g_fProjectionMatrix[15]);
      SETSTAT_FT(stats.proj_0, rawProjection[0]);
      SETSTAT_FT(stats.proj_1, rawProjection[1]);
      SETSTAT_FT(stats.proj_2, rawProjection[2]);
      SETSTAT_FT(stats.proj_3, rawProjection[3]);
      SETSTAT_FT(stats.proj_4, rawProjection[4]);
      SETSTAT_FT(stats.proj_5, rawProjection[5]);
      break;

    default:
      ERROR_LOG(VIDEO, "Unknown projection type: %d", xfmem.projection.type);
    }

    PRIM_LOG("Projection: %f %f %f %f %f %f", rawProjection[0], rawProjection[1], rawProjection[2],
             rawProjection[3], rawProjection[4], rawProjection[5]);

    if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE)
    {
      Matrix44 mtxA;
      Matrix44 mtxB;
      Matrix44 viewMtx;

      Matrix44::Translate(mtxA, s_fViewTranslationVector);
      Matrix44::LoadMatrix33(mtxB, s_viewRotationMatrix);
      Matrix44::Multiply(mtxB, mtxA, viewMtx);  // view = rotation x translation
      Matrix44::Set(mtxB, g_fProjectionMatrix);
      Matrix44::Multiply(mtxB, viewMtx, mtxA);               // mtxA = projection x view
      Matrix44::Multiply(s_viewportCorrection, mtxA, mtxB);  // mtxB = viewportCorrection x mtxA
      memcpy(constants.projection, mtxB.data, 4 * sizeof(float4));
    }
    else
    {
      Matrix44 projMtx;
      Matrix44::Set(projMtx, g_fProjectionMatrix);

      Matrix44 correctedMtx;
      Matrix44::Multiply(s_viewportCorrection, projMtx, correctedMtx);
      memcpy(constants.projection, correctedMtx.data, 4 * sizeof(float4));
    }

    dirty = true;
  }
}