diff options
Diffstat (limited to 'Ryujinx.Graphics/Graphics3d')
22 files changed, 3078 insertions, 3083 deletions
diff --git a/Ryujinx.Graphics/Graphics3d/INvGpuEngine.cs b/Ryujinx.Graphics/Graphics3d/INvGpuEngine.cs index c2474a17..aa0db682 100644 --- a/Ryujinx.Graphics/Graphics3d/INvGpuEngine.cs +++ b/Ryujinx.Graphics/Graphics3d/INvGpuEngine.cs @@ -6,6 +6,6 @@ namespace Ryujinx.Graphics.Graphics3d { int[] Registers { get; } - void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall); + void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall); } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/MacroInterpreter.cs b/Ryujinx.Graphics/Graphics3d/MacroInterpreter.cs index a124aca4..84576213 100644 --- a/Ryujinx.Graphics/Graphics3d/MacroInterpreter.cs +++ b/Ryujinx.Graphics/Graphics3d/MacroInterpreter.cs @@ -42,78 +42,78 @@ namespace Ryujinx.Graphics.Graphics3d BitwiseNotAnd = 12 } - private NvGpuFifo PFifo; - private INvGpuEngine Engine; + private NvGpuFifo _pFifo; + private INvGpuEngine _engine; public Queue<int> Fifo { get; private set; } - private int[] Gprs; + private int[] _gprs; - private int MethAddr; - private int MethIncr; + private int _methAddr; + private int _methIncr; - private bool Carry; + private bool _carry; - private int OpCode; + private int _opCode; - private int PipeOp; + private int _pipeOp; - private int Pc; + private int _pc; - public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine) + public MacroInterpreter(NvGpuFifo pFifo, INvGpuEngine engine) { - this.PFifo = PFifo; - this.Engine = Engine; + _pFifo = pFifo; + _engine = engine; Fifo = new Queue<int>(); - Gprs = new int[8]; + _gprs = new int[8]; } - public void Execute(NvGpuVmm Vmm, int[] Mme, int Position, int Param) + public void Execute(NvGpuVmm vmm, int[] mme, int position, int param) { Reset(); - Gprs[1] = Param; + _gprs[1] = param; - Pc = Position; + _pc = position; - FetchOpCode(Mme); + FetchOpCode(mme); - while (Step(Vmm, Mme)); + while (Step(vmm, mme)); //Due to the delay slot, we still need to execute //one more instruction before we actually exit. - Step(Vmm, Mme); + Step(vmm, mme); } private void Reset() { - for (int Index = 0; Index < Gprs.Length; Index++) + for (int index = 0; index < _gprs.Length; index++) { - Gprs[Index] = 0; + _gprs[index] = 0; } - MethAddr = 0; - MethIncr = 0; + _methAddr = 0; + _methIncr = 0; - Carry = false; + _carry = false; } - private bool Step(NvGpuVmm Vmm, int[] Mme) + private bool Step(NvGpuVmm vmm, int[] mme) { - int BaseAddr = Pc - 1; + int baseAddr = _pc - 1; - FetchOpCode(Mme); + FetchOpCode(mme); - if ((OpCode & 7) < 7) + if ((_opCode & 7) < 7) { //Operation produces a value. - AssignmentOperation AsgOp = (AssignmentOperation)((OpCode >> 4) & 7); + AssignmentOperation asgOp = (AssignmentOperation)((_opCode >> 4) & 7); - int Result = GetAluResult(); + int result = GetAluResult(); - switch (AsgOp) + switch (asgOp) { //Fetch parameter and ignore result. case AssignmentOperation.IgnoreAndFetch: @@ -126,7 +126,7 @@ namespace Ryujinx.Graphics.Graphics3d //Move result. case AssignmentOperation.Move: { - SetDstGpr(Result); + SetDstGpr(result); break; } @@ -134,9 +134,9 @@ namespace Ryujinx.Graphics.Graphics3d //Move result and use as Method Address. case AssignmentOperation.MoveAndSetMaddr: { - SetDstGpr(Result); + SetDstGpr(result); - SetMethAddr(Result); + SetMethAddr(result); break; } @@ -146,7 +146,7 @@ namespace Ryujinx.Graphics.Graphics3d { SetDstGpr(FetchParam()); - Send(Vmm, Result); + Send(vmm, result); break; } @@ -154,9 +154,9 @@ namespace Ryujinx.Graphics.Graphics3d //Move and send result. case AssignmentOperation.MoveAndSend: { - SetDstGpr(Result); + SetDstGpr(result); - Send(Vmm, Result); + Send(vmm, result); break; } @@ -166,7 +166,7 @@ namespace Ryujinx.Graphics.Graphics3d { SetDstGpr(FetchParam()); - SetMethAddr(Result); + SetMethAddr(result); break; } @@ -174,11 +174,11 @@ namespace Ryujinx.Graphics.Graphics3d //Move result and use as Method Address, then fetch and send paramter. case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend: { - SetDstGpr(Result); + SetDstGpr(result); - SetMethAddr(Result); + SetMethAddr(result); - Send(Vmm, FetchParam()); + Send(vmm, FetchParam()); break; } @@ -186,11 +186,11 @@ namespace Ryujinx.Graphics.Graphics3d //Move result and use as Method Address, then send bits 17:12 of result. case AssignmentOperation.MoveAndSetMaddrThenSendHigh: { - SetDstGpr(Result); + SetDstGpr(result); - SetMethAddr(Result); + SetMethAddr(result); - Send(Vmm, (Result >> 12) & 0x3f); + Send(vmm, (result >> 12) & 0x3f); break; } @@ -199,50 +199,50 @@ namespace Ryujinx.Graphics.Graphics3d else { //Branch. - bool OnNotZero = ((OpCode >> 4) & 1) != 0; + bool onNotZero = ((_opCode >> 4) & 1) != 0; - bool Taken = OnNotZero + bool taken = onNotZero ? GetGprA() != 0 : GetGprA() == 0; - if (Taken) + if (taken) { - Pc = BaseAddr + GetImm(); + _pc = baseAddr + GetImm(); - bool NoDelays = (OpCode & 0x20) != 0; + bool noDelays = (_opCode & 0x20) != 0; - if (NoDelays) + if (noDelays) { - FetchOpCode(Mme); + FetchOpCode(mme); } return true; } } - bool Exit = (OpCode & 0x80) != 0; + bool exit = (_opCode & 0x80) != 0; - return !Exit; + return !exit; } - private void FetchOpCode(int[] Mme) + private void FetchOpCode(int[] mme) { - OpCode = PipeOp; + _opCode = _pipeOp; - PipeOp = Mme[Pc++]; + _pipeOp = mme[_pc++]; } private int GetAluResult() { - AluOperation Op = (AluOperation)(OpCode & 7); + AluOperation op = (AluOperation)(_opCode & 7); - switch (Op) + switch (op) { case AluOperation.AluReg: { - AluRegOperation AluOp = (AluRegOperation)((OpCode >> 17) & 0x1f); + AluRegOperation aluOp = (AluRegOperation)((_opCode >> 17) & 0x1f); - return GetAluResult(AluOp, GetGprA(), GetGprB()); + return GetAluResult(aluOp, GetGprA(), GetGprB()); } case AluOperation.AddImmediate: @@ -254,40 +254,40 @@ namespace Ryujinx.Graphics.Graphics3d case AluOperation.BitfieldExtractLslImm: case AluOperation.BitfieldExtractLslReg: { - int BfSrcBit = (OpCode >> 17) & 0x1f; - int BfSize = (OpCode >> 22) & 0x1f; - int BfDstBit = (OpCode >> 27) & 0x1f; + int bfSrcBit = (_opCode >> 17) & 0x1f; + int bfSize = (_opCode >> 22) & 0x1f; + int bfDstBit = (_opCode >> 27) & 0x1f; - int BfMask = (1 << BfSize) - 1; + int bfMask = (1 << bfSize) - 1; - int Dst = GetGprA(); - int Src = GetGprB(); + int dst = GetGprA(); + int src = GetGprB(); - switch (Op) + switch (op) { case AluOperation.BitfieldReplace: { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; + src = (int)((uint)src >> bfSrcBit) & bfMask; - Dst &= ~(BfMask << BfDstBit); + dst &= ~(bfMask << bfDstBit); - Dst |= Src << BfDstBit; + dst |= src << bfDstBit; - return Dst; + return dst; } case AluOperation.BitfieldExtractLslImm: { - Src = (int)((uint)Src >> Dst) & BfMask; + src = (int)((uint)src >> dst) & bfMask; - return Src << BfDstBit; + return src << bfDstBit; } case AluOperation.BitfieldExtractLslReg: { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; + src = (int)((uint)src >> bfSrcBit) & bfMask; - return Src << Dst; + return src << dst; } } @@ -300,117 +300,117 @@ namespace Ryujinx.Graphics.Graphics3d } } - throw new ArgumentException(nameof(OpCode)); + throw new ArgumentException(nameof(_opCode)); } - private int GetAluResult(AluRegOperation AluOp, int A, int B) + private int GetAluResult(AluRegOperation aluOp, int a, int b) { - switch (AluOp) + switch (aluOp) { case AluRegOperation.Add: { - ulong Result = (ulong)A + (ulong)B; + ulong result = (ulong)a + (ulong)b; - Carry = Result > 0xffffffff; + _carry = result > 0xffffffff; - return (int)Result; + return (int)result; } case AluRegOperation.AddWithCarry: { - ulong Result = (ulong)A + (ulong)B + (Carry ? 1UL : 0UL); + ulong result = (ulong)a + (ulong)b + (_carry ? 1UL : 0UL); - Carry = Result > 0xffffffff; + _carry = result > 0xffffffff; - return (int)Result; + return (int)result; } case AluRegOperation.Subtract: { - ulong Result = (ulong)A - (ulong)B; + ulong result = (ulong)a - (ulong)b; - Carry = Result < 0x100000000; + _carry = result < 0x100000000; - return (int)Result; + return (int)result; } case AluRegOperation.SubtractWithBorrow: { - ulong Result = (ulong)A - (ulong)B - (Carry ? 0UL : 1UL); + ulong result = (ulong)a - (ulong)b - (_carry ? 0UL : 1UL); - Carry = Result < 0x100000000; + _carry = result < 0x100000000; - return (int)Result; + return (int)result; } - case AluRegOperation.BitwiseExclusiveOr: return A ^ B; - case AluRegOperation.BitwiseOr: return A | B; - case AluRegOperation.BitwiseAnd: return A & B; - case AluRegOperation.BitwiseAndNot: return A & ~B; - case AluRegOperation.BitwiseNotAnd: return ~(A & B); + case AluRegOperation.BitwiseExclusiveOr: return a ^ b; + case AluRegOperation.BitwiseOr: return a | b; + case AluRegOperation.BitwiseAnd: return a & b; + case AluRegOperation.BitwiseAndNot: return a & ~b; + case AluRegOperation.BitwiseNotAnd: return ~(a & b); } - throw new ArgumentOutOfRangeException(nameof(AluOp)); + throw new ArgumentOutOfRangeException(nameof(aluOp)); } private int GetImm() { //Note: The immediate is signed, the sign-extension is intended here. - return OpCode >> 14; + return _opCode >> 14; } - private void SetMethAddr(int Value) + private void SetMethAddr(int value) { - MethAddr = (Value >> 0) & 0xfff; - MethIncr = (Value >> 12) & 0x3f; + _methAddr = (value >> 0) & 0xfff; + _methIncr = (value >> 12) & 0x3f; } - private void SetDstGpr(int Value) + private void SetDstGpr(int value) { - Gprs[(OpCode >> 8) & 7] = Value; + _gprs[(_opCode >> 8) & 7] = value; } private int GetGprA() { - return GetGprValue((OpCode >> 11) & 7); + return GetGprValue((_opCode >> 11) & 7); } private int GetGprB() { - return GetGprValue((OpCode >> 14) & 7); + return GetGprValue((_opCode >> 14) & 7); } - private int GetGprValue(int Index) + private int GetGprValue(int index) { - return Index != 0 ? Gprs[Index] : 0; + return index != 0 ? _gprs[index] : 0; } private int FetchParam() { - int Value; + int value; - if (!Fifo.TryDequeue(out Value)) + if (!Fifo.TryDequeue(out value)) { Logger.PrintWarning(LogClass.Gpu, "Macro attempted to fetch an inexistent argument."); return 0; } - return Value; + return value; } - private int Read(int Reg) + private int Read(int reg) { - return Engine.Registers[Reg]; + return _engine.Registers[reg]; } - private void Send(NvGpuVmm Vmm, int Value) + private void Send(NvGpuVmm vmm, int value) { - GpuMethodCall MethCall = new GpuMethodCall(MethAddr, Value); + GpuMethodCall methCall = new GpuMethodCall(_methAddr, value); - Engine.CallMethod(Vmm, MethCall); + _engine.CallMethod(vmm, methCall); - MethAddr += MethIncr; + _methAddr += _methIncr; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuEngine2d.cs b/Ryujinx.Graphics/Graphics3d/NvGpuEngine2d.cs index 3295f6da..361b1706 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuEngine2d.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuEngine2d.cs @@ -1,4 +1,3 @@ -using Ryujinx.Common.Logging; using Ryujinx.Graphics.Gal; using Ryujinx.Graphics.Memory; using Ryujinx.Graphics.Texture; @@ -20,187 +19,187 @@ namespace Ryujinx.Graphics.Graphics3d public int[] Registers { get; private set; } - private NvGpu Gpu; + private NvGpu _gpu; - public NvGpuEngine2d(NvGpu Gpu) + public NvGpuEngine2d(NvGpu gpu) { - this.Gpu = Gpu; + _gpu = gpu; Registers = new int[0x238]; } - public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - WriteRegister(MethCall); + WriteRegister(methCall); - if ((NvGpuEngine2dReg)MethCall.Method == NvGpuEngine2dReg.BlitSrcYInt) + if ((NvGpuEngine2dReg)methCall.Method == NvGpuEngine2dReg.BlitSrcYInt) { - TextureCopy(Vmm); + TextureCopy(vmm); } } - private void TextureCopy(NvGpuVmm Vmm) + private void TextureCopy(NvGpuVmm vmm) { - CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); + CopyOperation operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); - int DstFormat = ReadRegister(NvGpuEngine2dReg.DstFormat); - bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; - int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); - int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); - int DstDepth = ReadRegister(NvGpuEngine2dReg.DstDepth); - int DstLayer = ReadRegister(NvGpuEngine2dReg.DstLayer); - int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); - int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); + int dstFormat = ReadRegister(NvGpuEngine2dReg.DstFormat); + bool dstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; + int dstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); + int dstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); + int dstDepth = ReadRegister(NvGpuEngine2dReg.DstDepth); + int dstLayer = ReadRegister(NvGpuEngine2dReg.DstLayer); + int dstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); + int dstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); - int SrcFormat = ReadRegister(NvGpuEngine2dReg.SrcFormat); - bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; - int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); - int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); - int SrcDepth = ReadRegister(NvGpuEngine2dReg.SrcDepth); - int SrcLayer = ReadRegister(NvGpuEngine2dReg.SrcLayer); - int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch); - int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions); + int srcFormat = ReadRegister(NvGpuEngine2dReg.SrcFormat); + bool srcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; + int srcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); + int srcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); + int srcDepth = ReadRegister(NvGpuEngine2dReg.SrcDepth); + int srcLayer = ReadRegister(NvGpuEngine2dReg.SrcLayer); + int srcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch); + int srcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions); - int DstBlitX = ReadRegister(NvGpuEngine2dReg.BlitDstX); - int DstBlitY = ReadRegister(NvGpuEngine2dReg.BlitDstY); - int DstBlitW = ReadRegister(NvGpuEngine2dReg.BlitDstW); - int DstBlitH = ReadRegister(NvGpuEngine2dReg.BlitDstH); + int dstBlitX = ReadRegister(NvGpuEngine2dReg.BlitDstX); + int dstBlitY = ReadRegister(NvGpuEngine2dReg.BlitDstY); + int dstBlitW = ReadRegister(NvGpuEngine2dReg.BlitDstW); + int dstBlitH = ReadRegister(NvGpuEngine2dReg.BlitDstH); - long BlitDuDx = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitDuDxFract); - long BlitDvDy = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitDvDyFract); + long blitDuDx = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitDuDxFract); + long blitDvDy = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitDvDyFract); - long SrcBlitX = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitSrcXFract); - long SrcBlitY = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitSrcYFract); + long srcBlitX = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitSrcXFract); + long srcBlitY = ReadRegisterFixed1_31_32(NvGpuEngine2dReg.BlitSrcYFract); - GalImageFormat SrcImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)SrcFormat); - GalImageFormat DstImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)DstFormat); + GalImageFormat srcImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)srcFormat); + GalImageFormat dstImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)dstFormat); - GalMemoryLayout SrcLayout = GetLayout(SrcLinear); - GalMemoryLayout DstLayout = GetLayout(DstLinear); + GalMemoryLayout srcLayout = GetLayout(srcLinear); + GalMemoryLayout dstLayout = GetLayout(dstLinear); - int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + int srcBlockHeight = 1 << ((srcBlkDim >> 4) & 0xf); + int dstBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf); - long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); + long srcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); + long dstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); - long SrcKey = Vmm.GetPhysicalAddress(SrcAddress); - long DstKey = Vmm.GetPhysicalAddress(DstAddress); + long srcKey = vmm.GetPhysicalAddress(srcAddress); + long dstKey = vmm.GetPhysicalAddress(dstAddress); - bool IsSrcLayered = false; - bool IsDstLayered = false; + bool isSrcLayered = false; + bool isDstLayered = false; - GalTextureTarget SrcTarget = GalTextureTarget.TwoD; + GalTextureTarget srcTarget = GalTextureTarget.TwoD; - if (SrcDepth != 0) + if (srcDepth != 0) { - SrcTarget = GalTextureTarget.TwoDArray; - SrcDepth++; - IsSrcLayered = true; + srcTarget = GalTextureTarget.TwoDArray; + srcDepth++; + isSrcLayered = true; } else { - SrcDepth = 1; + srcDepth = 1; } - GalTextureTarget DstTarget = GalTextureTarget.TwoD; + GalTextureTarget dstTarget = GalTextureTarget.TwoD; - if (DstDepth != 0) + if (dstDepth != 0) { - DstTarget = GalTextureTarget.TwoDArray; - DstDepth++; - IsDstLayered = true; + dstTarget = GalTextureTarget.TwoDArray; + dstDepth++; + isDstLayered = true; } else { - DstDepth = 1; + dstDepth = 1; } - GalImage SrcTexture = new GalImage( - SrcWidth, - SrcHeight, - 1, SrcDepth, 1, - SrcBlockHeight, 1, - SrcLayout, - SrcImgFormat, - SrcTarget); - - GalImage DstTexture = new GalImage( - DstWidth, - DstHeight, - 1, DstDepth, 1, - DstBlockHeight, 1, - DstLayout, - DstImgFormat, - DstTarget); - - SrcTexture.Pitch = SrcPitch; - DstTexture.Pitch = DstPitch; - - long GetLayerOffset(GalImage Image, int Layer) + GalImage srcTexture = new GalImage( + srcWidth, + srcHeight, + 1, srcDepth, 1, + srcBlockHeight, 1, + srcLayout, + srcImgFormat, + srcTarget); + + GalImage dstTexture = new GalImage( + dstWidth, + dstHeight, + 1, dstDepth, 1, + dstBlockHeight, 1, + dstLayout, + dstImgFormat, + dstTarget); + + srcTexture.Pitch = srcPitch; + dstTexture.Pitch = dstPitch; + + long GetLayerOffset(GalImage image, int layer) { - int TargetMipLevel = Image.MaxMipmapLevel <= 1 ? 1 : Image.MaxMipmapLevel - 1; - return ImageUtils.GetLayerOffset(Image, TargetMipLevel) * Layer; + int targetMipLevel = image.MaxMipmapLevel <= 1 ? 1 : image.MaxMipmapLevel - 1; + return ImageUtils.GetLayerOffset(image, targetMipLevel) * layer; } - int SrcLayerIndex = -1; + int srcLayerIndex = -1; - if (IsSrcLayered && Gpu.ResourceManager.TryGetTextureLayer(SrcKey, out SrcLayerIndex) && SrcLayerIndex != 0) + if (isSrcLayered && _gpu.ResourceManager.TryGetTextureLayer(srcKey, out srcLayerIndex) && srcLayerIndex != 0) { - SrcKey = SrcKey - GetLayerOffset(SrcTexture, SrcLayerIndex); + srcKey = srcKey - GetLayerOffset(srcTexture, srcLayerIndex); } - int DstLayerIndex = -1; + int dstLayerIndex = -1; - if (IsDstLayered && Gpu.ResourceManager.TryGetTextureLayer(DstKey, out DstLayerIndex) && DstLayerIndex != 0) + if (isDstLayered && _gpu.ResourceManager.TryGetTextureLayer(dstKey, out dstLayerIndex) && dstLayerIndex != 0) { - DstKey = DstKey - GetLayerOffset(DstTexture, DstLayerIndex); + dstKey = dstKey - GetLayerOffset(dstTexture, dstLayerIndex); } - Gpu.ResourceManager.SendTexture(Vmm, SrcKey, SrcTexture); - Gpu.ResourceManager.SendTexture(Vmm, DstKey, DstTexture); + _gpu.ResourceManager.SendTexture(vmm, srcKey, srcTexture); + _gpu.ResourceManager.SendTexture(vmm, dstKey, dstTexture); - if (IsSrcLayered && SrcLayerIndex == -1) + if (isSrcLayered && srcLayerIndex == -1) { - for (int Layer = 0; Layer < SrcTexture.LayerCount; Layer++) + for (int layer = 0; layer < srcTexture.LayerCount; layer++) { - Gpu.ResourceManager.SetTextureArrayLayer(SrcKey + GetLayerOffset(SrcTexture, Layer), Layer); + _gpu.ResourceManager.SetTextureArrayLayer(srcKey + GetLayerOffset(srcTexture, layer), layer); } - SrcLayerIndex = 0; + srcLayerIndex = 0; } - if (IsDstLayered && DstLayerIndex == -1) + if (isDstLayered && dstLayerIndex == -1) { - for (int Layer = 0; Layer < DstTexture.LayerCount; Layer++) + for (int layer = 0; layer < dstTexture.LayerCount; layer++) { - Gpu.ResourceManager.SetTextureArrayLayer(DstKey + GetLayerOffset(DstTexture, Layer), Layer); + _gpu.ResourceManager.SetTextureArrayLayer(dstKey + GetLayerOffset(dstTexture, layer), layer); } - DstLayerIndex = 0; + dstLayerIndex = 0; } - int SrcBlitX1 = (int)(SrcBlitX >> 32); - int SrcBlitY1 = (int)(SrcBlitY >> 32); - - int SrcBlitX2 = (int)(SrcBlitX + DstBlitW * BlitDuDx >> 32); - int SrcBlitY2 = (int)(SrcBlitY + DstBlitH * BlitDvDy >> 32); - - Gpu.Renderer.RenderTarget.Copy( - SrcTexture, - DstTexture, - SrcKey, - DstKey, - SrcLayerIndex, - DstLayerIndex, - SrcBlitX1, - SrcBlitY1, - SrcBlitX2, - SrcBlitY2, - DstBlitX, - DstBlitY, - DstBlitX + DstBlitW, - DstBlitY + DstBlitH); + int srcBlitX1 = (int)(srcBlitX >> 32); + int srcBlitY1 = (int)(srcBlitY >> 32); + + int srcBlitX2 = (int)(srcBlitX + dstBlitW * blitDuDx >> 32); + int srcBlitY2 = (int)(srcBlitY + dstBlitH * blitDvDy >> 32); + + _gpu.Renderer.RenderTarget.Copy( + srcTexture, + dstTexture, + srcKey, + dstKey, + srcLayerIndex, + dstLayerIndex, + srcBlitX1, + srcBlitY1, + srcBlitX2, + srcBlitY2, + dstBlitX, + dstBlitY, + dstBlitX + dstBlitW, + dstBlitY + dstBlitH); //Do a guest side copy aswell. This is necessary when //the texture is modified by the guest, however it doesn't @@ -209,51 +208,51 @@ namespace Ryujinx.Graphics.Graphics3d // FIXME: SUPPORT MULTILAYER CORRECTLY HERE (this will cause weird stuffs on the first layer) ImageUtils.CopyTexture( - Vmm, - SrcTexture, - DstTexture, - SrcAddress, - DstAddress, - SrcBlitX1, - SrcBlitY1, - DstBlitX, - DstBlitY, - DstBlitW, - DstBlitH); - - Vmm.IsRegionModified(DstKey, ImageUtils.GetSize(DstTexture), NvGpuBufferType.Texture); + vmm, + srcTexture, + dstTexture, + srcAddress, + dstAddress, + srcBlitX1, + srcBlitY1, + dstBlitX, + dstBlitY, + dstBlitW, + dstBlitH); + + vmm.IsRegionModified(dstKey, ImageUtils.GetSize(dstTexture), NvGpuBufferType.Texture); } - private static GalMemoryLayout GetLayout(bool Linear) + private static GalMemoryLayout GetLayout(bool linear) { - return Linear + return linear ? GalMemoryLayout.Pitch : GalMemoryLayout.BlockLinear; } - private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) + private long MakeInt64From2xInt32(NvGpuEngine2dReg reg) { return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; + (long)Registers[(int)reg + 0] << 32 | + (uint)Registers[(int)reg + 1]; } - private void WriteRegister(GpuMethodCall MethCall) + private void WriteRegister(GpuMethodCall methCall) { - Registers[MethCall.Method] = MethCall.Argument; + Registers[methCall.Method] = methCall.Argument; } - private long ReadRegisterFixed1_31_32(NvGpuEngine2dReg Reg) + private long ReadRegisterFixed1_31_32(NvGpuEngine2dReg reg) { - long Low = (uint)ReadRegister(Reg + 0); - long High = (uint)ReadRegister(Reg + 1); + long low = (uint)ReadRegister(reg + 0); + long high = (uint)ReadRegister(reg + 1); - return Low | (High << 32); + return low | (high << 32); } - private int ReadRegister(NvGpuEngine2dReg Reg) + private int ReadRegister(NvGpuEngine2dReg reg) { - return Registers[(int)Reg]; + return Registers[(int)reg]; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuEngine3d.cs b/Ryujinx.Graphics/Graphics3d/NvGpuEngine3d.cs index 370fe1fa..605cbda8 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuEngine3d.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuEngine3d.cs @@ -1,5 +1,4 @@ using Ryujinx.Common; -using Ryujinx.Common.Logging; using Ryujinx.Graphics.Gal; using Ryujinx.Graphics.Memory; using Ryujinx.Graphics.Texture; @@ -12,9 +11,9 @@ namespace Ryujinx.Graphics.Graphics3d { public int[] Registers { get; private set; } - private NvGpu Gpu; + private NvGpu _gpu; - private Dictionary<int, NvGpuMethod> Methods; + private Dictionary<int, NvGpuMethod> _methods; private struct ConstBuffer { @@ -23,33 +22,33 @@ namespace Ryujinx.Graphics.Graphics3d public int Size; } - private ConstBuffer[][] ConstBuffers; + private ConstBuffer[][] _constBuffers; // Viewport dimensions kept for scissor test limits - private int ViewportX0 = 0; - private int ViewportY0 = 0; - private int ViewportX1 = 0; - private int ViewportY1 = 0; - private int ViewportWidth = 0; - private int ViewportHeight = 0; + private int _viewportX0 = 0; + private int _viewportY0 = 0; + private int _viewportX1 = 0; + private int _viewportY1 = 0; + private int _viewportWidth = 0; + private int _viewportHeight = 0; - private int CurrentInstance = 0; + private int _currentInstance = 0; - public NvGpuEngine3d(NvGpu Gpu) + public NvGpuEngine3d(NvGpu gpu) { - this.Gpu = Gpu; + _gpu = gpu; Registers = new int[0xe00]; - Methods = new Dictionary<int, NvGpuMethod>(); + _methods = new Dictionary<int, NvGpuMethod>(); - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + void AddMethod(int meth, int count, int stride, NvGpuMethod method) { - while (Count-- > 0) + while (count-- > 0) { - Methods.Add(Meth, Method); + _methods.Add(meth, method); - Meth += Stride; + meth += stride; } } @@ -59,11 +58,11 @@ namespace Ryujinx.Graphics.Graphics3d AddMethod(0x8e4, 16, 1, CbData); AddMethod(0x904, 5, 8, CbBind); - ConstBuffers = new ConstBuffer[6][]; + _constBuffers = new ConstBuffer[6][]; - for (int Index = 0; Index < ConstBuffers.Length; Index++) + for (int index = 0; index < _constBuffers.Length; index++) { - ConstBuffers[Index] = new ConstBuffer[18]; + _constBuffers[index] = new ConstBuffer[18]; } //Ensure that all components are enabled by default. @@ -72,227 +71,227 @@ namespace Ryujinx.Graphics.Graphics3d WriteRegister(NvGpuEngine3dReg.FrameBufferSrgb, 1); - WriteRegister(NvGpuEngine3dReg.FrontFace, (int)GalFrontFace.CW); + WriteRegister(NvGpuEngine3dReg.FrontFace, (int)GalFrontFace.Cw); - for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++) + for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++) { - WriteRegister(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8, (int)GalBlendEquation.FuncAdd); - WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8, (int)GalBlendFactor.One); - WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8, (int)GalBlendFactor.Zero); - WriteRegister(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8, (int)GalBlendEquation.FuncAdd); - WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8, (int)GalBlendFactor.One); - WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8, (int)GalBlendFactor.Zero); + WriteRegister(NvGpuEngine3dReg.IBlendNEquationRgb + index * 8, (int)GalBlendEquation.FuncAdd); + WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb + index * 8, (int)GalBlendFactor.One); + WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb + index * 8, (int)GalBlendFactor.Zero); + WriteRegister(NvGpuEngine3dReg.IBlendNEquationAlpha + index * 8, (int)GalBlendEquation.FuncAdd); + WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha + index * 8, (int)GalBlendFactor.One); + WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha + index * 8, (int)GalBlendFactor.Zero); } } - public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method)) + if (_methods.TryGetValue(methCall.Method, out NvGpuMethod method)) { - Method(Vmm, MethCall); + method(vmm, methCall); } else { - WriteRegister(MethCall); + WriteRegister(methCall); } } - private void VertexEndGl(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void VertexEndGl(NvGpuVmm vmm, GpuMethodCall methCall) { LockCaches(); - GalPipelineState State = new GalPipelineState(); + GalPipelineState state = new GalPipelineState(); // Framebuffer must be run configured because viewport dimensions may be used in other methods - SetFrameBuffer(State); + SetFrameBuffer(state); - for (int FbIndex = 0; FbIndex < 8; FbIndex++) + for (int fbIndex = 0; fbIndex < 8; fbIndex++) { - SetFrameBuffer(Vmm, FbIndex); + SetFrameBuffer(vmm, fbIndex); } - SetFrontFace(State); - SetCullFace(State); - SetDepth(State); - SetStencil(State); - SetScissor(State); - SetBlending(State); - SetColorMask(State); - SetPrimitiveRestart(State); + SetFrontFace(state); + SetCullFace(state); + SetDepth(state); + SetStencil(state); + SetScissor(state); + SetBlending(state); + SetColorMask(state); + SetPrimitiveRestart(state); - SetZeta(Vmm); + SetZeta(vmm); SetRenderTargets(); - long[] Keys = UploadShaders(Vmm); + long[] keys = UploadShaders(vmm); - Gpu.Renderer.Shader.BindProgram(); + _gpu.Renderer.Shader.BindProgram(); - UploadTextures(Vmm, State, Keys); - UploadConstBuffers(Vmm, State, Keys); - UploadVertexArrays(Vmm, State); + UploadTextures(vmm, state, keys); + UploadConstBuffers(vmm, state, keys); + UploadVertexArrays(vmm, state); - DispatchRender(Vmm, State); + DispatchRender(vmm, state); UnlockCaches(); } private void LockCaches() { - Gpu.Renderer.Buffer.LockCache(); - Gpu.Renderer.Rasterizer.LockCaches(); - Gpu.Renderer.Texture.LockCache(); + _gpu.Renderer.Buffer.LockCache(); + _gpu.Renderer.Rasterizer.LockCaches(); + _gpu.Renderer.Texture.LockCache(); } private void UnlockCaches() { - Gpu.Renderer.Buffer.UnlockCache(); - Gpu.Renderer.Rasterizer.UnlockCaches(); - Gpu.Renderer.Texture.UnlockCache(); + _gpu.Renderer.Buffer.UnlockCache(); + _gpu.Renderer.Rasterizer.UnlockCaches(); + _gpu.Renderer.Texture.UnlockCache(); } - private void ClearBuffers(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void ClearBuffers(NvGpuVmm vmm, GpuMethodCall methCall) { - int Attachment = (MethCall.Argument >> 6) & 0xf; + int attachment = (methCall.Argument >> 6) & 0xf; - GalClearBufferFlags Flags = (GalClearBufferFlags)(MethCall.Argument & 0x3f); + GalClearBufferFlags flags = (GalClearBufferFlags)(methCall.Argument & 0x3f); - float Red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0); - float Green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1); - float Blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2); - float Alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3); + float red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0); + float green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1); + float blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2); + float alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3); - float Depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth); + float depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth); - int Stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil); + int stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil); - SetFrameBuffer(Vmm, Attachment); + SetFrameBuffer(vmm, attachment); - SetZeta(Vmm); + SetZeta(vmm); SetRenderTargets(); - Gpu.Renderer.RenderTarget.Bind(); + _gpu.Renderer.RenderTarget.Bind(); - Gpu.Renderer.Rasterizer.ClearBuffers(Flags, Attachment, Red, Green, Blue, Alpha, Depth, Stencil); + _gpu.Renderer.Rasterizer.ClearBuffers(flags, attachment, red, green, blue, alpha, depth, stencil); - Gpu.Renderer.Pipeline.ResetDepthMask(); - Gpu.Renderer.Pipeline.ResetColorMask(Attachment); + _gpu.Renderer.Pipeline.ResetDepthMask(); + _gpu.Renderer.Pipeline.ResetColorMask(attachment); } - private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex) + private void SetFrameBuffer(NvGpuVmm vmm, int fbIndex) { - long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10); + long va = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + fbIndex * 0x10); - int SurfFormat = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + FbIndex * 0x10); + int surfFormat = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + fbIndex * 0x10); - if (VA == 0 || SurfFormat == 0) + if (va == 0 || surfFormat == 0) { - Gpu.Renderer.RenderTarget.UnbindColor(FbIndex); + _gpu.Renderer.RenderTarget.UnbindColor(fbIndex); return; } - long Key = Vmm.GetPhysicalAddress(VA); + long key = vmm.GetPhysicalAddress(va); - int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); - int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); + int width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + fbIndex * 0x10); + int height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + fbIndex * 0x10); - int ArrayMode = ReadRegister(NvGpuEngine3dReg.FrameBufferNArrayMode + FbIndex * 0x10); - int LayerCount = ArrayMode & 0xFFFF; - int LayerStride = ReadRegister(NvGpuEngine3dReg.FrameBufferNLayerStride + FbIndex * 0x10); - int BaseLayer = ReadRegister(NvGpuEngine3dReg.FrameBufferNBaseLayer + FbIndex * 0x10); - int BlockDim = ReadRegister(NvGpuEngine3dReg.FrameBufferNBlockDim + FbIndex * 0x10); + int arrayMode = ReadRegister(NvGpuEngine3dReg.FrameBufferNArrayMode + fbIndex * 0x10); + int layerCount = arrayMode & 0xFFFF; + int layerStride = ReadRegister(NvGpuEngine3dReg.FrameBufferNLayerStride + fbIndex * 0x10); + int baseLayer = ReadRegister(NvGpuEngine3dReg.FrameBufferNBaseLayer + fbIndex * 0x10); + int blockDim = ReadRegister(NvGpuEngine3dReg.FrameBufferNBlockDim + fbIndex * 0x10); - int GobBlockHeight = 1 << ((BlockDim >> 4) & 7); + int gobBlockHeight = 1 << ((blockDim >> 4) & 7); - GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1); + GalMemoryLayout layout = (GalMemoryLayout)((blockDim >> 12) & 1); - float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 8); - float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 8); + float tx = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + fbIndex * 8); + float ty = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + fbIndex * 8); - float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 8); - float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 8); + float sx = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + fbIndex * 8); + float sy = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + fbIndex * 8); - ViewportX0 = (int)MathF.Max(0, TX - MathF.Abs(SX)); - ViewportY0 = (int)MathF.Max(0, TY - MathF.Abs(SY)); + _viewportX0 = (int)MathF.Max(0, tx - MathF.Abs(sx)); + _viewportY0 = (int)MathF.Max(0, ty - MathF.Abs(sy)); - ViewportX1 = (int)(TX + MathF.Abs(SX)); - ViewportY1 = (int)(TY + MathF.Abs(SY)); + _viewportX1 = (int)(tx + MathF.Abs(sx)); + _viewportY1 = (int)(ty + MathF.Abs(sy)); - GalImageFormat Format = ImageUtils.ConvertSurface((GalSurfaceFormat)SurfFormat); + GalImageFormat format = ImageUtils.ConvertSurface((GalSurfaceFormat)surfFormat); - GalImage Image = new GalImage(Width, Height, 1, 1, 1, GobBlockHeight, 1, Layout, Format, GalTextureTarget.TwoD); + GalImage image = new GalImage(width, height, 1, 1, 1, gobBlockHeight, 1, layout, format, GalTextureTarget.TwoD); - Gpu.ResourceManager.SendColorBuffer(Vmm, Key, FbIndex, Image); + _gpu.ResourceManager.SendColorBuffer(vmm, key, fbIndex, image); - Gpu.Renderer.RenderTarget.SetViewport(FbIndex, ViewportX0, ViewportY0, ViewportX1 - ViewportX0, ViewportY1 - ViewportY0); + _gpu.Renderer.RenderTarget.SetViewport(fbIndex, _viewportX0, _viewportY0, _viewportX1 - _viewportX0, _viewportY1 - _viewportY0); } - private void SetFrameBuffer(GalPipelineState State) + private void SetFrameBuffer(GalPipelineState state) { - State.FramebufferSrgb = ReadRegisterBool(NvGpuEngine3dReg.FrameBufferSrgb); + state.FramebufferSrgb = ReadRegisterBool(NvGpuEngine3dReg.FrameBufferSrgb); - State.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); - State.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); + state.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); + state.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); - int ScreenYControl = ReadRegister(NvGpuEngine3dReg.ScreenYControl); + int screenYControl = ReadRegister(NvGpuEngine3dReg.ScreenYControl); - bool NegateY = (ScreenYControl & 1) != 0; + bool negateY = (screenYControl & 1) != 0; - if (NegateY) + if (negateY) { - State.FlipY = -State.FlipY; + state.FlipY = -state.FlipY; } } - private void SetZeta(NvGpuVmm Vmm) + private void SetZeta(NvGpuVmm vmm) { - long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress); + long va = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress); - int ZetaFormat = ReadRegister(NvGpuEngine3dReg.ZetaFormat); + int zetaFormat = ReadRegister(NvGpuEngine3dReg.ZetaFormat); - int BlockDim = ReadRegister(NvGpuEngine3dReg.ZetaBlockDimensions); + int blockDim = ReadRegister(NvGpuEngine3dReg.ZetaBlockDimensions); - int GobBlockHeight = 1 << ((BlockDim >> 4) & 7); + int gobBlockHeight = 1 << ((blockDim >> 4) & 7); - GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1); //? + GalMemoryLayout layout = (GalMemoryLayout)((blockDim >> 12) & 1); //? - bool ZetaEnable = ReadRegisterBool(NvGpuEngine3dReg.ZetaEnable); + bool zetaEnable = ReadRegisterBool(NvGpuEngine3dReg.ZetaEnable); - if (VA == 0 || ZetaFormat == 0 || !ZetaEnable) + if (va == 0 || zetaFormat == 0 || !zetaEnable) { - Gpu.Renderer.RenderTarget.UnbindZeta(); + _gpu.Renderer.RenderTarget.UnbindZeta(); return; } - long Key = Vmm.GetPhysicalAddress(VA); + long key = vmm.GetPhysicalAddress(va); - int Width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz); - int Height = ReadRegister(NvGpuEngine3dReg.ZetaVert); + int width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz); + int height = ReadRegister(NvGpuEngine3dReg.ZetaVert); - GalImageFormat Format = ImageUtils.ConvertZeta((GalZetaFormat)ZetaFormat); + GalImageFormat format = ImageUtils.ConvertZeta((GalZetaFormat)zetaFormat); // TODO: Support non 2D? - GalImage Image = new GalImage(Width, Height, 1, 1, 1, GobBlockHeight, 1, Layout, Format, GalTextureTarget.TwoD); + GalImage image = new GalImage(width, height, 1, 1, 1, gobBlockHeight, 1, layout, format, GalTextureTarget.TwoD); - Gpu.ResourceManager.SendZetaBuffer(Vmm, Key, Image); + _gpu.ResourceManager.SendZetaBuffer(vmm, key, image); } - private long[] UploadShaders(NvGpuVmm Vmm) + private long[] UploadShaders(NvGpuVmm vmm) { - long[] Keys = new long[5]; + long[] keys = new long[5]; - long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + long basePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - int Index = 1; + int index = 1; - int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl); + int vpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl); - bool VpAEnable = (VpAControl & 1) != 0; + bool vpAEnable = (vpAControl & 1) != 0; - if (VpAEnable) + if (vpAEnable) { //Note: The maxwell supports 2 vertex programs, usually //only VP B is used, but in some cases VP A is also used. @@ -300,51 +299,51 @@ namespace Ryujinx.Graphics.Graphics3d //shader stage. //The graphics abstraction layer has a special overload for this //case, which should merge the two shaders into one vertex shader. - int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset); - int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10); + int vpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset); + int vpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10); - long VpAPos = BasePosition + (uint)VpAOffset; - long VpBPos = BasePosition + (uint)VpBOffset; + long vpAPos = basePosition + (uint)vpAOffset; + long vpBPos = basePosition + (uint)vpBOffset; - Keys[(int)GalShaderType.Vertex] = VpBPos; + keys[(int)GalShaderType.Vertex] = vpBPos; - Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex); - Gpu.Renderer.Shader.Bind(VpBPos); + _gpu.Renderer.Shader.Create(vmm, vpAPos, vpBPos, GalShaderType.Vertex); + _gpu.Renderer.Shader.Bind(vpBPos); - Index = 2; + index = 2; } - for (; Index < 6; Index++) + for (; index < 6; index++) { - GalShaderType Type = GetTypeFromProgram(Index); + GalShaderType type = GetTypeFromProgram(index); - int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10); - int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10); + int control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + index * 0x10); + int offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + index * 0x10); //Note: Vertex Program (B) is always enabled. - bool Enable = (Control & 1) != 0 || Index == 1; + bool enable = (control & 1) != 0 || index == 1; - if (!Enable) + if (!enable) { - Gpu.Renderer.Shader.Unbind(Type); + _gpu.Renderer.Shader.Unbind(type); continue; } - long Key = BasePosition + (uint)Offset; + long key = basePosition + (uint)offset; - Keys[(int)Type] = Key; + keys[(int)type] = key; - Gpu.Renderer.Shader.Create(Vmm, Key, Type); - Gpu.Renderer.Shader.Bind(Key); + _gpu.Renderer.Shader.Create(vmm, key, type); + _gpu.Renderer.Shader.Bind(key); } - return Keys; + return keys; } - private static GalShaderType GetTypeFromProgram(int Program) + private static GalShaderType GetTypeFromProgram(int program) { - switch (Program) + switch (program) { case 0: case 1: return GalShaderType.Vertex; @@ -354,104 +353,104 @@ namespace Ryujinx.Graphics.Graphics3d case 5: return GalShaderType.Fragment; } - throw new ArgumentOutOfRangeException(nameof(Program)); + throw new ArgumentOutOfRangeException(nameof(program)); } - private void SetFrontFace(GalPipelineState State) + private void SetFrontFace(GalPipelineState state) { - float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); - float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); + float signX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); + float signY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); - GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace); + GalFrontFace frontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace); //Flipping breaks facing. Flipping front facing too fixes it - if (SignX != SignY) + if (signX != signY) { - switch (FrontFace) + switch (frontFace) { - case GalFrontFace.CW: FrontFace = GalFrontFace.CCW; break; - case GalFrontFace.CCW: FrontFace = GalFrontFace.CW; break; + case GalFrontFace.Cw: frontFace = GalFrontFace.Ccw; break; + case GalFrontFace.Ccw: frontFace = GalFrontFace.Cw; break; } } - State.FrontFace = FrontFace; + state.FrontFace = frontFace; } - private void SetCullFace(GalPipelineState State) + private void SetCullFace(GalPipelineState state) { - State.CullFaceEnabled = ReadRegisterBool(NvGpuEngine3dReg.CullFaceEnable); + state.CullFaceEnabled = ReadRegisterBool(NvGpuEngine3dReg.CullFaceEnable); - if (State.CullFaceEnabled) + if (state.CullFaceEnabled) { - State.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace); + state.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace); } } - private void SetDepth(GalPipelineState State) + private void SetDepth(GalPipelineState state) { - State.DepthTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthTestEnable); + state.DepthTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthTestEnable); - State.DepthWriteEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthWriteEnable); + state.DepthWriteEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthWriteEnable); - if (State.DepthTestEnabled) + if (state.DepthTestEnabled) { - State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction); + state.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction); } - State.DepthRangeNear = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNNear); - State.DepthRangeFar = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNFar); + state.DepthRangeNear = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNNear); + state.DepthRangeFar = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNFar); } - private void SetStencil(GalPipelineState State) + private void SetStencil(GalPipelineState state) { - State.StencilTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.StencilEnable); + state.StencilTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.StencilEnable); - if (State.StencilTestEnabled) + if (state.StencilTestEnabled) { - State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc); - State.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef); - State.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask); - State.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail); - State.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail); - State.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass); - State.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask); - - State.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc); - State.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef); - State.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask); - State.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail); - State.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail); - State.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass); - State.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask); + state.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc); + state.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef); + state.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask); + state.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail); + state.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail); + state.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass); + state.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask); + + state.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc); + state.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef); + state.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask); + state.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail); + state.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail); + state.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass); + state.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask); } } - private void SetScissor(GalPipelineState State) + private void SetScissor(GalPipelineState state) { int count = 0; - for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++) + for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++) { - State.ScissorTestEnabled[Index] = ReadRegisterBool(NvGpuEngine3dReg.ScissorEnable + Index * 4); + state.ScissorTestEnabled[index] = ReadRegisterBool(NvGpuEngine3dReg.ScissorEnable + index * 4); - if (State.ScissorTestEnabled[Index]) + if (state.ScissorTestEnabled[index]) { - uint ScissorHorizontal = (uint)ReadRegister(NvGpuEngine3dReg.ScissorHorizontal + Index * 4); - uint ScissorVertical = (uint)ReadRegister(NvGpuEngine3dReg.ScissorVertical + Index * 4); + uint scissorHorizontal = (uint)ReadRegister(NvGpuEngine3dReg.ScissorHorizontal + index * 4); + uint scissorVertical = (uint)ReadRegister(NvGpuEngine3dReg.ScissorVertical + index * 4); - int left = (int)(ScissorHorizontal & 0xFFFF); // Left, lower 16 bits - int right = (int)(ScissorHorizontal >> 16); // Right, upper 16 bits + int left = (int)(scissorHorizontal & 0xFFFF); // Left, lower 16 bits + int right = (int)(scissorHorizontal >> 16); // Right, upper 16 bits - int bottom = (int)(ScissorVertical & 0xFFFF); // Bottom, lower 16 bits - int top = (int)(ScissorVertical >> 16); // Top, upper 16 bits + int bottom = (int)(scissorVertical & 0xFFFF); // Bottom, lower 16 bits + int top = (int)(scissorVertical >> 16); // Top, upper 16 bits int width = Math.Abs(right - left); int height = Math.Abs(top - bottom); - // If the scissor test covers the whole possible viewport, i.e. uninititalized, disable scissor test + // If the scissor test covers the whole possible viewport, i.e. uninitialized, disable scissor test if ((width > NvGpu.MaxViewportSize && height > NvGpu.MaxViewportSize) || width <= 0 || height <= 0) { - State.ScissorTestEnabled[Index] = false; + state.ScissorTestEnabled[index] = false; continue; } @@ -460,10 +459,10 @@ namespace Ryujinx.Graphics.Graphics3d count++; // Flip X - if (State.FlipX == -1) + if (state.FlipX == -1) { - left = ViewportX1 - (left - ViewportX0); - right = ViewportX1 - (right - ViewportX0); + left = _viewportX1 - (left - _viewportX0); + right = _viewportX1 - (right - _viewportX0); } // Ensure X is in the right order @@ -475,10 +474,10 @@ namespace Ryujinx.Graphics.Graphics3d } // Flip Y - if (State.FlipY == -1) + if (state.FlipY == -1) { - bottom = ViewportY1 - (bottom - ViewportY0); - top = ViewportY1 - (top - ViewportY0); + bottom = _viewportY1 - (bottom - _viewportY0); + top = _viewportY1 - (top - _viewportY0); } // Ensure Y is in the right order @@ -490,102 +489,102 @@ namespace Ryujinx.Graphics.Graphics3d } // Handle out of active viewport dimensions - left = Math.Clamp(left, ViewportX0, ViewportX1); - right = Math.Clamp(right, ViewportX0, ViewportX1); - top = Math.Clamp(top, ViewportY0, ViewportY1); - bottom = Math.Clamp(bottom, ViewportY0, ViewportY1); + left = Math.Clamp(left, _viewportX0, _viewportX1); + right = Math.Clamp(right, _viewportX0, _viewportX1); + top = Math.Clamp(top, _viewportY0, _viewportY1); + bottom = Math.Clamp(bottom, _viewportY0, _viewportY1); // Save values to state - State.ScissorTestX[Index] = left; - State.ScissorTestY[Index] = bottom; + state.ScissorTestX[index] = left; + state.ScissorTestY[index] = bottom; - State.ScissorTestWidth[Index] = right - left; - State.ScissorTestHeight[Index] = top - bottom; + state.ScissorTestWidth[index] = right - left; + state.ScissorTestHeight[index] = top - bottom; } } - State.ScissorTestCount = count; + state.ScissorTestCount = count; } - private void SetBlending(GalPipelineState State) + private void SetBlending(GalPipelineState state) { - bool BlendIndependent = ReadRegisterBool(NvGpuEngine3dReg.BlendIndependent); + bool blendIndependent = ReadRegisterBool(NvGpuEngine3dReg.BlendIndependent); - State.BlendIndependent = BlendIndependent; + state.BlendIndependent = blendIndependent; - for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++) + for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++) { - if (BlendIndependent) + if (blendIndependent) { - State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable + Index); + state.Blends[index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable + index); - if (State.Blends[Index].Enabled) + if (state.Blends[index].Enabled) { - State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.IBlendNSeparateAlpha + Index * 8); - - State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8); - State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8); - State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8); - State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8); - State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8); - State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8); + state.Blends[index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.IBlendNSeparateAlpha + index * 8); + + state.Blends[index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationRgb + index * 8); + state.Blends[index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcRgb + index * 8); + state.Blends[index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstRgb + index * 8); + state.Blends[index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationAlpha + index * 8); + state.Blends[index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcAlpha + index * 8); + state.Blends[index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstAlpha + index * 8); } } else { //It seems that even when independent blend is disabled, the first IBlend enable //register is still set to indicate whenever blend is enabled or not (?). - State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable); + state.Blends[index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable); - if (State.Blends[Index].Enabled) + if (state.Blends[index].Enabled) { - State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.BlendSeparateAlpha); - - State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationRgb); - State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcRgb); - State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstRgb); - State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationAlpha); - State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcAlpha); - State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstAlpha); + state.Blends[index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.BlendSeparateAlpha); + + state.Blends[index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationRgb); + state.Blends[index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcRgb); + state.Blends[index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstRgb); + state.Blends[index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationAlpha); + state.Blends[index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcAlpha); + state.Blends[index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstAlpha); } } } } - private GalBlendEquation ReadBlendEquation(NvGpuEngine3dReg Register) + private GalBlendEquation ReadBlendEquation(NvGpuEngine3dReg register) { - return (GalBlendEquation)ReadRegister(Register); + return (GalBlendEquation)ReadRegister(register); } - private GalBlendFactor ReadBlendFactor(NvGpuEngine3dReg Register) + private GalBlendFactor ReadBlendFactor(NvGpuEngine3dReg register) { - return (GalBlendFactor)ReadRegister(Register); + return (GalBlendFactor)ReadRegister(register); } - private void SetColorMask(GalPipelineState State) + private void SetColorMask(GalPipelineState state) { - bool ColorMaskCommon = ReadRegisterBool(NvGpuEngine3dReg.ColorMaskCommon); + bool colorMaskCommon = ReadRegisterBool(NvGpuEngine3dReg.ColorMaskCommon); - State.ColorMaskCommon = ColorMaskCommon; + state.ColorMaskCommon = colorMaskCommon; - for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++) + for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++) { - int ColorMask = ReadRegister(NvGpuEngine3dReg.ColorMaskN + (ColorMaskCommon ? 0 : Index)); + int colorMask = ReadRegister(NvGpuEngine3dReg.ColorMaskN + (colorMaskCommon ? 0 : index)); - State.ColorMasks[Index].Red = ((ColorMask >> 0) & 0xf) != 0; - State.ColorMasks[Index].Green = ((ColorMask >> 4) & 0xf) != 0; - State.ColorMasks[Index].Blue = ((ColorMask >> 8) & 0xf) != 0; - State.ColorMasks[Index].Alpha = ((ColorMask >> 12) & 0xf) != 0; + state.ColorMasks[index].Red = ((colorMask >> 0) & 0xf) != 0; + state.ColorMasks[index].Green = ((colorMask >> 4) & 0xf) != 0; + state.ColorMasks[index].Blue = ((colorMask >> 8) & 0xf) != 0; + state.ColorMasks[index].Alpha = ((colorMask >> 12) & 0xf) != 0; } } - private void SetPrimitiveRestart(GalPipelineState State) + private void SetPrimitiveRestart(GalPipelineState state) { - State.PrimitiveRestartEnabled = ReadRegisterBool(NvGpuEngine3dReg.PrimRestartEnable); + state.PrimitiveRestartEnabled = ReadRegisterBool(NvGpuEngine3dReg.PrimRestartEnable); - if (State.PrimitiveRestartEnabled) + if (state.PrimitiveRestartEnabled) { - State.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex); + state.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex); } } @@ -594,461 +593,461 @@ namespace Ryujinx.Graphics.Graphics3d //Commercial games do not seem to //bool SeparateFragData = ReadRegisterBool(NvGpuEngine3dReg.RTSeparateFragData); - uint Control = (uint)(ReadRegister(NvGpuEngine3dReg.RTControl)); + uint control = (uint)(ReadRegister(NvGpuEngine3dReg.RtControl)); - uint Count = Control & 0xf; + uint count = control & 0xf; - if (Count > 0) + if (count > 0) { - int[] Map = new int[Count]; + int[] map = new int[count]; - for (int Index = 0; Index < Count; Index++) + for (int index = 0; index < count; index++) { - int Shift = 4 + Index * 3; + int shift = 4 + index * 3; - Map[Index] = (int)((Control >> Shift) & 7); + map[index] = (int)((control >> shift) & 7); } - Gpu.Renderer.RenderTarget.SetMap(Map); + _gpu.Renderer.RenderTarget.SetMap(map); } else { - Gpu.Renderer.RenderTarget.SetMap(null); + _gpu.Renderer.RenderTarget.SetMap(null); } } - private void UploadTextures(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) + private void UploadTextures(NvGpuVmm vmm, GalPipelineState state, long[] keys) { - long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + long baseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); + int textureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); - List<(long, GalImage, GalTextureSampler)> UnboundTextures = new List<(long, GalImage, GalTextureSampler)>(); + List<(long, GalImage, GalTextureSampler)> unboundTextures = new List<(long, GalImage, GalTextureSampler)>(); - for (int Index = 0; Index < Keys.Length; Index++) + for (int index = 0; index < keys.Length; index++) { - foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index])) + foreach (ShaderDeclInfo declInfo in _gpu.Renderer.Shader.GetTextureUsage(keys[index])) { - long Position; + long position; - if (DeclInfo.IsCb) + if (declInfo.IsCb) { - Position = ConstBuffers[Index][DeclInfo.Cbuf].Position; + position = _constBuffers[index][declInfo.Cbuf].Position; } else { - Position = ConstBuffers[Index][TextureCbIndex].Position; + position = _constBuffers[index][textureCbIndex].Position; } - int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4); + int textureHandle = vmm.ReadInt32(position + declInfo.Index * 4); - UnboundTextures.Add(UploadTexture(Vmm, TextureHandle)); + unboundTextures.Add(UploadTexture(vmm, textureHandle)); } } - for (int Index = 0; Index < UnboundTextures.Count; Index++) + for (int index = 0; index < unboundTextures.Count; index++) { - (long Key, GalImage Image, GalTextureSampler Sampler) = UnboundTextures[Index]; + (long key, GalImage image, GalTextureSampler sampler) = unboundTextures[index]; - if (Key == 0) + if (key == 0) { continue; } - Gpu.Renderer.Texture.Bind(Key, Index, Image); - Gpu.Renderer.Texture.SetSampler(Image, Sampler); + _gpu.Renderer.Texture.Bind(key, index, image); + _gpu.Renderer.Texture.SetSampler(image, sampler); } } - private (long, GalImage, GalTextureSampler) UploadTexture(NvGpuVmm Vmm, int TextureHandle) + private (long, GalImage, GalTextureSampler) UploadTexture(NvGpuVmm vmm, int textureHandle) { - if (TextureHandle == 0) + if (textureHandle == 0) { //FIXME: Some games like puyo puyo will use handles with the value 0. //This is a bug, most likely caused by sync issues. return (0, default(GalImage), default(GalTextureSampler)); } - bool LinkedTsc = ReadRegisterBool(NvGpuEngine3dReg.LinkedTsc); + bool linkedTsc = ReadRegisterBool(NvGpuEngine3dReg.LinkedTsc); - int TicIndex = (TextureHandle >> 0) & 0xfffff; + int ticIndex = (textureHandle >> 0) & 0xfffff; - int TscIndex = LinkedTsc ? TicIndex : (TextureHandle >> 20) & 0xfff; + int tscIndex = linkedTsc ? ticIndex : (textureHandle >> 20) & 0xfff; - long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); - long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); + long ticPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); + long tscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); - TicPosition += TicIndex * 0x20; - TscPosition += TscIndex * 0x20; + ticPosition += ticIndex * 0x20; + tscPosition += tscIndex * 0x20; - GalImage Image = TextureFactory.MakeTexture(Vmm, TicPosition); + GalImage image = TextureFactory.MakeTexture(vmm, ticPosition); - GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); + GalTextureSampler sampler = TextureFactory.MakeSampler(_gpu, vmm, tscPosition); - long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; + long key = vmm.ReadInt64(ticPosition + 4) & 0xffffffffffff; - if (Image.Layout == GalMemoryLayout.BlockLinear) + if (image.Layout == GalMemoryLayout.BlockLinear) { - Key &= ~0x1ffL; + key &= ~0x1ffL; } - else if (Image.Layout == GalMemoryLayout.Pitch) + else if (image.Layout == GalMemoryLayout.Pitch) { - Key &= ~0x1fL; + key &= ~0x1fL; } - Key = Vmm.GetPhysicalAddress(Key); + key = vmm.GetPhysicalAddress(key); - if (Key == -1) + if (key == -1) { //FIXME: Shouldn't ignore invalid addresses. return (0, default(GalImage), default(GalTextureSampler)); } - Gpu.ResourceManager.SendTexture(Vmm, Key, Image); + _gpu.ResourceManager.SendTexture(vmm, key, image); - return (Key, Image, Sampler); + return (key, image, sampler); } - private void UploadConstBuffers(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) + private void UploadConstBuffers(NvGpuVmm vmm, GalPipelineState state, long[] keys) { - for (int Stage = 0; Stage < Keys.Length; Stage++) + for (int stage = 0; stage < keys.Length; stage++) { - foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetConstBufferUsage(Keys[Stage])) + foreach (ShaderDeclInfo declInfo in _gpu.Renderer.Shader.GetConstBufferUsage(keys[stage])) { - ConstBuffer Cb = ConstBuffers[Stage][DeclInfo.Cbuf]; + ConstBuffer cb = _constBuffers[stage][declInfo.Cbuf]; - if (!Cb.Enabled) + if (!cb.Enabled) { continue; } - long Key = Vmm.GetPhysicalAddress(Cb.Position); + long key = vmm.GetPhysicalAddress(cb.Position); - if (Gpu.ResourceManager.MemoryRegionModified(Vmm, Key, Cb.Size, NvGpuBufferType.ConstBuffer)) + if (_gpu.ResourceManager.MemoryRegionModified(vmm, key, cb.Size, NvGpuBufferType.ConstBuffer)) { - if (Vmm.TryGetHostAddress(Cb.Position, Cb.Size, out IntPtr CbPtr)) + if (vmm.TryGetHostAddress(cb.Position, cb.Size, out IntPtr cbPtr)) { - Gpu.Renderer.Buffer.SetData(Key, Cb.Size, CbPtr); + _gpu.Renderer.Buffer.SetData(key, cb.Size, cbPtr); } else { - Gpu.Renderer.Buffer.SetData(Key, Vmm.ReadBytes(Cb.Position, Cb.Size)); + _gpu.Renderer.Buffer.SetData(key, vmm.ReadBytes(cb.Position, cb.Size)); } } - State.ConstBufferKeys[Stage][DeclInfo.Cbuf] = Key; + state.ConstBufferKeys[stage][declInfo.Cbuf] = key; } } } - private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State) + private void UploadVertexArrays(NvGpuVmm vmm, GalPipelineState state) { - long IbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); + long ibPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); - long IboKey = Vmm.GetPhysicalAddress(IbPosition); + long iboKey = vmm.GetPhysicalAddress(ibPosition); - int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); - int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); - int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); + int indexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); + int indexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); + int primCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); - GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); + GalPrimitiveType primType = (GalPrimitiveType)(primCtrl & 0xffff); - GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt; + GalIndexFormat indexFormat = (GalIndexFormat)indexEntryFmt; - int IndexEntrySize = 1 << IndexEntryFmt; + int indexEntrySize = 1 << indexEntryFmt; - if (IndexEntrySize > 4) + if (indexEntrySize > 4) { - throw new InvalidOperationException("Invalid index entry size \"" + IndexEntrySize + "\"!"); + throw new InvalidOperationException("Invalid index entry size \"" + indexEntrySize + "\"!"); } - if (IndexCount != 0) + if (indexCount != 0) { - int IbSize = IndexCount * IndexEntrySize; + int ibSize = indexCount * indexEntrySize; - bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize); + bool iboCached = _gpu.Renderer.Rasterizer.IsIboCached(iboKey, (uint)ibSize); - bool UsesLegacyQuads = - PrimType == GalPrimitiveType.Quads || - PrimType == GalPrimitiveType.QuadStrip; + bool usesLegacyQuads = + primType == GalPrimitiveType.Quads || + primType == GalPrimitiveType.QuadStrip; - if (!IboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index)) + if (!iboCached || _gpu.ResourceManager.MemoryRegionModified(vmm, iboKey, (uint)ibSize, NvGpuBufferType.Index)) { - if (!UsesLegacyQuads) + if (!usesLegacyQuads) { - if (Vmm.TryGetHostAddress(IbPosition, IbSize, out IntPtr IbPtr)) + if (vmm.TryGetHostAddress(ibPosition, ibSize, out IntPtr ibPtr)) { - Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, IbPtr); + _gpu.Renderer.Rasterizer.CreateIbo(iboKey, ibSize, ibPtr); } else { - Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Vmm.ReadBytes(IbPosition, IbSize)); + _gpu.Renderer.Rasterizer.CreateIbo(iboKey, ibSize, vmm.ReadBytes(ibPosition, ibSize)); } } else { - byte[] Buffer = Vmm.ReadBytes(IbPosition, IbSize); + byte[] buffer = vmm.ReadBytes(ibPosition, ibSize); - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - Buffer = QuadHelper.ConvertQuadsToTris(Buffer, IndexEntrySize, IndexCount); + buffer = QuadHelper.ConvertQuadsToTris(buffer, indexEntrySize, indexCount); } else /* if (PrimType == GalPrimitiveType.QuadStrip) */ { - Buffer = QuadHelper.ConvertQuadStripToTris(Buffer, IndexEntrySize, IndexCount); + buffer = QuadHelper.ConvertQuadStripToTris(buffer, indexEntrySize, indexCount); } - Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Buffer); + _gpu.Renderer.Rasterizer.CreateIbo(iboKey, ibSize, buffer); } } - if (!UsesLegacyQuads) + if (!usesLegacyQuads) { - Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat); + _gpu.Renderer.Rasterizer.SetIndexArray(ibSize, indexFormat); } else { - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadsToTris(IbSize), IndexFormat); + _gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadsToTris(ibSize), indexFormat); } else /* if (PrimType == GalPrimitiveType.QuadStrip) */ { - Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadStripToTris(IbSize), IndexFormat); + _gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadStripToTris(ibSize), indexFormat); } } } - List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32]; + List<GalVertexAttrib>[] attribs = new List<GalVertexAttrib>[32]; - for (int Attr = 0; Attr < 16; Attr++) + for (int attr = 0; attr < 16; attr++) { - int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr); + int packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + attr); - int ArrayIndex = Packed & 0x1f; + int arrayIndex = packed & 0x1f; - if (Attribs[ArrayIndex] == null) + if (attribs[arrayIndex] == null) { - Attribs[ArrayIndex] = new List<GalVertexAttrib>(); + attribs[arrayIndex] = new List<GalVertexAttrib>(); } - long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + ArrayIndex * 4); + long vbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + arrayIndex * 4); - if (VbPosition == 0) + if (vbPosition == 0) { continue; } - bool IsConst = ((Packed >> 6) & 1) != 0; + bool isConst = ((packed >> 6) & 1) != 0; - int Offset = (Packed >> 7) & 0x3fff; + int offset = (packed >> 7) & 0x3fff; - GalVertexAttribSize Size = (GalVertexAttribSize)((Packed >> 21) & 0x3f); - GalVertexAttribType Type = (GalVertexAttribType)((Packed >> 27) & 0x7); + GalVertexAttribSize size = (GalVertexAttribSize)((packed >> 21) & 0x3f); + GalVertexAttribType type = (GalVertexAttribType)((packed >> 27) & 0x7); - bool IsRgba = ((Packed >> 31) & 1) != 0; + bool isRgba = ((packed >> 31) & 1) != 0; // Check vertex array is enabled to avoid out of bounds exception when reading bytes - bool Enable = (ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + ArrayIndex * 4) & 0x1000) != 0; + bool enable = (ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + arrayIndex * 4) & 0x1000) != 0; //Note: 16 is the maximum size of an attribute, //having a component size of 32-bits with 4 elements (a vec4). - if (Enable) + if (enable) { - byte[] Data = Vmm.ReadBytes(VbPosition + Offset, 16); + byte[] data = vmm.ReadBytes(vbPosition + offset, 16); - Attribs[ArrayIndex].Add(new GalVertexAttrib(Attr, IsConst, Offset, Data, Size, Type, IsRgba)); + attribs[arrayIndex].Add(new GalVertexAttrib(attr, isConst, offset, data, size, type, isRgba)); } } - State.VertexBindings = new GalVertexBinding[32]; + state.VertexBindings = new GalVertexBinding[32]; - for (int Index = 0; Index < 32; Index++) + for (int index = 0; index < 32; index++) { - if (Attribs[Index] == null) + if (attribs[index] == null) { continue; } - int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); + int control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + index * 4); - bool Enable = (Control & 0x1000) != 0; + bool enable = (control & 0x1000) != 0; - if (!Enable) + if (!enable) { continue; } - long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); - long VbEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); + long vbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + index * 4); + long vbEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + index * 2); - int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4); + int vertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + index * 4); - bool Instanced = ReadRegisterBool(NvGpuEngine3dReg.VertexArrayNInstance + Index); + bool instanced = ReadRegisterBool(NvGpuEngine3dReg.VertexArrayNInstance + index); - int Stride = Control & 0xfff; + int stride = control & 0xfff; - if (Instanced && VertexDivisor != 0) + if (instanced && vertexDivisor != 0) { - VbPosition += Stride * (CurrentInstance / VertexDivisor); + vbPosition += stride * (_currentInstance / vertexDivisor); } - if (VbPosition > VbEndPos) + if (vbPosition > vbEndPos) { //Instance is invalid, ignore the draw call continue; } - long VboKey = Vmm.GetPhysicalAddress(VbPosition); + long vboKey = vmm.GetPhysicalAddress(vbPosition); - long VbSize = (VbEndPos - VbPosition) + 1; - int ModifiedVbSize = (int)VbSize; + long vbSize = (vbEndPos - vbPosition) + 1; + int modifiedVbSize = (int)vbSize; // If quads convert size to triangle length - if (Stride == 0) + if (stride == 0) { - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - ModifiedVbSize = QuadHelper.ConvertSizeQuadsToTris(ModifiedVbSize); + modifiedVbSize = QuadHelper.ConvertSizeQuadsToTris(modifiedVbSize); } - else if (PrimType == GalPrimitiveType.QuadStrip) + else if (primType == GalPrimitiveType.QuadStrip) { - ModifiedVbSize = QuadHelper.ConvertSizeQuadStripToTris(ModifiedVbSize); + modifiedVbSize = QuadHelper.ConvertSizeQuadStripToTris(modifiedVbSize); } } - bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, ModifiedVbSize); + bool vboCached = _gpu.Renderer.Rasterizer.IsVboCached(vboKey, modifiedVbSize); - if (!VboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex)) + if (!vboCached || _gpu.ResourceManager.MemoryRegionModified(vmm, vboKey, vbSize, NvGpuBufferType.Vertex)) { - if ((PrimType == GalPrimitiveType.Quads | PrimType == GalPrimitiveType.QuadStrip) && Stride != 0) + if ((primType == GalPrimitiveType.Quads | primType == GalPrimitiveType.QuadStrip) && stride != 0) { // Convert quad buffer to triangles - byte[] data = Vmm.ReadBytes(VbPosition, VbSize); + byte[] data = vmm.ReadBytes(vbPosition, vbSize); - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - data = QuadHelper.ConvertQuadsToTris(data, Stride, (int)(VbSize / Stride)); + data = QuadHelper.ConvertQuadsToTris(data, stride, (int)(vbSize / stride)); } else { - data = QuadHelper.ConvertQuadStripToTris(data, Stride, (int)(VbSize / Stride)); + data = QuadHelper.ConvertQuadStripToTris(data, stride, (int)(vbSize / stride)); } - Gpu.Renderer.Rasterizer.CreateVbo(VboKey, data); + _gpu.Renderer.Rasterizer.CreateVbo(vboKey, data); } - else if (Vmm.TryGetHostAddress(VbPosition, VbSize, out IntPtr VbPtr)) + else if (vmm.TryGetHostAddress(vbPosition, vbSize, out IntPtr vbPtr)) { - Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, VbPtr); + _gpu.Renderer.Rasterizer.CreateVbo(vboKey, (int)vbSize, vbPtr); } else { - Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Vmm.ReadBytes(VbPosition, VbSize)); + _gpu.Renderer.Rasterizer.CreateVbo(vboKey, vmm.ReadBytes(vbPosition, vbSize)); } } - State.VertexBindings[Index].Enabled = true; - State.VertexBindings[Index].Stride = Stride; - State.VertexBindings[Index].VboKey = VboKey; - State.VertexBindings[Index].Instanced = Instanced; - State.VertexBindings[Index].Divisor = VertexDivisor; - State.VertexBindings[Index].Attribs = Attribs[Index].ToArray(); + state.VertexBindings[index].Enabled = true; + state.VertexBindings[index].Stride = stride; + state.VertexBindings[index].VboKey = vboKey; + state.VertexBindings[index].Instanced = instanced; + state.VertexBindings[index].Divisor = vertexDivisor; + state.VertexBindings[index].Attribs = attribs[index].ToArray(); } } - private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State) + private void DispatchRender(NvGpuVmm vmm, GalPipelineState state) { - int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); - int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); + int indexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); + int primCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); - GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); + GalPrimitiveType primType = (GalPrimitiveType)(primCtrl & 0xffff); - bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0; - bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0; + bool instanceNext = ((primCtrl >> 26) & 1) != 0; + bool instanceCont = ((primCtrl >> 27) & 1) != 0; - if (InstanceNext && InstanceCont) + if (instanceNext && instanceCont) { throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time"); } - if (InstanceNext) + if (instanceNext) { - CurrentInstance++; + _currentInstance++; } - else if (!InstanceCont) + else if (!instanceCont) { - CurrentInstance = 0; + _currentInstance = 0; } - State.Instance = CurrentInstance; + state.Instance = _currentInstance; - Gpu.Renderer.Pipeline.Bind(State); + _gpu.Renderer.Pipeline.Bind(state); - Gpu.Renderer.RenderTarget.Bind(); + _gpu.Renderer.RenderTarget.Bind(); - if (IndexCount != 0) + if (indexCount != 0) { - int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); - int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); - int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); + int indexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); + int indexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); + int vertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); - long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); + long indexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); - long IboKey = Vmm.GetPhysicalAddress(IndexPosition); + long iboKey = vmm.GetPhysicalAddress(indexPosition); //Quad primitive types were deprecated on OpenGL 3.x, //they are converted to a triangles index buffer on IB creation, //so we should use the triangles type here too. - if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip) + if (primType == GalPrimitiveType.Quads || primType == GalPrimitiveType.QuadStrip) { //Note: We assume that index first points to the first //vertex of a quad, if it points to the middle of a //quad (First % 4 != 0 for Quads) then it will not work properly. - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - IndexFirst = QuadHelper.ConvertSizeQuadsToTris(IndexFirst); + indexFirst = QuadHelper.ConvertSizeQuadsToTris(indexFirst); } else // QuadStrip { - IndexFirst = QuadHelper.ConvertSizeQuadStripToTris(IndexFirst); + indexFirst = QuadHelper.ConvertSizeQuadStripToTris(indexFirst); } - PrimType = GalPrimitiveType.Triangles; + primType = GalPrimitiveType.Triangles; } - Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType); + _gpu.Renderer.Rasterizer.DrawElements(iboKey, indexFirst, vertexBase, primType); } else { - int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); - int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); + int vertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); + int vertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); //Quad primitive types were deprecated on OpenGL 3.x, //they are converted to a triangles index buffer on IB creation, //so we should use the triangles type here too. - if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip) + if (primType == GalPrimitiveType.Quads || primType == GalPrimitiveType.QuadStrip) { //Note: We assume that index first points to the first //vertex of a quad, if it points to the middle of a //quad (First % 4 != 0 for Quads) then it will not work properly. - if (PrimType == GalPrimitiveType.Quads) + if (primType == GalPrimitiveType.Quads) { - VertexFirst = QuadHelper.ConvertSizeQuadsToTris(VertexFirst); + vertexFirst = QuadHelper.ConvertSizeQuadsToTris(vertexFirst); } else // QuadStrip { - VertexFirst = QuadHelper.ConvertSizeQuadStripToTris(VertexFirst); + vertexFirst = QuadHelper.ConvertSizeQuadStripToTris(vertexFirst); } - PrimType = GalPrimitiveType.Triangles; - VertexCount = QuadHelper.ConvertSizeQuadsToTris(VertexCount); + primType = GalPrimitiveType.Triangles; + vertexCount = QuadHelper.ConvertSizeQuadsToTris(vertexCount); } - Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); + _gpu.Renderer.Rasterizer.DrawArrays(vertexFirst, vertexCount, primType); } // Reset pipeline for host OpenGL calls - Gpu.Renderer.Pipeline.Unbind(State); + _gpu.Renderer.Pipeline.Unbind(state); //Is the GPU really clearing those registers after draw? WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0); @@ -1062,115 +1061,115 @@ namespace Ryujinx.Graphics.Graphics3d WriteCounterAndTimestamp } - private void QueryControl(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void QueryControl(NvGpuVmm vmm, GpuMethodCall methCall) { - WriteRegister(MethCall); + WriteRegister(methCall); - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); + long position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); - int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; - int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; + int seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; + int ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; - QueryMode Mode = (QueryMode)(Ctrl & 3); + QueryMode mode = (QueryMode)(ctrl & 3); - switch (Mode) + switch (mode) { - case QueryMode.WriteSeq: Vmm.WriteInt32(Position, Seq); break; + case QueryMode.WriteSeq: vmm.WriteInt32(position, seq); break; case QueryMode.WriteCounterAndTimestamp: { //TODO: Implement counters. - long Counter = 1; + long counter = 1; - long Timestamp = PerformanceCounter.ElapsedMilliseconds; + long timestamp = PerformanceCounter.ElapsedMilliseconds; - Vmm.WriteInt64(Position + 0, Counter); - Vmm.WriteInt64(Position + 8, Timestamp); + vmm.WriteInt64(position + 0, counter); + vmm.WriteInt64(position + 8, timestamp); break; } } } - private void CbData(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void CbData(NvGpuVmm vmm, GpuMethodCall methCall) { - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + long position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); + int offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); - Vmm.WriteInt32(Position + Offset, MethCall.Argument); + vmm.WriteInt32(position + offset, methCall.Argument); - WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset + 4); + WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, offset + 4); - Gpu.ResourceManager.ClearPbCache(NvGpuBufferType.ConstBuffer); + _gpu.ResourceManager.ClearPbCache(NvGpuBufferType.ConstBuffer); } - private void CbBind(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void CbBind(NvGpuVmm vmm, GpuMethodCall methCall) { - int Stage = (MethCall.Method - 0x904) >> 3; + int stage = (methCall.Method - 0x904) >> 3; - int Index = MethCall.Argument; + int index = methCall.Argument; - bool Enabled = (Index & 1) != 0; + bool enabled = (index & 1) != 0; - Index = (Index >> 4) & 0x1f; + index = (index >> 4) & 0x1f; - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + long position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - long CbKey = Vmm.GetPhysicalAddress(Position); + long cbKey = vmm.GetPhysicalAddress(position); - int Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); + int size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); - if (!Gpu.Renderer.Buffer.IsCached(CbKey, Size)) + if (!_gpu.Renderer.Buffer.IsCached(cbKey, size)) { - Gpu.Renderer.Buffer.Create(CbKey, Size); + _gpu.Renderer.Buffer.Create(cbKey, size); } - ConstBuffer Cb = ConstBuffers[Stage][Index]; + ConstBuffer cb = _constBuffers[stage][index]; - if (Cb.Position != Position || Cb.Enabled != Enabled || Cb.Size != Size) + if (cb.Position != position || cb.Enabled != enabled || cb.Size != size) { - ConstBuffers[Stage][Index].Position = Position; - ConstBuffers[Stage][Index].Enabled = Enabled; - ConstBuffers[Stage][Index].Size = Size; + _constBuffers[stage][index].Position = position; + _constBuffers[stage][index].Enabled = enabled; + _constBuffers[stage][index].Size = size; } } - private float GetFlipSign(NvGpuEngine3dReg Reg) + private float GetFlipSign(NvGpuEngine3dReg reg) { - return MathF.Sign(ReadRegisterFloat(Reg)); + return MathF.Sign(ReadRegisterFloat(reg)); } - private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg) + private long MakeInt64From2xInt32(NvGpuEngine3dReg reg) { return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; + (long)Registers[(int)reg + 0] << 32 | + (uint)Registers[(int)reg + 1]; } - private void WriteRegister(GpuMethodCall MethCall) + private void WriteRegister(GpuMethodCall methCall) { - Registers[MethCall.Method] = MethCall.Argument; + Registers[methCall.Method] = methCall.Argument; } - private int ReadRegister(NvGpuEngine3dReg Reg) + private int ReadRegister(NvGpuEngine3dReg reg) { - return Registers[(int)Reg]; + return Registers[(int)reg]; } - private float ReadRegisterFloat(NvGpuEngine3dReg Reg) + private float ReadRegisterFloat(NvGpuEngine3dReg reg) { - return BitConverter.Int32BitsToSingle(ReadRegister(Reg)); + return BitConverter.Int32BitsToSingle(ReadRegister(reg)); } - private bool ReadRegisterBool(NvGpuEngine3dReg Reg) + private bool ReadRegisterBool(NvGpuEngine3dReg reg) { - return (ReadRegister(Reg) & 1) != 0; + return (ReadRegister(reg) & 1) != 0; } - private void WriteRegister(NvGpuEngine3dReg Reg, int Value) + private void WriteRegister(NvGpuEngine3dReg reg, int value) { - Registers[(int)Reg] = Value; + Registers[(int)reg] = value; } } } diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuEngine3dReg.cs b/Ryujinx.Graphics/Graphics3d/NvGpuEngine3dReg.cs index 91346464..c6596a30 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuEngine3dReg.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuEngine3dReg.cs @@ -32,13 +32,13 @@ namespace Ryujinx.Graphics.Graphics3d StencilBackMask = 0x3d6, StencilBackFuncMask = 0x3d7, ColorMaskCommon = 0x3e4, - RTSeparateFragData = 0x3eb, + RtSeparateFragData = 0x3eb, ZetaAddress = 0x3f8, ZetaFormat = 0x3fa, ZetaBlockDimensions = 0x3fb, ZetaLayerStride = 0x3fc, VertexAttribNFormat = 0x458, - RTControl = 0x487, + RtControl = 0x487, ZetaHoriz = 0x48a, ZetaVert = 0x48b, ZetaArrayMode = 0x48c, diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuEngineM2mf.cs b/Ryujinx.Graphics/Graphics3d/NvGpuEngineM2mf.cs index 2f1df3d3..c0f444c3 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuEngineM2mf.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuEngineM2mf.cs @@ -1,4 +1,3 @@ -using Ryujinx.Common.Logging; using Ryujinx.Graphics.Memory; using Ryujinx.Graphics.Texture; using System.Collections.Generic; @@ -9,188 +8,188 @@ namespace Ryujinx.Graphics.Graphics3d { public int[] Registers { get; private set; } - private NvGpu Gpu; + private NvGpu _gpu; - private Dictionary<int, NvGpuMethod> Methods; + private Dictionary<int, NvGpuMethod> _methods; - public NvGpuEngineM2mf(NvGpu Gpu) + public NvGpuEngineM2mf(NvGpu gpu) { - this.Gpu = Gpu; + _gpu = gpu; Registers = new int[0x1d6]; - Methods = new Dictionary<int, NvGpuMethod>(); + _methods = new Dictionary<int, NvGpuMethod>(); - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + void AddMethod(int meth, int count, int stride, NvGpuMethod method) { - while (Count-- > 0) + while (count-- > 0) { - Methods.Add(Meth, Method); + _methods.Add(meth, method); - Meth += Stride; + meth += stride; } } AddMethod(0xc0, 1, 1, Execute); } - public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method)) + if (_methods.TryGetValue(methCall.Method, out NvGpuMethod method)) { - Method(Vmm, MethCall); + method(vmm, methCall); } else { - WriteRegister(MethCall); + WriteRegister(methCall); } } - private void Execute(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void Execute(NvGpuVmm vmm, GpuMethodCall methCall) { //TODO: Some registers and copy modes are still not implemented. - int Control = MethCall.Argument; + int control = methCall.Argument; - bool SrcLinear = ((Control >> 7) & 1) != 0; - bool DstLinear = ((Control >> 8) & 1) != 0; - bool Copy2d = ((Control >> 9) & 1) != 0; + bool srcLinear = ((control >> 7) & 1) != 0; + bool dstLinear = ((control >> 8) & 1) != 0; + bool copy2D = ((control >> 9) & 1) != 0; - long SrcAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.DstAddress); + long srcAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.SrcAddress); + long dstAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.DstAddress); - int SrcPitch = ReadRegister(NvGpuEngineM2mfReg.SrcPitch); - int DstPitch = ReadRegister(NvGpuEngineM2mfReg.DstPitch); + int srcPitch = ReadRegister(NvGpuEngineM2mfReg.SrcPitch); + int dstPitch = ReadRegister(NvGpuEngineM2mfReg.DstPitch); - int XCount = ReadRegister(NvGpuEngineM2mfReg.XCount); - int YCount = ReadRegister(NvGpuEngineM2mfReg.YCount); + int xCount = ReadRegister(NvGpuEngineM2mfReg.XCount); + int yCount = ReadRegister(NvGpuEngineM2mfReg.YCount); - int Swizzle = ReadRegister(NvGpuEngineM2mfReg.Swizzle); + int swizzle = ReadRegister(NvGpuEngineM2mfReg.Swizzle); - int DstBlkDim = ReadRegister(NvGpuEngineM2mfReg.DstBlkDim); - int DstSizeX = ReadRegister(NvGpuEngineM2mfReg.DstSizeX); - int DstSizeY = ReadRegister(NvGpuEngineM2mfReg.DstSizeY); - int DstSizeZ = ReadRegister(NvGpuEngineM2mfReg.DstSizeZ); - int DstPosXY = ReadRegister(NvGpuEngineM2mfReg.DstPosXY); - int DstPosZ = ReadRegister(NvGpuEngineM2mfReg.DstPosZ); + int dstBlkDim = ReadRegister(NvGpuEngineM2mfReg.DstBlkDim); + int dstSizeX = ReadRegister(NvGpuEngineM2mfReg.DstSizeX); + int dstSizeY = ReadRegister(NvGpuEngineM2mfReg.DstSizeY); + int dstSizeZ = ReadRegister(NvGpuEngineM2mfReg.DstSizeZ); + int dstPosXY = ReadRegister(NvGpuEngineM2mfReg.DstPosXY); + int dstPosZ = ReadRegister(NvGpuEngineM2mfReg.DstPosZ); - int SrcBlkDim = ReadRegister(NvGpuEngineM2mfReg.SrcBlkDim); - int SrcSizeX = ReadRegister(NvGpuEngineM2mfReg.SrcSizeX); - int SrcSizeY = ReadRegister(NvGpuEngineM2mfReg.SrcSizeY); - int SrcSizeZ = ReadRegister(NvGpuEngineM2mfReg.SrcSizeZ); - int SrcPosXY = ReadRegister(NvGpuEngineM2mfReg.SrcPosXY); - int SrcPosZ = ReadRegister(NvGpuEngineM2mfReg.SrcPosZ); + int srcBlkDim = ReadRegister(NvGpuEngineM2mfReg.SrcBlkDim); + int srcSizeX = ReadRegister(NvGpuEngineM2mfReg.SrcSizeX); + int srcSizeY = ReadRegister(NvGpuEngineM2mfReg.SrcSizeY); + int srcSizeZ = ReadRegister(NvGpuEngineM2mfReg.SrcSizeZ); + int srcPosXY = ReadRegister(NvGpuEngineM2mfReg.SrcPosXY); + int srcPosZ = ReadRegister(NvGpuEngineM2mfReg.SrcPosZ); - int SrcCpp = ((Swizzle >> 20) & 7) + 1; - int DstCpp = ((Swizzle >> 24) & 7) + 1; + int srcCpp = ((swizzle >> 20) & 7) + 1; + int dstCpp = ((swizzle >> 24) & 7) + 1; - int DstPosX = (DstPosXY >> 0) & 0xffff; - int DstPosY = (DstPosXY >> 16) & 0xffff; + int dstPosX = (dstPosXY >> 0) & 0xffff; + int dstPosY = (dstPosXY >> 16) & 0xffff; - int SrcPosX = (SrcPosXY >> 0) & 0xffff; - int SrcPosY = (SrcPosXY >> 16) & 0xffff; + int srcPosX = (srcPosXY >> 0) & 0xffff; + int srcPosY = (srcPosXY >> 16) & 0xffff; - int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + int srcBlockHeight = 1 << ((srcBlkDim >> 4) & 0xf); + int dstBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf); - long SrcPA = Vmm.GetPhysicalAddress(SrcAddress); - long DstPA = Vmm.GetPhysicalAddress(DstAddress); + long srcPa = vmm.GetPhysicalAddress(srcAddress); + long dstPa = vmm.GetPhysicalAddress(dstAddress); - if (Copy2d) + if (copy2D) { - if (SrcLinear) + if (srcLinear) { - SrcPosX = SrcPosY = SrcPosZ = 0; + srcPosX = srcPosY = srcPosZ = 0; } - if (DstLinear) + if (dstLinear) { - DstPosX = DstPosY = DstPosZ = 0; + dstPosX = dstPosY = dstPosZ = 0; } - if (SrcLinear && DstLinear) + if (srcLinear && dstLinear) { - for (int Y = 0; Y < YCount; Y++) + for (int y = 0; y < yCount; y++) { - int SrcOffset = (SrcPosY + Y) * SrcPitch + SrcPosX * SrcCpp; - int DstOffset = (DstPosY + Y) * DstPitch + DstPosX * DstCpp; + int srcOffset = (srcPosY + y) * srcPitch + srcPosX * srcCpp; + int dstOffset = (dstPosY + y) * dstPitch + dstPosX * dstCpp; - long Src = SrcPA + (uint)SrcOffset; - long Dst = DstPA + (uint)DstOffset; + long src = srcPa + (uint)srcOffset; + long dst = dstPa + (uint)dstOffset; - Vmm.Memory.CopyBytes(Src, Dst, XCount * SrcCpp); + vmm.Memory.CopyBytes(src, dst, xCount * srcCpp); } } else { - ISwizzle SrcSwizzle; + ISwizzle srcSwizzle; - if (SrcLinear) + if (srcLinear) { - SrcSwizzle = new LinearSwizzle(SrcPitch, SrcCpp, SrcSizeX, SrcSizeY); + srcSwizzle = new LinearSwizzle(srcPitch, srcCpp, srcSizeX, srcSizeY); } else { - SrcSwizzle = new BlockLinearSwizzle( - SrcSizeX, - SrcSizeY, 1, - SrcBlockHeight, 1, - SrcCpp); + srcSwizzle = new BlockLinearSwizzle( + srcSizeX, + srcSizeY, 1, + srcBlockHeight, 1, + srcCpp); } - ISwizzle DstSwizzle; + ISwizzle dstSwizzle; - if (DstLinear) + if (dstLinear) { - DstSwizzle = new LinearSwizzle(DstPitch, DstCpp, SrcSizeX, SrcSizeY); + dstSwizzle = new LinearSwizzle(dstPitch, dstCpp, srcSizeX, srcSizeY); } else { - DstSwizzle = new BlockLinearSwizzle( - DstSizeX, - DstSizeY, 1, - DstBlockHeight, 1, - DstCpp); + dstSwizzle = new BlockLinearSwizzle( + dstSizeX, + dstSizeY, 1, + dstBlockHeight, 1, + dstCpp); } - for (int Y = 0; Y < YCount; Y++) - for (int X = 0; X < XCount; X++) + for (int y = 0; y < yCount; y++) + for (int x = 0; x < xCount; x++) { - int SrcOffset = SrcSwizzle.GetSwizzleOffset(SrcPosX + X, SrcPosY + Y, 0); - int DstOffset = DstSwizzle.GetSwizzleOffset(DstPosX + X, DstPosY + Y, 0); + int srcOffset = srcSwizzle.GetSwizzleOffset(srcPosX + x, srcPosY + y, 0); + int dstOffset = dstSwizzle.GetSwizzleOffset(dstPosX + x, dstPosY + y, 0); - long Src = SrcPA + (uint)SrcOffset; - long Dst = DstPA + (uint)DstOffset; + long src = srcPa + (uint)srcOffset; + long dst = dstPa + (uint)dstOffset; - Vmm.Memory.CopyBytes(Src, Dst, SrcCpp); + vmm.Memory.CopyBytes(src, dst, srcCpp); } } } else { - Vmm.Memory.CopyBytes(SrcPA, DstPA, XCount); + vmm.Memory.CopyBytes(srcPa, dstPa, xCount); } } - private long MakeInt64From2xInt32(NvGpuEngineM2mfReg Reg) + private long MakeInt64From2xInt32(NvGpuEngineM2mfReg reg) { return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; + (long)Registers[(int)reg + 0] << 32 | + (uint)Registers[(int)reg + 1]; } - private void WriteRegister(GpuMethodCall MethCall) + private void WriteRegister(GpuMethodCall methCall) { - Registers[MethCall.Method] = MethCall.Argument; + Registers[methCall.Method] = methCall.Argument; } - private int ReadRegister(NvGpuEngineM2mfReg Reg) + private int ReadRegister(NvGpuEngineM2mfReg reg) { - return Registers[(int)Reg]; + return Registers[(int)reg]; } - private void WriteRegister(NvGpuEngineM2mfReg Reg, int Value) + private void WriteRegister(NvGpuEngineM2mfReg reg, int value) { - Registers[(int)Reg] = Value; + Registers[(int)reg] = value; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuEngineP2mf.cs b/Ryujinx.Graphics/Graphics3d/NvGpuEngineP2mf.cs index 62872ba1..d24f2303 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuEngineP2mf.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuEngineP2mf.cs @@ -1,4 +1,3 @@ -using Ryujinx.Common.Logging; using Ryujinx.Graphics.Memory; using Ryujinx.Graphics.Texture; using System.Collections.Generic; @@ -9,41 +8,41 @@ namespace Ryujinx.Graphics.Graphics3d { public int[] Registers { get; private set; } - private NvGpu Gpu; + private NvGpu _gpu; - private Dictionary<int, NvGpuMethod> Methods; + private Dictionary<int, NvGpuMethod> _methods; - private int CopyStartX; - private int CopyStartY; + private int _copyStartX; + private int _copyStartY; - private int CopyWidth; - private int CopyHeight; - private int CopyGobBlockHeight; + private int _copyWidth; + private int _copyHeight; + private int _copyGobBlockHeight; - private long CopyAddress; + private long _copyAddress; - private int CopyOffset; - private int CopySize; + private int _copyOffset; + private int _copySize; - private bool CopyLinear; + private bool _copyLinear; - private byte[] Buffer; + private byte[] _buffer; - public NvGpuEngineP2mf(NvGpu Gpu) + public NvGpuEngineP2mf(NvGpu gpu) { - this.Gpu = Gpu; + _gpu = gpu; Registers = new int[0x80]; - Methods = new Dictionary<int, NvGpuMethod>(); + _methods = new Dictionary<int, NvGpuMethod>(); - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + void AddMethod(int meth, int count, int stride, NvGpuMethod method) { - while (Count-- > 0) + while (count-- > 0) { - Methods.Add(Meth, Method); + _methods.Add(meth, method); - Meth += Stride; + meth += stride; } } @@ -51,115 +50,115 @@ namespace Ryujinx.Graphics.Graphics3d AddMethod(0x6d, 1, 1, PushData); } - public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method)) + if (_methods.TryGetValue(methCall.Method, out NvGpuMethod method)) { - Method(Vmm, MethCall); + method(vmm, methCall); } else { - WriteRegister(MethCall); + WriteRegister(methCall); } } - private void Execute(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void Execute(NvGpuVmm vmm, GpuMethodCall methCall) { //TODO: Some registers and copy modes are still not implemented. - int Control = MethCall.Argument; + int control = methCall.Argument; - long DstAddress = MakeInt64From2xInt32(NvGpuEngineP2mfReg.DstAddress); + long dstAddress = MakeInt64From2xInt32(NvGpuEngineP2mfReg.DstAddress); - int DstPitch = ReadRegister(NvGpuEngineP2mfReg.DstPitch); - int DstBlkDim = ReadRegister(NvGpuEngineP2mfReg.DstBlockDim); + int dstPitch = ReadRegister(NvGpuEngineP2mfReg.DstPitch); + int dstBlkDim = ReadRegister(NvGpuEngineP2mfReg.DstBlockDim); - int DstX = ReadRegister(NvGpuEngineP2mfReg.DstX); - int DstY = ReadRegister(NvGpuEngineP2mfReg.DstY); + int dstX = ReadRegister(NvGpuEngineP2mfReg.DstX); + int dstY = ReadRegister(NvGpuEngineP2mfReg.DstY); - int DstWidth = ReadRegister(NvGpuEngineP2mfReg.DstWidth); - int DstHeight = ReadRegister(NvGpuEngineP2mfReg.DstHeight); + int dstWidth = ReadRegister(NvGpuEngineP2mfReg.DstWidth); + int dstHeight = ReadRegister(NvGpuEngineP2mfReg.DstHeight); - int LineLengthIn = ReadRegister(NvGpuEngineP2mfReg.LineLengthIn); - int LineCount = ReadRegister(NvGpuEngineP2mfReg.LineCount); + int lineLengthIn = ReadRegister(NvGpuEngineP2mfReg.LineLengthIn); + int lineCount = ReadRegister(NvGpuEngineP2mfReg.LineCount); - CopyLinear = (Control & 1) != 0; + _copyLinear = (control & 1) != 0; - CopyGobBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + _copyGobBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf); - CopyStartX = DstX; - CopyStartY = DstY; + _copyStartX = dstX; + _copyStartY = dstY; - CopyWidth = DstWidth; - CopyHeight = DstHeight; + _copyWidth = dstWidth; + _copyHeight = dstHeight; - CopyAddress = DstAddress; + _copyAddress = dstAddress; - CopyOffset = 0; - CopySize = LineLengthIn * LineCount; + _copyOffset = 0; + _copySize = lineLengthIn * lineCount; - Buffer = new byte[CopySize]; + _buffer = new byte[_copySize]; } - private void PushData(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void PushData(NvGpuVmm vmm, GpuMethodCall methCall) { - if (Buffer == null) + if (_buffer == null) { return; } - for (int Shift = 0; Shift < 32 && CopyOffset < CopySize; Shift += 8, CopyOffset++) + for (int shift = 0; shift < 32 && _copyOffset < _copySize; shift += 8, _copyOffset++) { - Buffer[CopyOffset] = (byte)(MethCall.Argument >> Shift); + _buffer[_copyOffset] = (byte)(methCall.Argument >> shift); } - if (MethCall.IsLastCall) + if (methCall.IsLastCall) { - if (CopyLinear) + if (_copyLinear) { - Vmm.WriteBytes(CopyAddress, Buffer); + vmm.WriteBytes(_copyAddress, _buffer); } else { - BlockLinearSwizzle Swizzle = new BlockLinearSwizzle( - CopyWidth, - CopyHeight, 1, - CopyGobBlockHeight, 1, 1); + BlockLinearSwizzle swizzle = new BlockLinearSwizzle( + _copyWidth, + _copyHeight, 1, + _copyGobBlockHeight, 1, 1); - int SrcOffset = 0; + int srcOffset = 0; - for (int Y = CopyStartY; Y < CopyHeight && SrcOffset < CopySize; Y++) - for (int X = CopyStartX; X < CopyWidth && SrcOffset < CopySize; X++) + for (int y = _copyStartY; y < _copyHeight && srcOffset < _copySize; y++) + for (int x = _copyStartX; x < _copyWidth && srcOffset < _copySize; x++) { - int DstOffset = Swizzle.GetSwizzleOffset(X, Y, 0); + int dstOffset = swizzle.GetSwizzleOffset(x, y, 0); - Vmm.WriteByte(CopyAddress + DstOffset, Buffer[SrcOffset++]); + vmm.WriteByte(_copyAddress + dstOffset, _buffer[srcOffset++]); } } - Buffer = null; + _buffer = null; } } - private long MakeInt64From2xInt32(NvGpuEngineP2mfReg Reg) + private long MakeInt64From2xInt32(NvGpuEngineP2mfReg reg) { return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; + (long)Registers[(int)reg + 0] << 32 | + (uint)Registers[(int)reg + 1]; } - private void WriteRegister(GpuMethodCall MethCall) + private void WriteRegister(GpuMethodCall methCall) { - Registers[MethCall.Method] = MethCall.Argument; + Registers[methCall.Method] = methCall.Argument; } - private int ReadRegister(NvGpuEngineP2mfReg Reg) + private int ReadRegister(NvGpuEngineP2mfReg reg) { - return Registers[(int)Reg]; + return Registers[(int)reg]; } - private void WriteRegister(NvGpuEngineP2mfReg Reg, int Value) + private void WriteRegister(NvGpuEngineP2mfReg reg, int value) { - Registers[(int)Reg] = Value; + Registers[(int)reg] = value; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuFifo.cs b/Ryujinx.Graphics/Graphics3d/NvGpuFifo.cs index f834ade7..25c1a9cd 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuFifo.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuFifo.cs @@ -11,166 +11,166 @@ namespace Ryujinx.Graphics.Graphics3d //a guess here and use 256kb as the size. Increase if needed. private const int MmeWords = 256 * 256; - private NvGpu Gpu; + private NvGpu _gpu; - private NvGpuEngine[] SubChannels; + private NvGpuEngine[] _subChannels; private struct CachedMacro { public int Position { get; private set; } - private bool ExecutionPending; - private int Argument; + private bool _executionPending; + private int _argument; - private MacroInterpreter Interpreter; + private MacroInterpreter _interpreter; - public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position) + public CachedMacro(NvGpuFifo pFifo, INvGpuEngine engine, int position) { - this.Position = Position; + Position = position; - ExecutionPending = false; - Argument = 0; + _executionPending = false; + _argument = 0; - Interpreter = new MacroInterpreter(PFifo, Engine); + _interpreter = new MacroInterpreter(pFifo, engine); } - public void StartExecution(int Argument) + public void StartExecution(int argument) { - this.Argument = Argument; + _argument = argument; - ExecutionPending = true; + _executionPending = true; } - public void Execute(NvGpuVmm Vmm, int[] Mme) + public void Execute(NvGpuVmm vmm, int[] mme) { - if (ExecutionPending) + if (_executionPending) { - ExecutionPending = false; + _executionPending = false; - Interpreter?.Execute(Vmm, Mme, Position, Argument); + _interpreter?.Execute(vmm, mme, Position, _argument); } } - public void PushArgument(int Argument) + public void PushArgument(int argument) { - Interpreter?.Fifo.Enqueue(Argument); + _interpreter?.Fifo.Enqueue(argument); } } - private int CurrMacroPosition; - private int CurrMacroBindIndex; + private int _currMacroPosition; + private int _currMacroBindIndex; - private CachedMacro[] Macros; + private CachedMacro[] _macros; - private int[] Mme; + private int[] _mme; - public NvGpuFifo(NvGpu Gpu) + public NvGpuFifo(NvGpu gpu) { - this.Gpu = Gpu; + _gpu = gpu; - SubChannels = new NvGpuEngine[8]; + _subChannels = new NvGpuEngine[8]; - Macros = new CachedMacro[MacrosCount]; + _macros = new CachedMacro[MacrosCount]; - Mme = new int[MmeWords]; + _mme = new int[MmeWords]; } - public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - if ((NvGpuFifoMeth)MethCall.Method == NvGpuFifoMeth.BindChannel) + if ((NvGpuFifoMeth)methCall.Method == NvGpuFifoMeth.BindChannel) { - NvGpuEngine Engine = (NvGpuEngine)MethCall.Argument; + NvGpuEngine engine = (NvGpuEngine)methCall.Argument; - SubChannels[MethCall.SubChannel] = Engine; + _subChannels[methCall.SubChannel] = engine; } else { - switch (SubChannels[MethCall.SubChannel]) + switch (_subChannels[methCall.SubChannel]) { - case NvGpuEngine._2d: Call2dMethod (Vmm, MethCall); break; - case NvGpuEngine._3d: Call3dMethod (Vmm, MethCall); break; - case NvGpuEngine.P2mf: CallP2mfMethod(Vmm, MethCall); break; - case NvGpuEngine.M2mf: CallM2mfMethod(Vmm, MethCall); break; + case NvGpuEngine._2d: Call2dMethod (vmm, methCall); break; + case NvGpuEngine._3d: Call3dMethod (vmm, methCall); break; + case NvGpuEngine.P2mf: CallP2mfMethod(vmm, methCall); break; + case NvGpuEngine.M2mf: CallM2mfMethod(vmm, methCall); break; } } } - private void Call2dMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void Call2dMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - Gpu.Engine2d.CallMethod(Vmm, MethCall); + _gpu.Engine2d.CallMethod(vmm, methCall); } - private void Call3dMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void Call3dMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - if (MethCall.Method < 0x80) + if (methCall.Method < 0x80) { - switch ((NvGpuFifoMeth)MethCall.Method) + switch ((NvGpuFifoMeth)methCall.Method) { case NvGpuFifoMeth.SetMacroUploadAddress: { - CurrMacroPosition = MethCall.Argument; + _currMacroPosition = methCall.Argument; break; } case NvGpuFifoMeth.SendMacroCodeData: { - Mme[CurrMacroPosition++] = MethCall.Argument; + _mme[_currMacroPosition++] = methCall.Argument; break; } case NvGpuFifoMeth.SetMacroBindingIndex: { - CurrMacroBindIndex = MethCall.Argument; + _currMacroBindIndex = methCall.Argument; break; } case NvGpuFifoMeth.BindMacro: { - int Position = MethCall.Argument; + int position = methCall.Argument; - Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position); + _macros[_currMacroBindIndex] = new CachedMacro(this, _gpu.Engine3d, position); break; } - default: CallP2mfMethod(Vmm, MethCall); break; + default: CallP2mfMethod(vmm, methCall); break; } } - else if (MethCall.Method < 0xe00) + else if (methCall.Method < 0xe00) { - Gpu.Engine3d.CallMethod(Vmm, MethCall); + _gpu.Engine3d.CallMethod(vmm, methCall); } else { - int MacroIndex = (MethCall.Method >> 1) & MacroIndexMask; + int macroIndex = (methCall.Method >> 1) & MacroIndexMask; - if ((MethCall.Method & 1) != 0) + if ((methCall.Method & 1) != 0) { - Macros[MacroIndex].PushArgument(MethCall.Argument); + _macros[macroIndex].PushArgument(methCall.Argument); } else { - Macros[MacroIndex].StartExecution(MethCall.Argument); + _macros[macroIndex].StartExecution(methCall.Argument); } - if (MethCall.IsLastCall) + if (methCall.IsLastCall) { - Macros[MacroIndex].Execute(Vmm, Mme); + _macros[macroIndex].Execute(vmm, _mme); } } } - private void CallP2mfMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void CallP2mfMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - Gpu.EngineP2mf.CallMethod(Vmm, MethCall); + _gpu.EngineP2mf.CallMethod(vmm, methCall); } - private void CallM2mfMethod(NvGpuVmm Vmm, GpuMethodCall MethCall) + private void CallM2mfMethod(NvGpuVmm vmm, GpuMethodCall methCall) { - Gpu.EngineM2mf.CallMethod(Vmm, MethCall); + _gpu.EngineM2mf.CallMethod(vmm, methCall); } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/NvGpuMethod.cs b/Ryujinx.Graphics/Graphics3d/NvGpuMethod.cs index 8730d144..23185c81 100644 --- a/Ryujinx.Graphics/Graphics3d/NvGpuMethod.cs +++ b/Ryujinx.Graphics/Graphics3d/NvGpuMethod.cs @@ -2,5 +2,5 @@ using Ryujinx.Graphics.Memory; namespace Ryujinx.Graphics.Graphics3d { - delegate void NvGpuMethod(NvGpuVmm Vmm, GpuMethodCall MethCall); + delegate void NvGpuMethod(NvGpuVmm vmm, GpuMethodCall methCall); }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/Texture/ASTCDecoder.cs b/Ryujinx.Graphics/Graphics3d/Texture/ASTCDecoder.cs deleted file mode 100644 index 00158dc1..00000000 --- a/Ryujinx.Graphics/Graphics3d/Texture/ASTCDecoder.cs +++ /dev/null @@ -1,1385 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; - -namespace Ryujinx.Graphics.Texture -{ - public class ASTCDecoderException : Exception - { - public ASTCDecoderException(string ExMsg) : base(ExMsg) { } - } - - //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp - public static class ASTCDecoder - { - struct TexelWeightParams - { - public int Width; - public int Height; - public bool DualPlane; - public int MaxWeight; - public bool Error; - public bool VoidExtentLDR; - public bool VoidExtentHDR; - - public int GetPackedBitSize() - { - // How many indices do we have? - int Indices = Height * Width; - - if (DualPlane) - { - Indices *= 2; - } - - IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(MaxWeight); - - return IntEncoded.GetBitLength(Indices); - } - - public int GetNumWeightValues() - { - int Ret = Width * Height; - - if (DualPlane) - { - Ret *= 2; - } - - return Ret; - } - } - - public static byte[] DecodeToRGBA8888( - byte[] InputBuffer, - int BlockX, - int BlockY, - int BlockZ, - int X, - int Y, - int Z) - { - using (MemoryStream InputStream = new MemoryStream(InputBuffer)) - { - BinaryReader BinReader = new BinaryReader(InputStream); - - if (BlockX > 12 || BlockY > 12) - { - throw new ASTCDecoderException("Block size unsupported!"); - } - - if (BlockZ != 1 || Z != 1) - { - // TODO: Support 3D textures? - throw new ASTCDecoderException("3D compressed textures unsupported!"); - } - - using (MemoryStream OutputStream = new MemoryStream()) - { - int BlockIndex = 0; - - for (int j = 0; j < Y; j += BlockY) - { - for (int i = 0; i < X; i += BlockX) - { - int[] DecompressedData = new int[144]; - - DecompressBlock(BinReader.ReadBytes(0x10), DecompressedData, BlockX, BlockY); - - int DecompressedWidth = Math.Min(BlockX, X - i); - int DecompressedHeight = Math.Min(BlockY, Y - j); - int BaseOffsets = (j * X + i) * 4; - - for (int jj = 0; jj < DecompressedHeight; jj++) - { - OutputStream.Seek(BaseOffsets + jj * X * 4, SeekOrigin.Begin); - - byte[] OutputBuffer = new byte[DecompressedData.Length * sizeof(int)]; - Buffer.BlockCopy(DecompressedData, 0, OutputBuffer, 0, OutputBuffer.Length); - - OutputStream.Write(OutputBuffer, jj * BlockX * 4, DecompressedWidth * 4); - } - - BlockIndex++; - } - } - - return OutputStream.ToArray(); - } - } - } - - public static bool DecompressBlock( - byte[] InputBuffer, - int[] OutputBuffer, - int BlockWidth, - int BlockHeight) - { - BitArrayStream BitStream = new BitArrayStream(new BitArray(InputBuffer)); - TexelWeightParams TexelParams = DecodeBlockInfo(BitStream); - - if (TexelParams.Error) - { - throw new ASTCDecoderException("Invalid block mode"); - } - - if (TexelParams.VoidExtentLDR) - { - FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight); - - return true; - } - - if (TexelParams.VoidExtentHDR) - { - throw new ASTCDecoderException("HDR void extent blocks are unsupported!"); - } - - if (TexelParams.Width > BlockWidth) - { - throw new ASTCDecoderException("Texel weight grid width should be smaller than block width"); - } - - if (TexelParams.Height > BlockHeight) - { - throw new ASTCDecoderException("Texel weight grid height should be smaller than block height"); - } - - // Read num partitions - int NumberPartitions = BitStream.ReadBits(2) + 1; - Debug.Assert(NumberPartitions <= 4); - - if (NumberPartitions == 4 && TexelParams.DualPlane) - { - throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks"); - } - - // Based on the number of partitions, read the color endpoint mode for - // each partition. - - // Determine partitions, partition index, and color endpoint modes - int PlaneIndices = -1; - int PartitionIndex; - uint[] ColorEndpointMode = { 0, 0, 0, 0 }; - - BitArrayStream ColorEndpointStream = new BitArrayStream(new BitArray(16 * 8)); - - // Read extra config data... - uint BaseColorEndpointMode = 0; - - if (NumberPartitions == 1) - { - ColorEndpointMode[0] = (uint)BitStream.ReadBits(4); - PartitionIndex = 0; - } - else - { - PartitionIndex = BitStream.ReadBits(10); - BaseColorEndpointMode = (uint)BitStream.ReadBits(6); - } - - uint BaseMode = (BaseColorEndpointMode & 3); - - // Remaining bits are color endpoint data... - int NumberWeightBits = TexelParams.GetPackedBitSize(); - int RemainingBits = 128 - NumberWeightBits - BitStream.Position; - - // Consider extra bits prior to texel data... - uint ExtraColorEndpointModeBits = 0; - - if (BaseMode != 0) - { - switch (NumberPartitions) - { - case 2: ExtraColorEndpointModeBits += 2; break; - case 3: ExtraColorEndpointModeBits += 5; break; - case 4: ExtraColorEndpointModeBits += 8; break; - default: Debug.Assert(false); break; - } - } - - RemainingBits -= (int)ExtraColorEndpointModeBits; - - // Do we have a dual plane situation? - int PlaneSelectorBits = 0; - - if (TexelParams.DualPlane) - { - PlaneSelectorBits = 2; - } - - RemainingBits -= PlaneSelectorBits; - - // Read color data... - int ColorDataBits = RemainingBits; - - while (RemainingBits > 0) - { - int NumberBits = Math.Min(RemainingBits, 8); - int Bits = BitStream.ReadBits(NumberBits); - ColorEndpointStream.WriteBits(Bits, NumberBits); - RemainingBits -= 8; - } - - // Read the plane selection bits - PlaneIndices = BitStream.ReadBits(PlaneSelectorBits); - - // Read the rest of the CEM - if (BaseMode != 0) - { - uint ExtraColorEndpointMode = (uint)BitStream.ReadBits((int)ExtraColorEndpointModeBits); - uint TempColorEndpointMode = (ExtraColorEndpointMode << 6) | BaseColorEndpointMode; - TempColorEndpointMode >>= 2; - - bool[] C = new bool[4]; - - for (int i = 0; i < NumberPartitions; i++) - { - C[i] = (TempColorEndpointMode & 1) != 0; - TempColorEndpointMode >>= 1; - } - - byte[] M = new byte[4]; - - for (int i = 0; i < NumberPartitions; i++) - { - M[i] = (byte)(TempColorEndpointMode & 3); - TempColorEndpointMode >>= 2; - Debug.Assert(M[i] <= 3); - } - - for (int i = 0; i < NumberPartitions; i++) - { - ColorEndpointMode[i] = BaseMode; - if (!(C[i])) ColorEndpointMode[i] -= 1; - ColorEndpointMode[i] <<= 2; - ColorEndpointMode[i] |= M[i]; - } - } - else if (NumberPartitions > 1) - { - uint TempColorEndpointMode = BaseColorEndpointMode >> 2; - - for (uint i = 0; i < NumberPartitions; i++) - { - ColorEndpointMode[i] = TempColorEndpointMode; - } - } - - // Make sure everything up till here is sane. - for (int i = 0; i < NumberPartitions; i++) - { - Debug.Assert(ColorEndpointMode[i] < 16); - } - Debug.Assert(BitStream.Position + TexelParams.GetPackedBitSize() == 128); - - // Decode both color data and texel weight data - int[] ColorValues = new int[32]; // Four values * two endpoints * four maximum partitions - DecodeColorValues(ColorValues, ColorEndpointStream.ToByteArray(), ColorEndpointMode, NumberPartitions, ColorDataBits); - - ASTCPixel[][] EndPoints = new ASTCPixel[4][]; - EndPoints[0] = new ASTCPixel[2]; - EndPoints[1] = new ASTCPixel[2]; - EndPoints[2] = new ASTCPixel[2]; - EndPoints[3] = new ASTCPixel[2]; - - int ColorValuesPosition = 0; - - for (int i = 0; i < NumberPartitions; i++) - { - ComputeEndpoints(EndPoints[i], ColorValues, ColorEndpointMode[i], ref ColorValuesPosition); - } - - // Read the texel weight data. - byte[] TexelWeightData = (byte[])InputBuffer.Clone(); - - // Reverse everything - for (int i = 0; i < 8; i++) - { - byte a = ReverseByte(TexelWeightData[i]); - byte b = ReverseByte(TexelWeightData[15 - i]); - - TexelWeightData[i] = b; - TexelWeightData[15 - i] = a; - } - - // Make sure that higher non-texel bits are set to zero - int ClearByteStart = (TexelParams.GetPackedBitSize() >> 3) + 1; - TexelWeightData[ClearByteStart - 1] &= (byte)((1 << (TexelParams.GetPackedBitSize() % 8)) - 1); - - int cLen = 16 - ClearByteStart; - for (int i = ClearByteStart; i < ClearByteStart + cLen; i++) TexelWeightData[i] = 0; - - List<IntegerEncoded> TexelWeightValues = new List<IntegerEncoded>(); - BitArrayStream WeightBitStream = new BitArrayStream(new BitArray(TexelWeightData)); - - IntegerEncoded.DecodeIntegerSequence(TexelWeightValues, WeightBitStream, TexelParams.MaxWeight, TexelParams.GetNumWeightValues()); - - // Blocks can be at most 12x12, so we can have as many as 144 weights - int[][] Weights = new int[2][]; - Weights[0] = new int[144]; - Weights[1] = new int[144]; - - UnquantizeTexelWeights(Weights, TexelWeightValues, TexelParams, BlockWidth, BlockHeight); - - // Now that we have endpoints and weights, we can interpolate and generate - // the proper decoding... - for (int j = 0; j < BlockHeight; j++) - { - for (int i = 0; i < BlockWidth; i++) - { - int Partition = Select2DPartition(PartitionIndex, i, j, NumberPartitions, ((BlockHeight * BlockWidth) < 32)); - Debug.Assert(Partition < NumberPartitions); - - ASTCPixel Pixel = new ASTCPixel(0, 0, 0, 0); - for (int Component = 0; Component < 4; Component++) - { - int Component0 = EndPoints[Partition][0].GetComponent(Component); - Component0 = BitArrayStream.Replicate(Component0, 8, 16); - int Component1 = EndPoints[Partition][1].GetComponent(Component); - Component1 = BitArrayStream.Replicate(Component1, 8, 16); - - int Plane = 0; - - if (TexelParams.DualPlane && (((PlaneIndices + 1) & 3) == Component)) - { - Plane = 1; - } - - int Weight = Weights[Plane][j * BlockWidth + i]; - int FinalComponent = (Component0 * (64 - Weight) + Component1 * Weight + 32) / 64; - - if (FinalComponent == 65535) - { - Pixel.SetComponent(Component, 255); - } - else - { - double FinalComponentFloat = FinalComponent; - Pixel.SetComponent(Component, (int)(255.0 * (FinalComponentFloat / 65536.0) + 0.5)); - } - } - - OutputBuffer[j * BlockWidth + i] = Pixel.Pack(); - } - } - - return true; - } - - private static int Select2DPartition(int Seed, int X, int Y, int PartitionCount, bool IsSmallBlock) - { - return SelectPartition(Seed, X, Y, 0, PartitionCount, IsSmallBlock); - } - - private static int SelectPartition(int Seed, int X, int Y, int Z, int PartitionCount, bool IsSmallBlock) - { - if (PartitionCount == 1) - { - return 0; - } - - if (IsSmallBlock) - { - X <<= 1; - Y <<= 1; - Z <<= 1; - } - - Seed += (PartitionCount - 1) * 1024; - - int RightNum = Hash52((uint)Seed); - byte Seed01 = (byte)(RightNum & 0xF); - byte Seed02 = (byte)((RightNum >> 4) & 0xF); - byte Seed03 = (byte)((RightNum >> 8) & 0xF); - byte Seed04 = (byte)((RightNum >> 12) & 0xF); - byte Seed05 = (byte)((RightNum >> 16) & 0xF); - byte Seed06 = (byte)((RightNum >> 20) & 0xF); - byte Seed07 = (byte)((RightNum >> 24) & 0xF); - byte Seed08 = (byte)((RightNum >> 28) & 0xF); - byte Seed09 = (byte)((RightNum >> 18) & 0xF); - byte Seed10 = (byte)((RightNum >> 22) & 0xF); - byte Seed11 = (byte)((RightNum >> 26) & 0xF); - byte Seed12 = (byte)(((RightNum >> 30) | (RightNum << 2)) & 0xF); - - Seed01 *= Seed01; Seed02 *= Seed02; - Seed03 *= Seed03; Seed04 *= Seed04; - Seed05 *= Seed05; Seed06 *= Seed06; - Seed07 *= Seed07; Seed08 *= Seed08; - Seed09 *= Seed09; Seed10 *= Seed10; - Seed11 *= Seed11; Seed12 *= Seed12; - - int SeedHash1, SeedHash2, SeedHash3; - - if ((Seed & 1) != 0) - { - SeedHash1 = (Seed & 2) != 0 ? 4 : 5; - SeedHash2 = (PartitionCount == 3) ? 6 : 5; - } - else - { - SeedHash1 = (PartitionCount == 3) ? 6 : 5; - SeedHash2 = (Seed & 2) != 0 ? 4 : 5; - } - - SeedHash3 = (Seed & 0x10) != 0 ? SeedHash1 : SeedHash2; - - Seed01 >>= SeedHash1; Seed02 >>= SeedHash2; Seed03 >>= SeedHash1; Seed04 >>= SeedHash2; - Seed05 >>= SeedHash1; Seed06 >>= SeedHash2; Seed07 >>= SeedHash1; Seed08 >>= SeedHash2; - Seed09 >>= SeedHash3; Seed10 >>= SeedHash3; Seed11 >>= SeedHash3; Seed12 >>= SeedHash3; - - int a = Seed01 * X + Seed02 * Y + Seed11 * Z + (RightNum >> 14); - int b = Seed03 * X + Seed04 * Y + Seed12 * Z + (RightNum >> 10); - int c = Seed05 * X + Seed06 * Y + Seed09 * Z + (RightNum >> 6); - int d = Seed07 * X + Seed08 * Y + Seed10 * Z + (RightNum >> 2); - - a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F; - - if (PartitionCount < 4) d = 0; - if (PartitionCount < 3) c = 0; - - if (a >= b && a >= c && a >= d) return 0; - else if (b >= c && b >= d) return 1; - else if (c >= d) return 2; - return 3; - } - - static int Hash52(uint Val) - { - Val ^= Val >> 15; Val -= Val << 17; Val += Val << 7; Val += Val << 4; - Val ^= Val >> 5; Val += Val << 16; Val ^= Val >> 7; Val ^= Val >> 3; - Val ^= Val << 6; Val ^= Val >> 17; - - return (int)Val; - } - - static void UnquantizeTexelWeights( - int[][] OutputBuffer, - List<IntegerEncoded> Weights, - TexelWeightParams TexelParams, - int BlockWidth, - int BlockHeight) - { - int WeightIndices = 0; - int[][] Unquantized = new int[2][]; - Unquantized[0] = new int[144]; - Unquantized[1] = new int[144]; - - for (int i = 0; i < Weights.Count; i++) - { - Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]); - - if (TexelParams.DualPlane) - { - i++; - Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]); - - if (i == Weights.Count) - { - break; - } - } - - if (++WeightIndices >= (TexelParams.Width * TexelParams.Height)) break; - } - - // Do infill if necessary (Section C.2.18) ... - int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1); - int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1); - - int PlaneScale = TexelParams.DualPlane ? 2 : 1; - - for (int Plane = 0; Plane < PlaneScale; Plane++) - { - for (int t = 0; t < BlockHeight; t++) - { - for (int s = 0; s < BlockWidth; s++) - { - int cs = Ds * s; - int ct = Dt * t; - - int gs = (cs * (TexelParams.Width - 1) + 32) >> 6; - int gt = (ct * (TexelParams.Height - 1) + 32) >> 6; - - int js = gs >> 4; - int fs = gs & 0xF; - - int jt = gt >> 4; - int ft = gt & 0x0F; - - int w11 = (fs * ft + 8) >> 4; - int w10 = ft - w11; - int w01 = fs - w11; - int w00 = 16 - fs - ft + w11; - - int v0 = js + jt * TexelParams.Width; - - int p00 = 0; - int p01 = 0; - int p10 = 0; - int p11 = 0; - - if (v0 < (TexelParams.Width * TexelParams.Height)) - { - p00 = Unquantized[Plane][v0]; - } - - if (v0 + 1 < (TexelParams.Width * TexelParams.Height)) - { - p01 = Unquantized[Plane][v0 + 1]; - } - - if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height)) - { - p10 = Unquantized[Plane][v0 + TexelParams.Width]; - } - - if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height)) - { - p11 = Unquantized[Plane][v0 + TexelParams.Width + 1]; - } - - OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4; - } - } - } - } - - static int UnquantizeTexelWeight(IntegerEncoded IntEncoded) - { - int BitValue = IntEncoded.BitValue; - int BitLength = IntEncoded.NumberBits; - - int A = BitArrayStream.Replicate(BitValue & 1, 1, 7); - int B = 0, C = 0, D = 0; - - int Result = 0; - - switch (IntEncoded.GetEncoding()) - { - case IntegerEncoded.EIntegerEncoding.JustBits: - Result = BitArrayStream.Replicate(BitValue, BitLength, 6); - break; - - case IntegerEncoded.EIntegerEncoding.Trit: - { - D = IntEncoded.TritValue; - Debug.Assert(D < 3); - - switch (BitLength) - { - case 0: - { - int[] Results = { 0, 32, 63 }; - Result = Results[D]; - - break; - } - - case 1: - { - C = 50; - break; - } - - case 2: - { - C = 23; - int b = (BitValue >> 1) & 1; - B = (b << 6) | (b << 2) | b; - - break; - } - - case 3: - { - C = 11; - int cb = (BitValue >> 1) & 3; - B = (cb << 5) | cb; - - break; - } - - default: - throw new ASTCDecoderException("Invalid trit encoding for texel weight"); - } - - break; - } - - case IntegerEncoded.EIntegerEncoding.Quint: - { - D = IntEncoded.QuintValue; - Debug.Assert(D < 5); - - switch (BitLength) - { - case 0: - { - int[] Results = { 0, 16, 32, 47, 63 }; - Result = Results[D]; - - break; - } - - case 1: - { - C = 28; - - break; - } - - case 2: - { - C = 13; - int b = (BitValue >> 1) & 1; - B = (b << 6) | (b << 1); - - break; - } - - default: - throw new ASTCDecoderException("Invalid quint encoding for texel weight"); - } - - break; - } - } - - if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0) - { - // Decode the value... - Result = D * C + B; - Result ^= A; - Result = (A & 0x20) | (Result >> 2); - } - - Debug.Assert(Result < 64); - - // Change from [0,63] to [0,64] - if (Result > 32) - { - Result += 1; - } - - return Result; - } - - static byte ReverseByte(byte b) - { - // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits - return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); - } - - static uint[] ReadUintColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) - { - uint[] Ret = new uint[Number]; - - for (int i = 0; i < Number; i++) - { - Ret[i] = (uint)ColorValues[ColorValuesPosition++]; - } - - return Ret; - } - - static int[] ReadIntColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) - { - int[] Ret = new int[Number]; - - for (int i = 0; i < Number; i++) - { - Ret[i] = ColorValues[ColorValuesPosition++]; - } - - return Ret; - } - - static void ComputeEndpoints( - ASTCPixel[] EndPoints, - int[] ColorValues, - uint ColorEndpointMode, - ref int ColorValuesPosition) - { - switch (ColorEndpointMode) - { - case 0: - { - uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[1], (short)Val[1]); - - break; - } - - - case 1: - { - uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); - int L0 = (int)((Val[0] >> 2) | (Val[1] & 0xC0)); - int L1 = (int)Math.Max(L0 + (Val[1] & 0x3F), 0xFFU); - - EndPoints[0] = new ASTCPixel(0xFF, (short)L0, (short)L0, (short)L0); - EndPoints[1] = new ASTCPixel(0xFF, (short)L1, (short)L1, (short)L1); - - break; - } - - case 4: - { - uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel((short)Val[3], (short)Val[1], (short)Val[1], (short)Val[1]); - - break; - } - - case 5: - { - int[] Val = ReadIntColorValues(4, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - - EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel((short)(Val[2] + Val[3]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1])); - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - case 6: - { - uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel(0xFF, (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[1], (short)Val[2]); - - break; - } - - case 8: - { - uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); - - if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) - { - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - } - - break; - } - - case 9: - { - int[] Val = ReadIntColorValues(6, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); - - if (Val[1] + Val[3] + Val[5] >= 0) - { - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel(0xFF, (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(0xFF, Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(0xFF, Val[0], Val[2], Val[4]); - } - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - case 10: - { - uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel((short)Val[4], (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); - EndPoints[1] = new ASTCPixel((short)Val[5], (short)Val[0], (short)Val[1], (short)Val[2]); - - break; - } - - case 12: - { - uint[] Val = ReadUintColorValues(8, ColorValues, ref ColorValuesPosition); - - if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) - { - EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); - EndPoints[1] = ASTCPixel.BlueContract((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - } - - break; - } - - case 13: - { - int[] Val = ReadIntColorValues(8, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); - BitArrayStream.BitTransferSigned(ref Val[7], ref Val[6]); - - if (Val[1] + Val[3] + Val[5] >= 0) - { - EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel((short)(Val[7] + Val[6]), (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(Val[6] + Val[7], Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(Val[6], Val[0], Val[2], Val[4]); - } - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported color endpoint mode (is it HDR?)"); - } - } - - static void DecodeColorValues( - int[] OutputValues, - byte[] InputData, - uint[] Modes, - int NumberPartitions, - int NumberBitsForColorData) - { - // First figure out how many color values we have - int NumberValues = 0; - - for (int i = 0; i < NumberPartitions; i++) - { - NumberValues += (int)((Modes[i] >> 2) + 1) << 1; - } - - // Then based on the number of values and the remaining number of bits, - // figure out the max value for each of them... - int Range = 256; - - while (--Range > 0) - { - IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range); - int BitLength = IntEncoded.GetBitLength(NumberValues); - - if (BitLength <= NumberBitsForColorData) - { - // Find the smallest possible range that matches the given encoding - while (--Range > 0) - { - IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range); - if (!NewIntEncoded.MatchesEncoding(IntEncoded)) - { - break; - } - } - - // Return to last matching range. - Range++; - break; - } - } - - // We now have enough to decode our integer sequence. - List<IntegerEncoded> IntegerEncodedSequence = new List<IntegerEncoded>(); - BitArrayStream ColorBitStream = new BitArrayStream(new BitArray(InputData)); - - IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues); - - // Once we have the decoded values, we need to dequantize them to the 0-255 range - // This procedure is outlined in ASTC spec C.2.13 - int OutputIndices = 0; - - foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence) - { - int BitLength = IntEncoded.NumberBits; - int BitValue = IntEncoded.BitValue; - - Debug.Assert(BitLength >= 1); - - int A = 0, B = 0, C = 0, D = 0; - // A is just the lsb replicated 9 times. - A = BitArrayStream.Replicate(BitValue & 1, 1, 9); - - switch (IntEncoded.GetEncoding()) - { - case IntegerEncoded.EIntegerEncoding.JustBits: - { - OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8); - - break; - } - - case IntegerEncoded.EIntegerEncoding.Trit: - { - D = IntEncoded.TritValue; - - switch (BitLength) - { - case 1: - { - C = 204; - - break; - } - - case 2: - { - C = 93; - // B = b000b0bb0 - int b = (BitValue >> 1) & 1; - B = (b << 8) | (b << 4) | (b << 2) | (b << 1); - - break; - } - - case 3: - { - C = 44; - // B = cb000cbcb - int cb = (BitValue >> 1) & 3; - B = (cb << 7) | (cb << 2) | cb; - - break; - } - - - case 4: - { - C = 22; - // B = dcb000dcb - int dcb = (BitValue >> 1) & 7; - B = (dcb << 6) | dcb; - - break; - } - - case 5: - { - C = 11; - // B = edcb000ed - int edcb = (BitValue >> 1) & 0xF; - B = (edcb << 5) | (edcb >> 2); - - break; - } - - case 6: - { - C = 5; - // B = fedcb000f - int fedcb = (BitValue >> 1) & 0x1F; - B = (fedcb << 4) | (fedcb >> 4); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported trit encoding for color values!"); - } - - break; - } - - case IntegerEncoded.EIntegerEncoding.Quint: - { - D = IntEncoded.QuintValue; - - switch (BitLength) - { - case 1: - { - C = 113; - - break; - } - - case 2: - { - C = 54; - // B = b0000bb00 - int b = (BitValue >> 1) & 1; - B = (b << 8) | (b << 3) | (b << 2); - - break; - } - - case 3: - { - C = 26; - // B = cb0000cbc - int cb = (BitValue >> 1) & 3; - B = (cb << 7) | (cb << 1) | (cb >> 1); - - break; - } - - case 4: - { - C = 13; - // B = dcb0000dc - int dcb = (BitValue >> 1) & 7; - B = (dcb << 6) | (dcb >> 1); - - break; - } - - case 5: - { - C = 6; - // B = edcb0000e - int edcb = (BitValue >> 1) & 0xF; - B = (edcb << 5) | (edcb >> 3); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported quint encoding for color values!"); - } - break; - } - } - - if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits) - { - int T = D * C + B; - T ^= A; - T = (A & 0x80) | (T >> 2); - - OutputValues[OutputIndices++] = T; - } - } - - // Make sure that each of our values is in the proper range... - for (int i = 0; i < NumberValues; i++) - { - Debug.Assert(OutputValues[i] <= 255); - } - } - - static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight) - { - // Don't actually care about the void extent, just read the bits... - for (int i = 0; i < 4; ++i) - { - BitStream.ReadBits(13); - } - - // Decode the RGBA components and renormalize them to the range [0, 255] - ushort R = (ushort)BitStream.ReadBits(16); - ushort G = (ushort)BitStream.ReadBits(16); - ushort B = (ushort)BitStream.ReadBits(16); - ushort A = (ushort)BitStream.ReadBits(16); - - int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16; - - for (int j = 0; j < BlockHeight; j++) - { - for (int i = 0; i < BlockWidth; i++) - { - OutputBuffer[j * BlockWidth + i] = RGBA; - } - } - } - - static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream) - { - TexelWeightParams TexelParams = new TexelWeightParams(); - - // Read the entire block mode all at once - ushort ModeBits = (ushort)BitStream.ReadBits(11); - - // Does this match the void extent block mode? - if ((ModeBits & 0x01FF) == 0x1FC) - { - if ((ModeBits & 0x200) != 0) - { - TexelParams.VoidExtentHDR = true; - } - else - { - TexelParams.VoidExtentLDR = true; - } - - // Next two bits must be one. - if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0) - { - TexelParams.Error = true; - } - - return TexelParams; - } - - // First check if the last four bits are zero - if ((ModeBits & 0xF) == 0) - { - TexelParams.Error = true; - return TexelParams; - } - - // If the last two bits are zero, then if bits - // [6-8] are all ones, this is also reserved. - if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0) - { - TexelParams.Error = true; - - return TexelParams; - } - - // Otherwise, there is no error... Figure out the layout - // of the block mode. Layout is determined by a number - // between 0 and 9 corresponding to table C.2.8 of the - // ASTC spec. - int Layout = 0; - - if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0) - { - // layout is in [0-4] - if ((ModeBits & 0x8) != 0) - { - // layout is in [2-4] - if ((ModeBits & 0x4) != 0) - { - // layout is in [3-4] - if ((ModeBits & 0x100) != 0) - { - Layout = 4; - } - else - { - Layout = 3; - } - } - else - { - Layout = 2; - } - } - else - { - // layout is in [0-1] - if ((ModeBits & 0x4) != 0) - { - Layout = 1; - } - else - { - Layout = 0; - } - } - } - else - { - // layout is in [5-9] - if ((ModeBits & 0x100) != 0) - { - // layout is in [7-9] - if ((ModeBits & 0x80) != 0) - { - // layout is in [7-8] - Debug.Assert((ModeBits & 0x40) == 0); - - if ((ModeBits & 0x20) != 0) - { - Layout = 8; - } - else - { - Layout = 7; - } - } - else - { - Layout = 9; - } - } - else - { - // layout is in [5-6] - if ((ModeBits & 0x80) != 0) - { - Layout = 6; - } - else - { - Layout = 5; - } - } - } - - Debug.Assert(Layout < 10); - - // Determine R - int R = (ModeBits >> 4) & 1; - if (Layout < 5) - { - R |= (ModeBits & 0x3) << 1; - } - else - { - R |= (ModeBits & 0xC) >> 1; - } - - Debug.Assert(2 <= R && R <= 7); - - // Determine width & height - switch (Layout) - { - case 0: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = B + 4; - TexelParams.Height = A + 2; - - break; - } - - case 1: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = B + 8; - TexelParams.Height = A + 2; - - break; - } - - case 2: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = A + 2; - TexelParams.Height = B + 8; - - break; - } - - case 3: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x1; - - TexelParams.Width = A + 2; - TexelParams.Height = B + 6; - - break; - } - - case 4: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x1; - - TexelParams.Width = B + 2; - TexelParams.Height = A + 2; - - break; - } - - case 5: - { - int A = (ModeBits >> 5) & 0x3; - - TexelParams.Width = 12; - TexelParams.Height = A + 2; - - break; - } - - case 6: - { - int A = (ModeBits >> 5) & 0x3; - - TexelParams.Width = A + 2; - TexelParams.Height = 12; - - break; - } - - case 7: - { - TexelParams.Width = 6; - TexelParams.Height = 10; - - break; - } - - case 8: - { - TexelParams.Width = 10; - TexelParams.Height = 6; - break; - } - - case 9: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 9) & 0x3; - - TexelParams.Width = A + 6; - TexelParams.Height = B + 6; - - break; - } - - default: - //Don't know this layout... - TexelParams.Error = true; - break; - } - - // Determine whether or not we're using dual planes - // and/or high precision layouts. - bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0)); - bool H = (Layout != 9) && ((ModeBits & 0x200) != 0); - - if (H) - { - int[] MaxWeights = { 9, 11, 15, 19, 23, 31 }; - TexelParams.MaxWeight = MaxWeights[R - 2]; - } - else - { - int[] MaxWeights = { 1, 2, 3, 4, 5, 7 }; - TexelParams.MaxWeight = MaxWeights[R - 2]; - } - - TexelParams.DualPlane = D; - - return TexelParams; - } - } -} diff --git a/Ryujinx.Graphics/Graphics3d/Texture/ASTCPixel.cs b/Ryujinx.Graphics/Graphics3d/Texture/ASTCPixel.cs deleted file mode 100644 index c43eaf93..00000000 --- a/Ryujinx.Graphics/Graphics3d/Texture/ASTCPixel.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Diagnostics; - -namespace Ryujinx.Graphics.Texture -{ - class ASTCPixel - { - public short R { get; set; } - public short G { get; set; } - public short B { get; set; } - public short A { get; set; } - - byte[] BitDepth = new byte[4]; - - public ASTCPixel(short _A, short _R, short _G, short _B) - { - A = _A; - R = _R; - G = _G; - B = _B; - - for (int i = 0; i < 4; i++) - BitDepth[i] = 8; - } - - public void ClampByte() - { - R = Math.Min(Math.Max(R, (short)0), (short)255); - G = Math.Min(Math.Max(G, (short)0), (short)255); - B = Math.Min(Math.Max(B, (short)0), (short)255); - A = Math.Min(Math.Max(A, (short)0), (short)255); - } - - public short GetComponent(int Index) - { - switch(Index) - { - case 0: return A; - case 1: return R; - case 2: return G; - case 3: return B; - } - - return 0; - } - - public void SetComponent(int Index, int Value) - { - switch (Index) - { - case 0: - A = (short)Value; - break; - case 1: - R = (short)Value; - break; - case 2: - G = (short)Value; - break; - case 3: - B = (short)Value; - break; - } - } - - public void ChangeBitDepth(byte[] Depth) - { - for(int i = 0; i< 4; i++) - { - int Value = ChangeBitDepth(GetComponent(i), BitDepth[i], Depth[i]); - - SetComponent(i, Value); - BitDepth[i] = Depth[i]; - } - } - - short ChangeBitDepth(short Value, byte OldDepth, byte NewDepth) - { - Debug.Assert(NewDepth <= 8); - Debug.Assert(OldDepth <= 8); - - if (OldDepth == NewDepth) - { - // Do nothing - return Value; - } - else if (OldDepth == 0 && NewDepth != 0) - { - return (short)((1 << NewDepth) - 1); - } - else if (NewDepth > OldDepth) - { - return (short)BitArrayStream.Replicate(Value, OldDepth, NewDepth); - } - else - { - // oldDepth > newDepth - if (NewDepth == 0) - { - return 0xFF; - } - else - { - byte BitsWasted = (byte)(OldDepth - NewDepth); - short TempValue = Value; - - TempValue = (short)((TempValue + (1 << (BitsWasted - 1))) >> BitsWasted); - TempValue = Math.Min(Math.Max((short)0, TempValue), (short)((1 << NewDepth) - 1)); - - return (byte)(TempValue); - } - } - } - - public int Pack() - { - ASTCPixel NewPixel = new ASTCPixel(A, R, G, B); - byte[] eightBitDepth = { 8, 8, 8, 8 }; - - NewPixel.ChangeBitDepth(eightBitDepth); - - return (byte)NewPixel.A << 24 | - (byte)NewPixel.B << 16 | - (byte)NewPixel.G << 8 | - (byte)NewPixel.R << 0; - } - - // Adds more precision to the blue channel as described - // in C.2.14 - public static ASTCPixel BlueContract(int a, int r, int g, int b) - { - return new ASTCPixel((short)(a), - (short)((r + b) >> 1), - (short)((g + b) >> 1), - (short)(b)); - } - } -} diff --git a/Ryujinx.Graphics/Graphics3d/Texture/AstcDecoder.cs b/Ryujinx.Graphics/Graphics3d/Texture/AstcDecoder.cs new file mode 100644 index 00000000..99b166f3 --- /dev/null +++ b/Ryujinx.Graphics/Graphics3d/Texture/AstcDecoder.cs @@ -0,0 +1,1385 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; + +namespace Ryujinx.Graphics.Texture +{ + public class AstcDecoderException : Exception + { + public AstcDecoderException(string exMsg) : base(exMsg) { } + } + + //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp + public static class AstcDecoder + { + struct TexelWeightParams + { + public int Width; + public int Height; + public bool DualPlane; + public int MaxWeight; + public bool Error; + public bool VoidExtentLdr; + public bool VoidExtentHdr; + + public int GetPackedBitSize() + { + // How many indices do we have? + int indices = Height * Width; + + if (DualPlane) + { + indices *= 2; + } + + IntegerEncoded intEncoded = IntegerEncoded.CreateEncoding(MaxWeight); + + return intEncoded.GetBitLength(indices); + } + + public int GetNumWeightValues() + { + int ret = Width * Height; + + if (DualPlane) + { + ret *= 2; + } + + return ret; + } + } + + public static byte[] DecodeToRgba8888( + byte[] inputBuffer, + int blockX, + int blockY, + int blockZ, + int x, + int y, + int z) + { + using (MemoryStream inputStream = new MemoryStream(inputBuffer)) + { + BinaryReader binReader = new BinaryReader(inputStream); + + if (blockX > 12 || blockY > 12) + { + throw new AstcDecoderException("Block size unsupported!"); + } + + if (blockZ != 1 || z != 1) + { + // TODO: Support 3D textures? + throw new AstcDecoderException("3D compressed textures unsupported!"); + } + + using (MemoryStream outputStream = new MemoryStream()) + { + int blockIndex = 0; + + for (int j = 0; j < y; j += blockY) + { + for (int i = 0; i < x; i += blockX) + { + int[] decompressedData = new int[144]; + + DecompressBlock(binReader.ReadBytes(0x10), decompressedData, blockX, blockY); + + int decompressedWidth = Math.Min(blockX, x - i); + int decompressedHeight = Math.Min(blockY, y - j); + int baseOffsets = (j * x + i) * 4; + + for (int jj = 0; jj < decompressedHeight; jj++) + { + outputStream.Seek(baseOffsets + jj * x * 4, SeekOrigin.Begin); + + byte[] outputBuffer = new byte[decompressedData.Length * sizeof(int)]; + Buffer.BlockCopy(decompressedData, 0, outputBuffer, 0, outputBuffer.Length); + + outputStream.Write(outputBuffer, jj * blockX * 4, decompressedWidth * 4); + } + + blockIndex++; + } + } + + return outputStream.ToArray(); + } + } + } + + public static bool DecompressBlock( + byte[] inputBuffer, + int[] outputBuffer, + int blockWidth, + int blockHeight) + { + BitArrayStream bitStream = new BitArrayStream(new BitArray(inputBuffer)); + TexelWeightParams texelParams = DecodeBlockInfo(bitStream); + + if (texelParams.Error) + { + throw new AstcDecoderException("Invalid block mode"); + } + + if (texelParams.VoidExtentLdr) + { + FillVoidExtentLdr(bitStream, outputBuffer, blockWidth, blockHeight); + + return true; + } + + if (texelParams.VoidExtentHdr) + { + throw new AstcDecoderException("HDR void extent blocks are unsupported!"); + } + + if (texelParams.Width > blockWidth) + { + throw new AstcDecoderException("Texel weight grid width should be smaller than block width"); + } + + if (texelParams.Height > blockHeight) + { + throw new AstcDecoderException("Texel weight grid height should be smaller than block height"); + } + + // Read num partitions + int numberPartitions = bitStream.ReadBits(2) + 1; + Debug.Assert(numberPartitions <= 4); + + if (numberPartitions == 4 && texelParams.DualPlane) + { + throw new AstcDecoderException("Dual plane mode is incompatible with four partition blocks"); + } + + // Based on the number of partitions, read the color endpoint mode for + // each partition. + + // Determine partitions, partition index, and color endpoint modes + int planeIndices = -1; + int partitionIndex; + uint[] colorEndpointMode = { 0, 0, 0, 0 }; + + BitArrayStream colorEndpointStream = new BitArrayStream(new BitArray(16 * 8)); + + // Read extra config data... + uint baseColorEndpointMode = 0; + + if (numberPartitions == 1) + { + colorEndpointMode[0] = (uint)bitStream.ReadBits(4); + partitionIndex = 0; + } + else + { + partitionIndex = bitStream.ReadBits(10); + baseColorEndpointMode = (uint)bitStream.ReadBits(6); + } + + uint baseMode = (baseColorEndpointMode & 3); + + // Remaining bits are color endpoint data... + int numberWeightBits = texelParams.GetPackedBitSize(); + int remainingBits = 128 - numberWeightBits - bitStream.Position; + + // Consider extra bits prior to texel data... + uint extraColorEndpointModeBits = 0; + + if (baseMode != 0) + { + switch (numberPartitions) + { + case 2: extraColorEndpointModeBits += 2; break; + case 3: extraColorEndpointModeBits += 5; break; + case 4: extraColorEndpointModeBits += 8; break; + default: Debug.Assert(false); break; + } + } + + remainingBits -= (int)extraColorEndpointModeBits; + + // Do we have a dual plane situation? + int planeSelectorBits = 0; + + if (texelParams.DualPlane) + { + planeSelectorBits = 2; + } + + remainingBits -= planeSelectorBits; + + // Read color data... + int colorDataBits = remainingBits; + + while (remainingBits > 0) + { + int numberBits = Math.Min(remainingBits, 8); + int bits = bitStream.ReadBits(numberBits); + colorEndpointStream.WriteBits(bits, numberBits); + remainingBits -= 8; + } + + // Read the plane selection bits + planeIndices = bitStream.ReadBits(planeSelectorBits); + + // Read the rest of the CEM + if (baseMode != 0) + { + uint extraColorEndpointMode = (uint)bitStream.ReadBits((int)extraColorEndpointModeBits); + uint tempColorEndpointMode = (extraColorEndpointMode << 6) | baseColorEndpointMode; + tempColorEndpointMode >>= 2; + + bool[] c = new bool[4]; + + for (int i = 0; i < numberPartitions; i++) + { + c[i] = (tempColorEndpointMode & 1) != 0; + tempColorEndpointMode >>= 1; + } + + byte[] m = new byte[4]; + + for (int i = 0; i < numberPartitions; i++) + { + m[i] = (byte)(tempColorEndpointMode & 3); + tempColorEndpointMode >>= 2; + Debug.Assert(m[i] <= 3); + } + + for (int i = 0; i < numberPartitions; i++) + { + colorEndpointMode[i] = baseMode; + if (!(c[i])) colorEndpointMode[i] -= 1; + colorEndpointMode[i] <<= 2; + colorEndpointMode[i] |= m[i]; + } + } + else if (numberPartitions > 1) + { + uint tempColorEndpointMode = baseColorEndpointMode >> 2; + + for (uint i = 0; i < numberPartitions; i++) + { + colorEndpointMode[i] = tempColorEndpointMode; + } + } + + // Make sure everything up till here is sane. + for (int i = 0; i < numberPartitions; i++) + { + Debug.Assert(colorEndpointMode[i] < 16); + } + Debug.Assert(bitStream.Position + texelParams.GetPackedBitSize() == 128); + + // Decode both color data and texel weight data + int[] colorValues = new int[32]; // Four values * two endpoints * four maximum partitions + DecodeColorValues(colorValues, colorEndpointStream.ToByteArray(), colorEndpointMode, numberPartitions, colorDataBits); + + AstcPixel[][] endPoints = new AstcPixel[4][]; + endPoints[0] = new AstcPixel[2]; + endPoints[1] = new AstcPixel[2]; + endPoints[2] = new AstcPixel[2]; + endPoints[3] = new AstcPixel[2]; + + int colorValuesPosition = 0; + + for (int i = 0; i < numberPartitions; i++) + { + ComputeEndpoints(endPoints[i], colorValues, colorEndpointMode[i], ref colorValuesPosition); + } + + // Read the texel weight data. + byte[] texelWeightData = (byte[])inputBuffer.Clone(); + + // Reverse everything + for (int i = 0; i < 8; i++) + { + byte a = ReverseByte(texelWeightData[i]); + byte b = ReverseByte(texelWeightData[15 - i]); + + texelWeightData[i] = b; + texelWeightData[15 - i] = a; + } + + // Make sure that higher non-texel bits are set to zero + int clearByteStart = (texelParams.GetPackedBitSize() >> 3) + 1; + texelWeightData[clearByteStart - 1] &= (byte)((1 << (texelParams.GetPackedBitSize() % 8)) - 1); + + int cLen = 16 - clearByteStart; + for (int i = clearByteStart; i < clearByteStart + cLen; i++) texelWeightData[i] = 0; + + List<IntegerEncoded> texelWeightValues = new List<IntegerEncoded>(); + BitArrayStream weightBitStream = new BitArrayStream(new BitArray(texelWeightData)); + + IntegerEncoded.DecodeIntegerSequence(texelWeightValues, weightBitStream, texelParams.MaxWeight, texelParams.GetNumWeightValues()); + + // Blocks can be at most 12x12, so we can have as many as 144 weights + int[][] weights = new int[2][]; + weights[0] = new int[144]; + weights[1] = new int[144]; + + UnquantizeTexelWeights(weights, texelWeightValues, texelParams, blockWidth, blockHeight); + + // Now that we have endpoints and weights, we can interpolate and generate + // the proper decoding... + for (int j = 0; j < blockHeight; j++) + { + for (int i = 0; i < blockWidth; i++) + { + int partition = Select2dPartition(partitionIndex, i, j, numberPartitions, ((blockHeight * blockWidth) < 32)); + Debug.Assert(partition < numberPartitions); + + AstcPixel pixel = new AstcPixel(0, 0, 0, 0); + for (int component = 0; component < 4; component++) + { + int component0 = endPoints[partition][0].GetComponent(component); + component0 = BitArrayStream.Replicate(component0, 8, 16); + int component1 = endPoints[partition][1].GetComponent(component); + component1 = BitArrayStream.Replicate(component1, 8, 16); + + int plane = 0; + + if (texelParams.DualPlane && (((planeIndices + 1) & 3) == component)) + { + plane = 1; + } + + int weight = weights[plane][j * blockWidth + i]; + int finalComponent = (component0 * (64 - weight) + component1 * weight + 32) / 64; + + if (finalComponent == 65535) + { + pixel.SetComponent(component, 255); + } + else + { + double finalComponentFloat = finalComponent; + pixel.SetComponent(component, (int)(255.0 * (finalComponentFloat / 65536.0) + 0.5)); + } + } + + outputBuffer[j * blockWidth + i] = pixel.Pack(); + } + } + + return true; + } + + private static int Select2dPartition(int seed, int x, int y, int partitionCount, bool isSmallBlock) + { + return SelectPartition(seed, x, y, 0, partitionCount, isSmallBlock); + } + + private static int SelectPartition(int seed, int x, int y, int z, int partitionCount, bool isSmallBlock) + { + if (partitionCount == 1) + { + return 0; + } + + if (isSmallBlock) + { + x <<= 1; + y <<= 1; + z <<= 1; + } + + seed += (partitionCount - 1) * 1024; + + int rightNum = Hash52((uint)seed); + byte seed01 = (byte)(rightNum & 0xF); + byte seed02 = (byte)((rightNum >> 4) & 0xF); + byte seed03 = (byte)((rightNum >> 8) & 0xF); + byte seed04 = (byte)((rightNum >> 12) & 0xF); + byte seed05 = (byte)((rightNum >> 16) & 0xF); + byte seed06 = (byte)((rightNum >> 20) & 0xF); + byte seed07 = (byte)((rightNum >> 24) & 0xF); + byte seed08 = (byte)((rightNum >> 28) & 0xF); + byte seed09 = (byte)((rightNum >> 18) & 0xF); + byte seed10 = (byte)((rightNum >> 22) & 0xF); + byte seed11 = (byte)((rightNum >> 26) & 0xF); + byte seed12 = (byte)(((rightNum >> 30) | (rightNum << 2)) & 0xF); + + seed01 *= seed01; seed02 *= seed02; + seed03 *= seed03; seed04 *= seed04; + seed05 *= seed05; seed06 *= seed06; + seed07 *= seed07; seed08 *= seed08; + seed09 *= seed09; seed10 *= seed10; + seed11 *= seed11; seed12 *= seed12; + + int seedHash1, seedHash2, seedHash3; + + if ((seed & 1) != 0) + { + seedHash1 = (seed & 2) != 0 ? 4 : 5; + seedHash2 = (partitionCount == 3) ? 6 : 5; + } + else + { + seedHash1 = (partitionCount == 3) ? 6 : 5; + seedHash2 = (seed & 2) != 0 ? 4 : 5; + } + + seedHash3 = (seed & 0x10) != 0 ? seedHash1 : seedHash2; + + seed01 >>= seedHash1; seed02 >>= seedHash2; seed03 >>= seedHash1; seed04 >>= seedHash2; + seed05 >>= seedHash1; seed06 >>= seedHash2; seed07 >>= seedHash1; seed08 >>= seedHash2; + seed09 >>= seedHash3; seed10 >>= seedHash3; seed11 >>= seedHash3; seed12 >>= seedHash3; + + int a = seed01 * x + seed02 * y + seed11 * z + (rightNum >> 14); + int b = seed03 * x + seed04 * y + seed12 * z + (rightNum >> 10); + int c = seed05 * x + seed06 * y + seed09 * z + (rightNum >> 6); + int d = seed07 * x + seed08 * y + seed10 * z + (rightNum >> 2); + + a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F; + + if (partitionCount < 4) d = 0; + if (partitionCount < 3) c = 0; + + if (a >= b && a >= c && a >= d) return 0; + else if (b >= c && b >= d) return 1; + else if (c >= d) return 2; + return 3; + } + + static int Hash52(uint val) + { + val ^= val >> 15; val -= val << 17; val += val << 7; val += val << 4; + val ^= val >> 5; val += val << 16; val ^= val >> 7; val ^= val >> 3; + val ^= val << 6; val ^= val >> 17; + + return (int)val; + } + + static void UnquantizeTexelWeights( + int[][] outputBuffer, + List<IntegerEncoded> weights, + TexelWeightParams texelParams, + int blockWidth, + int blockHeight) + { + int weightIndices = 0; + int[][] unquantized = new int[2][]; + unquantized[0] = new int[144]; + unquantized[1] = new int[144]; + + for (int i = 0; i < weights.Count; i++) + { + unquantized[0][weightIndices] = UnquantizeTexelWeight(weights[i]); + + if (texelParams.DualPlane) + { + i++; + unquantized[1][weightIndices] = UnquantizeTexelWeight(weights[i]); + + if (i == weights.Count) + { + break; + } + } + + if (++weightIndices >= (texelParams.Width * texelParams.Height)) break; + } + + // Do infill if necessary (Section C.2.18) ... + int ds = (1024 + (blockWidth / 2)) / (blockWidth - 1); + int dt = (1024 + (blockHeight / 2)) / (blockHeight - 1); + + int planeScale = texelParams.DualPlane ? 2 : 1; + + for (int plane = 0; plane < planeScale; plane++) + { + for (int t = 0; t < blockHeight; t++) + { + for (int s = 0; s < blockWidth; s++) + { + int cs = ds * s; + int ct = dt * t; + + int gs = (cs * (texelParams.Width - 1) + 32) >> 6; + int gt = (ct * (texelParams.Height - 1) + 32) >> 6; + + int js = gs >> 4; + int fs = gs & 0xF; + + int jt = gt >> 4; + int ft = gt & 0x0F; + + int w11 = (fs * ft + 8) >> 4; + int w10 = ft - w11; + int w01 = fs - w11; + int w00 = 16 - fs - ft + w11; + + int v0 = js + jt * texelParams.Width; + + int p00 = 0; + int p01 = 0; + int p10 = 0; + int p11 = 0; + + if (v0 < (texelParams.Width * texelParams.Height)) + { + p00 = unquantized[plane][v0]; + } + + if (v0 + 1 < (texelParams.Width * texelParams.Height)) + { + p01 = unquantized[plane][v0 + 1]; + } + + if (v0 + texelParams.Width < (texelParams.Width * texelParams.Height)) + { + p10 = unquantized[plane][v0 + texelParams.Width]; + } + + if (v0 + texelParams.Width + 1 < (texelParams.Width * texelParams.Height)) + { + p11 = unquantized[plane][v0 + texelParams.Width + 1]; + } + + outputBuffer[plane][t * blockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4; + } + } + } + } + + static int UnquantizeTexelWeight(IntegerEncoded intEncoded) + { + int bitValue = intEncoded.BitValue; + int bitLength = intEncoded.NumberBits; + + int a = BitArrayStream.Replicate(bitValue & 1, 1, 7); + int b = 0, c = 0, d = 0; + + int result = 0; + + switch (intEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + result = BitArrayStream.Replicate(bitValue, bitLength, 6); + break; + + case IntegerEncoded.EIntegerEncoding.Trit: + { + d = intEncoded.TritValue; + Debug.Assert(d < 3); + + switch (bitLength) + { + case 0: + { + int[] results = { 0, 32, 63 }; + result = results[d]; + + break; + } + + case 1: + { + c = 50; + break; + } + + case 2: + { + c = 23; + int b2 = (bitValue >> 1) & 1; + b = (b2 << 6) | (b2 << 2) | b2; + + break; + } + + case 3: + { + c = 11; + int cb = (bitValue >> 1) & 3; + b = (cb << 5) | cb; + + break; + } + + default: + throw new AstcDecoderException("Invalid trit encoding for texel weight"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + d = intEncoded.QuintValue; + Debug.Assert(d < 5); + + switch (bitLength) + { + case 0: + { + int[] results = { 0, 16, 32, 47, 63 }; + result = results[d]; + + break; + } + + case 1: + { + c = 28; + + break; + } + + case 2: + { + c = 13; + int b2 = (bitValue >> 1) & 1; + b = (b2 << 6) | (b2 << 1); + + break; + } + + default: + throw new AstcDecoderException("Invalid quint encoding for texel weight"); + } + + break; + } + } + + if (intEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && bitLength > 0) + { + // Decode the value... + result = d * c + b; + result ^= a; + result = (a & 0x20) | (result >> 2); + } + + Debug.Assert(result < 64); + + // Change from [0,63] to [0,64] + if (result > 32) + { + result += 1; + } + + return result; + } + + static byte ReverseByte(byte b) + { + // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits + return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); + } + + static uint[] ReadUintColorValues(int number, int[] colorValues, ref int colorValuesPosition) + { + uint[] ret = new uint[number]; + + for (int i = 0; i < number; i++) + { + ret[i] = (uint)colorValues[colorValuesPosition++]; + } + + return ret; + } + + static int[] ReadIntColorValues(int number, int[] colorValues, ref int colorValuesPosition) + { + int[] ret = new int[number]; + + for (int i = 0; i < number; i++) + { + ret[i] = colorValues[colorValuesPosition++]; + } + + return ret; + } + + static void ComputeEndpoints( + AstcPixel[] endPoints, + int[] colorValues, + uint colorEndpointMode, + ref int colorValuesPosition) + { + switch (colorEndpointMode) + { + case 0: + { + uint[] val = ReadUintColorValues(2, colorValues, ref colorValuesPosition); + + endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[0], (short)val[0]); + endPoints[1] = new AstcPixel(0xFF, (short)val[1], (short)val[1], (short)val[1]); + + break; + } + + + case 1: + { + uint[] val = ReadUintColorValues(2, colorValues, ref colorValuesPosition); + int l0 = (int)((val[0] >> 2) | (val[1] & 0xC0)); + int l1 = (int)Math.Max(l0 + (val[1] & 0x3F), 0xFFU); + + endPoints[0] = new AstcPixel(0xFF, (short)l0, (short)l0, (short)l0); + endPoints[1] = new AstcPixel(0xFF, (short)l1, (short)l1, (short)l1); + + break; + } + + case 4: + { + uint[] val = ReadUintColorValues(4, colorValues, ref colorValuesPosition); + + endPoints[0] = new AstcPixel((short)val[2], (short)val[0], (short)val[0], (short)val[0]); + endPoints[1] = new AstcPixel((short)val[3], (short)val[1], (short)val[1], (short)val[1]); + + break; + } + + case 5: + { + int[] val = ReadIntColorValues(4, colorValues, ref colorValuesPosition); + + BitArrayStream.BitTransferSigned(ref val[1], ref val[0]); + BitArrayStream.BitTransferSigned(ref val[3], ref val[2]); + + endPoints[0] = new AstcPixel((short)val[2], (short)val[0], (short)val[0], (short)val[0]); + endPoints[1] = new AstcPixel((short)(val[2] + val[3]), (short)(val[0] + val[1]), (short)(val[0] + val[1]), (short)(val[0] + val[1])); + + endPoints[0].ClampByte(); + endPoints[1].ClampByte(); + + break; + } + + case 6: + { + uint[] val = ReadUintColorValues(4, colorValues, ref colorValuesPosition); + + endPoints[0] = new AstcPixel(0xFF, (short)(val[0] * val[3] >> 8), (short)(val[1] * val[3] >> 8), (short)(val[2] * val[3] >> 8)); + endPoints[1] = new AstcPixel(0xFF, (short)val[0], (short)val[1], (short)val[2]); + + break; + } + + case 8: + { + uint[] val = ReadUintColorValues(6, colorValues, ref colorValuesPosition); + + if (val[1] + val[3] + val[5] >= val[0] + val[2] + val[4]) + { + endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[2], (short)val[4]); + endPoints[1] = new AstcPixel(0xFF, (short)val[1], (short)val[3], (short)val[5]); + } + else + { + endPoints[0] = AstcPixel.BlueContract(0xFF, (short)val[1], (short)val[3], (short)val[5]); + endPoints[1] = AstcPixel.BlueContract(0xFF, (short)val[0], (short)val[2], (short)val[4]); + } + + break; + } + + case 9: + { + int[] val = ReadIntColorValues(6, colorValues, ref colorValuesPosition); + + BitArrayStream.BitTransferSigned(ref val[1], ref val[0]); + BitArrayStream.BitTransferSigned(ref val[3], ref val[2]); + BitArrayStream.BitTransferSigned(ref val[5], ref val[4]); + + if (val[1] + val[3] + val[5] >= 0) + { + endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[2], (short)val[4]); + endPoints[1] = new AstcPixel(0xFF, (short)(val[0] + val[1]), (short)(val[2] + val[3]), (short)(val[4] + val[5])); + } + else + { + endPoints[0] = AstcPixel.BlueContract(0xFF, val[0] + val[1], val[2] + val[3], val[4] + val[5]); + endPoints[1] = AstcPixel.BlueContract(0xFF, val[0], val[2], val[4]); + } + + endPoints[0].ClampByte(); + endPoints[1].ClampByte(); + + break; + } + + case 10: + { + uint[] val = ReadUintColorValues(6, colorValues, ref colorValuesPosition); + + endPoints[0] = new AstcPixel((short)val[4], (short)(val[0] * val[3] >> 8), (short)(val[1] * val[3] >> 8), (short)(val[2] * val[3] >> 8)); + endPoints[1] = new AstcPixel((short)val[5], (short)val[0], (short)val[1], (short)val[2]); + + break; + } + + case 12: + { + uint[] val = ReadUintColorValues(8, colorValues, ref colorValuesPosition); + + if (val[1] + val[3] + val[5] >= val[0] + val[2] + val[4]) + { + endPoints[0] = new AstcPixel((short)val[6], (short)val[0], (short)val[2], (short)val[4]); + endPoints[1] = new AstcPixel((short)val[7], (short)val[1], (short)val[3], (short)val[5]); + } + else + { + endPoints[0] = AstcPixel.BlueContract((short)val[7], (short)val[1], (short)val[3], (short)val[5]); + endPoints[1] = AstcPixel.BlueContract((short)val[6], (short)val[0], (short)val[2], (short)val[4]); + } + + break; + } + + case 13: + { + int[] val = ReadIntColorValues(8, colorValues, ref colorValuesPosition); + + BitArrayStream.BitTransferSigned(ref val[1], ref val[0]); + BitArrayStream.BitTransferSigned(ref val[3], ref val[2]); + BitArrayStream.BitTransferSigned(ref val[5], ref val[4]); + BitArrayStream.BitTransferSigned(ref val[7], ref val[6]); + + if (val[1] + val[3] + val[5] >= 0) + { + endPoints[0] = new AstcPixel((short)val[6], (short)val[0], (short)val[2], (short)val[4]); + endPoints[1] = new AstcPixel((short)(val[7] + val[6]), (short)(val[0] + val[1]), (short)(val[2] + val[3]), (short)(val[4] + val[5])); + } + else + { + endPoints[0] = AstcPixel.BlueContract(val[6] + val[7], val[0] + val[1], val[2] + val[3], val[4] + val[5]); + endPoints[1] = AstcPixel.BlueContract(val[6], val[0], val[2], val[4]); + } + + endPoints[0].ClampByte(); + endPoints[1].ClampByte(); + + break; + } + + default: + throw new AstcDecoderException("Unsupported color endpoint mode (is it HDR?)"); + } + } + + static void DecodeColorValues( + int[] outputValues, + byte[] inputData, + uint[] modes, + int numberPartitions, + int numberBitsForColorData) + { + // First figure out how many color values we have + int numberValues = 0; + + for (int i = 0; i < numberPartitions; i++) + { + numberValues += (int)((modes[i] >> 2) + 1) << 1; + } + + // Then based on the number of values and the remaining number of bits, + // figure out the max value for each of them... + int range = 256; + + while (--range > 0) + { + IntegerEncoded intEncoded = IntegerEncoded.CreateEncoding(range); + int bitLength = intEncoded.GetBitLength(numberValues); + + if (bitLength <= numberBitsForColorData) + { + // Find the smallest possible range that matches the given encoding + while (--range > 0) + { + IntegerEncoded newIntEncoded = IntegerEncoded.CreateEncoding(range); + if (!newIntEncoded.MatchesEncoding(intEncoded)) + { + break; + } + } + + // Return to last matching range. + range++; + break; + } + } + + // We now have enough to decode our integer sequence. + List<IntegerEncoded> integerEncodedSequence = new List<IntegerEncoded>(); + BitArrayStream colorBitStream = new BitArrayStream(new BitArray(inputData)); + + IntegerEncoded.DecodeIntegerSequence(integerEncodedSequence, colorBitStream, range, numberValues); + + // Once we have the decoded values, we need to dequantize them to the 0-255 range + // This procedure is outlined in ASTC spec C.2.13 + int outputIndices = 0; + + foreach (IntegerEncoded intEncoded in integerEncodedSequence) + { + int bitLength = intEncoded.NumberBits; + int bitValue = intEncoded.BitValue; + + Debug.Assert(bitLength >= 1); + + int a = 0, b = 0, c = 0, d = 0; + // A is just the lsb replicated 9 times. + a = BitArrayStream.Replicate(bitValue & 1, 1, 9); + + switch (intEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + { + outputValues[outputIndices++] = BitArrayStream.Replicate(bitValue, bitLength, 8); + + break; + } + + case IntegerEncoded.EIntegerEncoding.Trit: + { + d = intEncoded.TritValue; + + switch (bitLength) + { + case 1: + { + c = 204; + + break; + } + + case 2: + { + c = 93; + // B = b000b0bb0 + int b2 = (bitValue >> 1) & 1; + b = (b2 << 8) | (b2 << 4) | (b2 << 2) | (b2 << 1); + + break; + } + + case 3: + { + c = 44; + // B = cb000cbcb + int cb = (bitValue >> 1) & 3; + b = (cb << 7) | (cb << 2) | cb; + + break; + } + + + case 4: + { + c = 22; + // B = dcb000dcb + int dcb = (bitValue >> 1) & 7; + b = (dcb << 6) | dcb; + + break; + } + + case 5: + { + c = 11; + // B = edcb000ed + int edcb = (bitValue >> 1) & 0xF; + b = (edcb << 5) | (edcb >> 2); + + break; + } + + case 6: + { + c = 5; + // B = fedcb000f + int fedcb = (bitValue >> 1) & 0x1F; + b = (fedcb << 4) | (fedcb >> 4); + + break; + } + + default: + throw new AstcDecoderException("Unsupported trit encoding for color values!"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + d = intEncoded.QuintValue; + + switch (bitLength) + { + case 1: + { + c = 113; + + break; + } + + case 2: + { + c = 54; + // B = b0000bb00 + int b2 = (bitValue >> 1) & 1; + b = (b2 << 8) | (b2 << 3) | (b2 << 2); + + break; + } + + case 3: + { + c = 26; + // B = cb0000cbc + int cb = (bitValue >> 1) & 3; + b = (cb << 7) | (cb << 1) | (cb >> 1); + + break; + } + + case 4: + { + c = 13; + // B = dcb0000dc + int dcb = (bitValue >> 1) & 7; + b = (dcb << 6) | (dcb >> 1); + + break; + } + + case 5: + { + c = 6; + // B = edcb0000e + int edcb = (bitValue >> 1) & 0xF; + b = (edcb << 5) | (edcb >> 3); + + break; + } + + default: + throw new AstcDecoderException("Unsupported quint encoding for color values!"); + } + break; + } + } + + if (intEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits) + { + int T = d * c + b; + T ^= a; + T = (a & 0x80) | (T >> 2); + + outputValues[outputIndices++] = T; + } + } + + // Make sure that each of our values is in the proper range... + for (int i = 0; i < numberValues; i++) + { + Debug.Assert(outputValues[i] <= 255); + } + } + + static void FillVoidExtentLdr(BitArrayStream bitStream, int[] outputBuffer, int blockWidth, int blockHeight) + { + // Don't actually care about the void extent, just read the bits... + for (int i = 0; i < 4; ++i) + { + bitStream.ReadBits(13); + } + + // Decode the RGBA components and renormalize them to the range [0, 255] + ushort r = (ushort)bitStream.ReadBits(16); + ushort g = (ushort)bitStream.ReadBits(16); + ushort b = (ushort)bitStream.ReadBits(16); + ushort a = (ushort)bitStream.ReadBits(16); + + int rgba = (r >> 8) | (g & 0xFF00) | ((b) & 0xFF00) << 8 | ((a) & 0xFF00) << 16; + + for (int j = 0; j < blockHeight; j++) + { + for (int i = 0; i < blockWidth; i++) + { + outputBuffer[j * blockWidth + i] = rgba; + } + } + } + + static TexelWeightParams DecodeBlockInfo(BitArrayStream bitStream) + { + TexelWeightParams texelParams = new TexelWeightParams(); + + // Read the entire block mode all at once + ushort modeBits = (ushort)bitStream.ReadBits(11); + + // Does this match the void extent block mode? + if ((modeBits & 0x01FF) == 0x1FC) + { + if ((modeBits & 0x200) != 0) + { + texelParams.VoidExtentHdr = true; + } + else + { + texelParams.VoidExtentLdr = true; + } + + // Next two bits must be one. + if ((modeBits & 0x400) == 0 || bitStream.ReadBits(1) == 0) + { + texelParams.Error = true; + } + + return texelParams; + } + + // First check if the last four bits are zero + if ((modeBits & 0xF) == 0) + { + texelParams.Error = true; + return texelParams; + } + + // If the last two bits are zero, then if bits + // [6-8] are all ones, this is also reserved. + if ((modeBits & 0x3) == 0 && (modeBits & 0x1C0) == 0x1C0) + { + texelParams.Error = true; + + return texelParams; + } + + // Otherwise, there is no error... Figure out the layout + // of the block mode. Layout is determined by a number + // between 0 and 9 corresponding to table C.2.8 of the + // ASTC spec. + int layout = 0; + + if ((modeBits & 0x1) != 0 || (modeBits & 0x2) != 0) + { + // layout is in [0-4] + if ((modeBits & 0x8) != 0) + { + // layout is in [2-4] + if ((modeBits & 0x4) != 0) + { + // layout is in [3-4] + if ((modeBits & 0x100) != 0) + { + layout = 4; + } + else + { + layout = 3; + } + } + else + { + layout = 2; + } + } + else + { + // layout is in [0-1] + if ((modeBits & 0x4) != 0) + { + layout = 1; + } + else + { + layout = 0; + } + } + } + else + { + // layout is in [5-9] + if ((modeBits & 0x100) != 0) + { + // layout is in [7-9] + if ((modeBits & 0x80) != 0) + { + // layout is in [7-8] + Debug.Assert((modeBits & 0x40) == 0); + + if ((modeBits & 0x20) != 0) + { + layout = 8; + } + else + { + layout = 7; + } + } + else + { + layout = 9; + } + } + else + { + // layout is in [5-6] + if ((modeBits & 0x80) != 0) + { + layout = 6; + } + else + { + layout = 5; + } + } + } + + Debug.Assert(layout < 10); + + // Determine R + int r = (modeBits >> 4) & 1; + if (layout < 5) + { + r |= (modeBits & 0x3) << 1; + } + else + { + r |= (modeBits & 0xC) >> 1; + } + + Debug.Assert(2 <= r && r <= 7); + + // Determine width & height + switch (layout) + { + case 0: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 7) & 0x3; + + texelParams.Width = b + 4; + texelParams.Height = a + 2; + + break; + } + + case 1: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 7) & 0x3; + + texelParams.Width = b + 8; + texelParams.Height = a + 2; + + break; + } + + case 2: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 7) & 0x3; + + texelParams.Width = a + 2; + texelParams.Height = b + 8; + + break; + } + + case 3: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 7) & 0x1; + + texelParams.Width = a + 2; + texelParams.Height = b + 6; + + break; + } + + case 4: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 7) & 0x1; + + texelParams.Width = b + 2; + texelParams.Height = a + 2; + + break; + } + + case 5: + { + int a = (modeBits >> 5) & 0x3; + + texelParams.Width = 12; + texelParams.Height = a + 2; + + break; + } + + case 6: + { + int a = (modeBits >> 5) & 0x3; + + texelParams.Width = a + 2; + texelParams.Height = 12; + + break; + } + + case 7: + { + texelParams.Width = 6; + texelParams.Height = 10; + + break; + } + + case 8: + { + texelParams.Width = 10; + texelParams.Height = 6; + break; + } + + case 9: + { + int a = (modeBits >> 5) & 0x3; + int b = (modeBits >> 9) & 0x3; + + texelParams.Width = a + 6; + texelParams.Height = b + 6; + + break; + } + + default: + //Don't know this layout... + texelParams.Error = true; + break; + } + + // Determine whether or not we're using dual planes + // and/or high precision layouts. + bool d = ((layout != 9) && ((modeBits & 0x400) != 0)); + bool h = (layout != 9) && ((modeBits & 0x200) != 0); + + if (h) + { + int[] maxWeights = { 9, 11, 15, 19, 23, 31 }; + texelParams.MaxWeight = maxWeights[r - 2]; + } + else + { + int[] maxWeights = { 1, 2, 3, 4, 5, 7 }; + texelParams.MaxWeight = maxWeights[r - 2]; + } + + texelParams.DualPlane = d; + + return texelParams; + } + } +} diff --git a/Ryujinx.Graphics/Graphics3d/Texture/AstcPixel.cs b/Ryujinx.Graphics/Graphics3d/Texture/AstcPixel.cs new file mode 100644 index 00000000..cd30acca --- /dev/null +++ b/Ryujinx.Graphics/Graphics3d/Texture/AstcPixel.cs @@ -0,0 +1,138 @@ +using System; +using System.Diagnostics; + +namespace Ryujinx.Graphics.Texture +{ + class AstcPixel + { + public short R { get; set; } + public short G { get; set; } + public short B { get; set; } + public short A { get; set; } + + byte[] _bitDepth = new byte[4]; + + public AstcPixel(short a, short r, short g, short b) + { + A = a; + R = r; + G = g; + B = b; + + for (int i = 0; i < 4; i++) + _bitDepth[i] = 8; + } + + public void ClampByte() + { + R = Math.Min(Math.Max(R, (short)0), (short)255); + G = Math.Min(Math.Max(G, (short)0), (short)255); + B = Math.Min(Math.Max(B, (short)0), (short)255); + A = Math.Min(Math.Max(A, (short)0), (short)255); + } + + public short GetComponent(int index) + { + switch(index) + { + case 0: return A; + case 1: return R; + case 2: return G; + case 3: return B; + } + + return 0; + } + + public void SetComponent(int index, int value) + { + switch (index) + { + case 0: + A = (short)value; + break; + case 1: + R = (short)value; + break; + case 2: + G = (short)value; + break; + case 3: + B = (short)value; + break; + } + } + + public void ChangeBitDepth(byte[] depth) + { + for(int i = 0; i< 4; i++) + { + int value = ChangeBitDepth(GetComponent(i), _bitDepth[i], depth[i]); + + SetComponent(i, value); + _bitDepth[i] = depth[i]; + } + } + + short ChangeBitDepth(short value, byte oldDepth, byte newDepth) + { + Debug.Assert(newDepth <= 8); + Debug.Assert(oldDepth <= 8); + + if (oldDepth == newDepth) + { + // Do nothing + return value; + } + else if (oldDepth == 0 && newDepth != 0) + { + return (short)((1 << newDepth) - 1); + } + else if (newDepth > oldDepth) + { + return (short)BitArrayStream.Replicate(value, oldDepth, newDepth); + } + else + { + // oldDepth > newDepth + if (newDepth == 0) + { + return 0xFF; + } + else + { + byte bitsWasted = (byte)(oldDepth - newDepth); + short tempValue = value; + + tempValue = (short)((tempValue + (1 << (bitsWasted - 1))) >> bitsWasted); + tempValue = Math.Min(Math.Max((short)0, tempValue), (short)((1 << newDepth) - 1)); + + return (byte)(tempValue); + } + } + } + + public int Pack() + { + AstcPixel newPixel = new AstcPixel(A, R, G, B); + byte[] eightBitDepth = { 8, 8, 8, 8 }; + + newPixel.ChangeBitDepth(eightBitDepth); + + return (byte)newPixel.A << 24 | + (byte)newPixel.B << 16 | + (byte)newPixel.G << 8 | + (byte)newPixel.R << 0; + } + + // Adds more precision to the blue channel as described + // in C.2.14 + public static AstcPixel BlueContract(int a, int r, int g, int b) + { + return new AstcPixel((short)(a), + (short)((r + b) >> 1), + (short)((g + b) >> 1), + (short)(b)); + } + } +} diff --git a/Ryujinx.Graphics/Graphics3d/Texture/BitArrayStream.cs b/Ryujinx.Graphics/Graphics3d/Texture/BitArrayStream.cs index 2a8ed091..24069d72 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/BitArrayStream.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/BitArrayStream.cs @@ -9,103 +9,103 @@ namespace Ryujinx.Graphics.Texture public int Position { get; private set; } - public BitArrayStream(BitArray BitArray) + public BitArrayStream(BitArray bitArray) { - BitsArray = BitArray; + BitsArray = bitArray; Position = 0; } - public short ReadBits(int Length) + public short ReadBits(int length) { - int RetValue = 0; - for (int i = Position; i < Position + Length; i++) + int retValue = 0; + for (int i = Position; i < Position + length; i++) { if (BitsArray[i]) { - RetValue |= 1 << (i - Position); + retValue |= 1 << (i - Position); } } - Position += Length; - return (short)RetValue; + Position += length; + return (short)retValue; } - public int ReadBits(int Start, int End) + public int ReadBits(int start, int end) { - int RetValue = 0; - for (int i = Start; i <= End; i++) + int retValue = 0; + for (int i = start; i <= end; i++) { if (BitsArray[i]) { - RetValue |= 1 << (i - Start); + retValue |= 1 << (i - start); } } - return RetValue; + return retValue; } - public int ReadBit(int Index) + public int ReadBit(int index) { - return Convert.ToInt32(BitsArray[Index]); + return Convert.ToInt32(BitsArray[index]); } - public void WriteBits(int Value, int Length) + public void WriteBits(int value, int length) { - for (int i = Position; i < Position + Length; i++) + for (int i = Position; i < Position + length; i++) { - BitsArray[i] = ((Value >> (i - Position)) & 1) != 0; + BitsArray[i] = ((value >> (i - Position)) & 1) != 0; } - Position += Length; + Position += length; } public byte[] ToByteArray() { - byte[] RetArray = new byte[(BitsArray.Length + 7) / 8]; - BitsArray.CopyTo(RetArray, 0); - return RetArray; + byte[] retArray = new byte[(BitsArray.Length + 7) / 8]; + BitsArray.CopyTo(retArray, 0); + return retArray; } - public static int Replicate(int Value, int NumberBits, int ToBit) + public static int Replicate(int value, int numberBits, int toBit) { - if (NumberBits == 0) return 0; - if (ToBit == 0) return 0; + if (numberBits == 0) return 0; + if (toBit == 0) return 0; - int TempValue = Value & ((1 << NumberBits) - 1); - int RetValue = TempValue; - int ResLength = NumberBits; + int tempValue = value & ((1 << numberBits) - 1); + int retValue = tempValue; + int resLength = numberBits; - while (ResLength < ToBit) + while (resLength < toBit) { - int Comp = 0; - if (NumberBits > ToBit - ResLength) + int comp = 0; + if (numberBits > toBit - resLength) { - int NewShift = ToBit - ResLength; - Comp = NumberBits - NewShift; - NumberBits = NewShift; + int newShift = toBit - resLength; + comp = numberBits - newShift; + numberBits = newShift; } - RetValue <<= NumberBits; - RetValue |= TempValue >> Comp; - ResLength += NumberBits; + retValue <<= numberBits; + retValue |= tempValue >> comp; + resLength += numberBits; } - return RetValue; + return retValue; } - public static int PopCnt(int Number) + public static int PopCnt(int number) { - int Counter; - for (Counter = 0; Number != 0; Counter++) + int counter; + for (counter = 0; number != 0; counter++) { - Number &= Number - 1; + number &= number - 1; } - return Counter; + return counter; } public static void Swap<T>(ref T lhs, ref T rhs) { - T Temp = lhs; + T temp = lhs; lhs = rhs; - rhs = Temp; + rhs = temp; } // Transfers a bit as described in C.2.14 diff --git a/Ryujinx.Graphics/Graphics3d/Texture/BlockLinearSwizzle.cs b/Ryujinx.Graphics/Graphics3d/Texture/BlockLinearSwizzle.cs index 1be06442..682f7d67 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/BlockLinearSwizzle.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/BlockLinearSwizzle.cs @@ -10,98 +10,98 @@ namespace Ryujinx.Graphics.Texture private const int GobSize = GobWidth * GobHeight; - private int TexWidth; - private int TexHeight; - private int TexDepth; - private int TexGobBlockHeight; - private int TexGobBlockDepth; - private int TexBpp; + private int _texWidth; + private int _texHeight; + private int _texDepth; + private int _texGobBlockHeight; + private int _texGobBlockDepth; + private int _texBpp; - private int BhMask; - private int BdMask; + private int _bhMask; + private int _bdMask; - private int BhShift; - private int BdShift; - private int BppShift; + private int _bhShift; + private int _bdShift; + private int _bppShift; - private int XShift; + private int _xShift; - private int RobSize; - private int SliceSize; + private int _robSize; + private int _sliceSize; - private int BaseOffset; + private int _baseOffset; public BlockLinearSwizzle( - int Width, - int Height, - int Depth, - int GobBlockHeight, - int GobBlockDepth, - int Bpp) + int width, + int height, + int depth, + int gobBlockHeight, + int gobBlockDepth, + int bpp) { - TexWidth = Width; - TexHeight = Height; - TexDepth = Depth; - TexGobBlockHeight = GobBlockHeight; - TexGobBlockDepth = GobBlockDepth; - TexBpp = Bpp; + _texWidth = width; + _texHeight = height; + _texDepth = depth; + _texGobBlockHeight = gobBlockHeight; + _texGobBlockDepth = gobBlockDepth; + _texBpp = bpp; - BppShift = BitUtils.CountTrailingZeros32(Bpp); + _bppShift = BitUtils.CountTrailingZeros32(bpp); SetMipLevel(0); } - public void SetMipLevel(int Level) + public void SetMipLevel(int level) { - BaseOffset = GetMipOffset(Level); + _baseOffset = GetMipOffset(level); - int Width = Math.Max(1, TexWidth >> Level); - int Height = Math.Max(1, TexHeight >> Level); - int Depth = Math.Max(1, TexDepth >> Level); + int width = Math.Max(1, _texWidth >> level); + int height = Math.Max(1, _texHeight >> level); + int depth = Math.Max(1, _texDepth >> level); - GobBlockSizes GbSizes = AdjustGobBlockSizes(Height, Depth); + GobBlockSizes gbSizes = AdjustGobBlockSizes(height, depth); - BhMask = GbSizes.Height - 1; - BdMask = GbSizes.Depth - 1; + _bhMask = gbSizes.Height - 1; + _bdMask = gbSizes.Depth - 1; - BhShift = BitUtils.CountTrailingZeros32(GbSizes.Height); - BdShift = BitUtils.CountTrailingZeros32(GbSizes.Depth); + _bhShift = BitUtils.CountTrailingZeros32(gbSizes.Height); + _bdShift = BitUtils.CountTrailingZeros32(gbSizes.Depth); - XShift = BitUtils.CountTrailingZeros32(GobSize * GbSizes.Height * GbSizes.Depth); + _xShift = BitUtils.CountTrailingZeros32(GobSize * gbSizes.Height * gbSizes.Depth); - RobAndSliceSizes GsSizes = GetRobAndSliceSizes(Width, Height, GbSizes); + RobAndSliceSizes gsSizes = GetRobAndSliceSizes(width, height, gbSizes); - RobSize = GsSizes.RobSize; - SliceSize = GsSizes.SliceSize; + _robSize = gsSizes.RobSize; + _sliceSize = gsSizes.SliceSize; } - public int GetImageSize(int MipsCount) + public int GetImageSize(int mipsCount) { - int Size = GetMipOffset(MipsCount); + int size = GetMipOffset(mipsCount); - Size = (Size + 0x1fff) & ~0x1fff; + size = (size + 0x1fff) & ~0x1fff; - return Size; + return size; } - public int GetMipOffset(int Level) + public int GetMipOffset(int level) { - int TotalSize = 0; + int totalSize = 0; - for (int Index = 0; Index < Level; Index++) + for (int index = 0; index < level; index++) { - int Width = Math.Max(1, TexWidth >> Index); - int Height = Math.Max(1, TexHeight >> Index); - int Depth = Math.Max(1, TexDepth >> Index); + int width = Math.Max(1, _texWidth >> index); + int height = Math.Max(1, _texHeight >> index); + int depth = Math.Max(1, _texDepth >> index); - GobBlockSizes GbSizes = AdjustGobBlockSizes(Height, Depth); + GobBlockSizes gbSizes = AdjustGobBlockSizes(height, depth); - RobAndSliceSizes RsSizes = GetRobAndSliceSizes(Width, Height, GbSizes); + RobAndSliceSizes rsSizes = GetRobAndSliceSizes(width, height, gbSizes); - TotalSize += BitUtils.DivRoundUp(Depth, GbSizes.Depth) * RsSizes.SliceSize; + totalSize += BitUtils.DivRoundUp(depth, gbSizes.Depth) * rsSizes.SliceSize; } - return TotalSize; + return totalSize; } private struct GobBlockSizes @@ -109,32 +109,32 @@ namespace Ryujinx.Graphics.Texture public int Height; public int Depth; - public GobBlockSizes(int GobBlockHeight, int GobBlockDepth) + public GobBlockSizes(int gobBlockHeight, int gobBlockDepth) { - this.Height = GobBlockHeight; - this.Depth = GobBlockDepth; + Height = gobBlockHeight; + Depth = gobBlockDepth; } } - private GobBlockSizes AdjustGobBlockSizes(int Height, int Depth) + private GobBlockSizes AdjustGobBlockSizes(int height, int depth) { - int GobBlockHeight = TexGobBlockHeight; - int GobBlockDepth = TexGobBlockDepth; + int gobBlockHeight = _texGobBlockHeight; + int gobBlockDepth = _texGobBlockDepth; - int Pow2Height = BitUtils.Pow2RoundUp(Height); - int Pow2Depth = BitUtils.Pow2RoundUp(Depth); + int pow2Height = BitUtils.Pow2RoundUp(height); + int pow2Depth = BitUtils.Pow2RoundUp(depth); - while (GobBlockHeight * GobHeight > Pow2Height && GobBlockHeight > 1) + while (gobBlockHeight * GobHeight > pow2Height && gobBlockHeight > 1) { - GobBlockHeight >>= 1; + gobBlockHeight >>= 1; } - while (GobBlockDepth > Pow2Depth && GobBlockDepth > 1) + while (gobBlockDepth > pow2Depth && gobBlockDepth > 1) { - GobBlockDepth >>= 1; + gobBlockDepth >>= 1; } - return new GobBlockSizes(GobBlockHeight, GobBlockDepth); + return new GobBlockSizes(gobBlockHeight, gobBlockDepth); } private struct RobAndSliceSizes @@ -142,45 +142,45 @@ namespace Ryujinx.Graphics.Texture public int RobSize; public int SliceSize; - public RobAndSliceSizes(int RobSize, int SliceSize) + public RobAndSliceSizes(int robSize, int sliceSize) { - this.RobSize = RobSize; - this.SliceSize = SliceSize; + RobSize = robSize; + SliceSize = sliceSize; } } - private RobAndSliceSizes GetRobAndSliceSizes(int Width, int Height, GobBlockSizes GbSizes) + private RobAndSliceSizes GetRobAndSliceSizes(int width, int height, GobBlockSizes gbSizes) { - int WidthInGobs = BitUtils.DivRoundUp(Width * TexBpp, GobWidth); + int widthInGobs = BitUtils.DivRoundUp(width * _texBpp, GobWidth); - int RobSize = GobSize * GbSizes.Height * GbSizes.Depth * WidthInGobs; + int robSize = GobSize * gbSizes.Height * gbSizes.Depth * widthInGobs; - int SliceSize = BitUtils.DivRoundUp(Height, GbSizes.Height * GobHeight) * RobSize; + int sliceSize = BitUtils.DivRoundUp(height, gbSizes.Height * GobHeight) * robSize; - return new RobAndSliceSizes(RobSize, SliceSize); + return new RobAndSliceSizes(robSize, sliceSize); } - public int GetSwizzleOffset(int X, int Y, int Z) + public int GetSwizzleOffset(int x, int y, int z) { - X <<= BppShift; + x <<= _bppShift; - int YH = Y / GobHeight; + int yh = y / GobHeight; - int Position = (Z >> BdShift) * SliceSize + (YH >> BhShift) * RobSize; + int position = (z >> _bdShift) * _sliceSize + (yh >> _bhShift) * _robSize; - Position += (X / GobWidth) << XShift; + position += (x / GobWidth) << _xShift; - Position += (YH & BhMask) * GobSize; + position += (yh & _bhMask) * GobSize; - Position += ((Z & BdMask) * GobSize) << BhShift; + position += ((z & _bdMask) * GobSize) << _bhShift; - Position += ((X & 0x3f) >> 5) << 8; - Position += ((Y & 0x07) >> 1) << 6; - Position += ((X & 0x1f) >> 4) << 5; - Position += ((Y & 0x01) >> 0) << 4; - Position += ((X & 0x0f) >> 0) << 0; + position += ((x & 0x3f) >> 5) << 8; + position += ((y & 0x07) >> 1) << 6; + position += ((x & 0x1f) >> 4) << 5; + position += ((y & 0x01) >> 0) << 4; + position += ((x & 0x0f) >> 0) << 0; - return BaseOffset + Position; + return _baseOffset + position; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/Texture/ISwizzle.cs b/Ryujinx.Graphics/Graphics3d/Texture/ISwizzle.cs index 2e0e8aed..fae3eada 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/ISwizzle.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/ISwizzle.cs @@ -2,12 +2,12 @@ namespace Ryujinx.Graphics.Texture { interface ISwizzle { - int GetSwizzleOffset(int X, int Y, int Z); + int GetSwizzleOffset(int x, int y, int z); - void SetMipLevel(int Level); + void SetMipLevel(int level); - int GetMipOffset(int Level); + int GetMipOffset(int level); - int GetImageSize(int MipsCount); + int GetImageSize(int mipsCount); } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/Texture/ImageUtils.cs b/Ryujinx.Graphics/Graphics3d/Texture/ImageUtils.cs index c4208935..bd9b4327 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/ImageUtils.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/ImageUtils.cs @@ -1,6 +1,5 @@ using ChocolArm64.Memory; using OpenTK.Graphics.OpenGL; -using Ryujinx.Common; using Ryujinx.Graphics.Gal; using Ryujinx.Graphics.Memory; using System; @@ -29,13 +28,13 @@ namespace Ryujinx.Graphics.Texture public TargetBuffer Target { get; private set; } - public ImageDescriptor(int BytesPerPixel, int BlockWidth, int BlockHeight, int BlockDepth, TargetBuffer Target) + public ImageDescriptor(int bytesPerPixel, int blockWidth, int blockHeight, int blockDepth, TargetBuffer target) { - this.BytesPerPixel = BytesPerPixel; - this.BlockWidth = BlockWidth; - this.BlockHeight = BlockHeight; - this.BlockDepth = BlockDepth; - this.Target = Target; + BytesPerPixel = bytesPerPixel; + BlockWidth = blockWidth; + BlockHeight = blockHeight; + BlockDepth = blockDepth; + Target = target; } } @@ -46,26 +45,26 @@ namespace Ryujinx.Graphics.Texture private const GalImageFormat Float = GalImageFormat.Float; private const GalImageFormat Srgb = GalImageFormat.Srgb; - private static readonly Dictionary<GalTextureFormat, GalImageFormat> s_TextureTable = + private static readonly Dictionary<GalTextureFormat, GalImageFormat> TextureTable = new Dictionary<GalTextureFormat, GalImageFormat>() { - { GalTextureFormat.RGBA32, GalImageFormat.RGBA32 | Sint | Uint | Float }, - { GalTextureFormat.RGBA16, GalImageFormat.RGBA16 | Snorm | Unorm | Sint | Uint | Float }, - { GalTextureFormat.RG32, GalImageFormat.RG32 | Sint | Uint | Float }, - { GalTextureFormat.RGBA8, GalImageFormat.RGBA8 | Snorm | Unorm | Sint | Uint | Srgb }, - { GalTextureFormat.RGB10A2, GalImageFormat.RGB10A2 | Snorm | Unorm | Sint | Uint }, - { GalTextureFormat.RG8, GalImageFormat.RG8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.Rgba32, GalImageFormat.Rgba32 | Sint | Uint | Float }, + { GalTextureFormat.Rgba16, GalImageFormat.Rgba16 | Snorm | Unorm | Sint | Uint | Float }, + { GalTextureFormat.Rg32, GalImageFormat.Rg32 | Sint | Uint | Float }, + { GalTextureFormat.Rgba8, GalImageFormat.Rgba8 | Snorm | Unorm | Sint | Uint | Srgb }, + { GalTextureFormat.Rgb10A2, GalImageFormat.Rgb10A2 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.Rg8, GalImageFormat.Rg8 | Snorm | Unorm | Sint | Uint }, { GalTextureFormat.R16, GalImageFormat.R16 | Snorm | Unorm | Sint | Uint | Float }, { GalTextureFormat.R8, GalImageFormat.R8 | Snorm | Unorm | Sint | Uint }, - { GalTextureFormat.RG16, GalImageFormat.RG16 | Snorm | Unorm | Sint | Float }, + { GalTextureFormat.Rg16, GalImageFormat.Rg16 | Snorm | Unorm | Sint | Float }, { GalTextureFormat.R32, GalImageFormat.R32 | Sint | Uint | Float }, - { GalTextureFormat.RGBA4, GalImageFormat.RGBA4 | Unorm }, - { GalTextureFormat.RGB5A1, GalImageFormat.RGB5A1 | Unorm }, - { GalTextureFormat.RGB565, GalImageFormat.RGB565 | Unorm }, + { GalTextureFormat.Rgba4, GalImageFormat.Rgba4 | Unorm }, + { GalTextureFormat.Rgb5A1, GalImageFormat.Rgb5A1 | Unorm }, + { GalTextureFormat.Rgb565, GalImageFormat.Rgb565 | Unorm }, { GalTextureFormat.R11G11B10F, GalImageFormat.R11G11B10 | Float }, { GalTextureFormat.D24S8, GalImageFormat.D24S8 | Unorm | Uint }, { GalTextureFormat.D32F, GalImageFormat.D32 | Float }, - { GalTextureFormat.D32FX24S8, GalImageFormat.D32S8 | Float }, + { GalTextureFormat.D32Fx24S8, GalImageFormat.D32S8 | Float }, { GalTextureFormat.D16, GalImageFormat.D16 | Unorm }, //Compressed formats @@ -93,27 +92,27 @@ namespace Ryujinx.Graphics.Texture { GalTextureFormat.Astc2D10x6, GalImageFormat.Astc2D10x6 | Unorm | Srgb } }; - private static readonly Dictionary<GalImageFormat, ImageDescriptor> s_ImageTable = + private static readonly Dictionary<GalImageFormat, ImageDescriptor> ImageTable = new Dictionary<GalImageFormat, ImageDescriptor>() { - { GalImageFormat.RGBA32, new ImageDescriptor(16, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGBA16, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RG32, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGBX8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGBA8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.BGRA8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGB10A2, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgba32, new ImageDescriptor(16, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgba16, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rg32, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgbx8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgba8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Bgra8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgb10A2, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.R32, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGBA4, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgba4, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.BptcSfloat, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) }, { GalImageFormat.BptcUfloat, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) }, - { GalImageFormat.BGR5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGB5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RGB565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.BGR565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Bgr5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgb5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rgb565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Bgr565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.BptcUnorm, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) }, - { GalImageFormat.RG16, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, - { GalImageFormat.RG8, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rg16, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, + { GalImageFormat.Rg8, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.R16, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.R8, new ImageDescriptor(1, 1, 1, 1, TargetBuffer.Color) }, { GalImageFormat.R11G11B10, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) }, @@ -145,77 +144,77 @@ namespace Ryujinx.Graphics.Texture }; public static GalImageFormat ConvertTexture( - GalTextureFormat Format, - GalTextureType RType, - GalTextureType GType, - GalTextureType BType, - GalTextureType AType, - bool ConvSrgb) + GalTextureFormat format, + GalTextureType rType, + GalTextureType gType, + GalTextureType bType, + GalTextureType aType, + bool convSrgb) { - if (!s_TextureTable.TryGetValue(Format, out GalImageFormat ImageFormat)) + if (!TextureTable.TryGetValue(format, out GalImageFormat imageFormat)) { - throw new NotImplementedException($"Format 0x{((int)Format):x} not implemented!"); + throw new NotImplementedException($"Format 0x{((int)format):x} not implemented!"); } - if (!HasDepth(ImageFormat) && (RType != GType || RType != BType || RType != AType)) + if (!HasDepth(imageFormat) && (rType != gType || rType != bType || rType != aType)) { - throw new NotImplementedException($"Per component types are not implemented!"); + throw new NotImplementedException("Per component types are not implemented!"); } - GalImageFormat FormatType = ConvSrgb ? Srgb : GetFormatType(RType); + GalImageFormat formatType = convSrgb ? Srgb : GetFormatType(rType); - GalImageFormat CombinedFormat = (ImageFormat & GalImageFormat.FormatMask) | FormatType; + GalImageFormat combinedFormat = (imageFormat & GalImageFormat.FormatMask) | formatType; - if (!ImageFormat.HasFlag(FormatType)) + if (!imageFormat.HasFlag(formatType)) { - throw new NotImplementedException($"Format \"{CombinedFormat}\" not implemented!"); + throw new NotImplementedException($"Format \"{combinedFormat}\" not implemented!"); } - return CombinedFormat; + return combinedFormat; } - public static GalImageFormat ConvertSurface(GalSurfaceFormat Format) + public static GalImageFormat ConvertSurface(GalSurfaceFormat format) { - switch (Format) + switch (format) { - case GalSurfaceFormat.RGBA32Float: return GalImageFormat.RGBA32 | Float; - case GalSurfaceFormat.RGBA32Uint: return GalImageFormat.RGBA32 | Uint; - case GalSurfaceFormat.RGBA16Float: return GalImageFormat.RGBA16 | Float; - case GalSurfaceFormat.RGBA16Unorm: return GalImageFormat.RGBA16 | Unorm; - case GalSurfaceFormat.RG32Float: return GalImageFormat.RG32 | Float; - case GalSurfaceFormat.RG32Sint: return GalImageFormat.RG32 | Sint; - case GalSurfaceFormat.RG32Uint: return GalImageFormat.RG32 | Uint; - case GalSurfaceFormat.BGRA8Unorm: return GalImageFormat.BGRA8 | Unorm; - case GalSurfaceFormat.BGRA8Srgb: return GalImageFormat.BGRA8 | Srgb; - case GalSurfaceFormat.RGB10A2Unorm: return GalImageFormat.RGB10A2 | Unorm; - case GalSurfaceFormat.RGBA8Unorm: return GalImageFormat.RGBA8 | Unorm; - case GalSurfaceFormat.RGBA8Srgb: return GalImageFormat.RGBA8 | Srgb; - case GalSurfaceFormat.RGBA8Snorm: return GalImageFormat.RGBA8 | Snorm; - case GalSurfaceFormat.RG16Snorm: return GalImageFormat.RG16 | Snorm; - case GalSurfaceFormat.RG16Unorm: return GalImageFormat.RG16 | Unorm; - case GalSurfaceFormat.RG16Sint: return GalImageFormat.RG16 | Sint; - case GalSurfaceFormat.RG16Float: return GalImageFormat.RG16 | Float; + case GalSurfaceFormat.Rgba32Float: return GalImageFormat.Rgba32 | Float; + case GalSurfaceFormat.Rgba32Uint: return GalImageFormat.Rgba32 | Uint; + case GalSurfaceFormat.Rgba16Float: return GalImageFormat.Rgba16 | Float; + case GalSurfaceFormat.Rgba16Unorm: return GalImageFormat.Rgba16 | Unorm; + case GalSurfaceFormat.Rg32Float: return GalImageFormat.Rg32 | Float; + case GalSurfaceFormat.Rg32Sint: return GalImageFormat.Rg32 | Sint; + case GalSurfaceFormat.Rg32Uint: return GalImageFormat.Rg32 | Uint; + case GalSurfaceFormat.Bgra8Unorm: return GalImageFormat.Bgra8 | Unorm; + case GalSurfaceFormat.Bgra8Srgb: return GalImageFormat.Bgra8 | Srgb; + case GalSurfaceFormat.Rgb10A2Unorm: return GalImageFormat.Rgb10A2 | Unorm; + case GalSurfaceFormat.Rgba8Unorm: return GalImageFormat.Rgba8 | Unorm; + case GalSurfaceFormat.Rgba8Srgb: return GalImageFormat.Rgba8 | Srgb; + case GalSurfaceFormat.Rgba8Snorm: return GalImageFormat.Rgba8 | Snorm; + case GalSurfaceFormat.Rg16Snorm: return GalImageFormat.Rg16 | Snorm; + case GalSurfaceFormat.Rg16Unorm: return GalImageFormat.Rg16 | Unorm; + case GalSurfaceFormat.Rg16Sint: return GalImageFormat.Rg16 | Sint; + case GalSurfaceFormat.Rg16Float: return GalImageFormat.Rg16 | Float; case GalSurfaceFormat.R11G11B10Float: return GalImageFormat.R11G11B10 | Float; case GalSurfaceFormat.R32Float: return GalImageFormat.R32 | Float; case GalSurfaceFormat.R32Uint: return GalImageFormat.R32 | Uint; - case GalSurfaceFormat.RG8Unorm: return GalImageFormat.RG8 | Unorm; - case GalSurfaceFormat.RG8Snorm: return GalImageFormat.RG8 | Snorm; + case GalSurfaceFormat.Rg8Unorm: return GalImageFormat.Rg8 | Unorm; + case GalSurfaceFormat.Rg8Snorm: return GalImageFormat.Rg8 | Snorm; case GalSurfaceFormat.R16Float: return GalImageFormat.R16 | Float; case GalSurfaceFormat.R16Unorm: return GalImageFormat.R16 | Unorm; case GalSurfaceFormat.R16Uint: return GalImageFormat.R16 | Uint; case GalSurfaceFormat.R8Unorm: return GalImageFormat.R8 | Unorm; case GalSurfaceFormat.R8Uint: return GalImageFormat.R8 | Uint; - case GalSurfaceFormat.B5G6R5Unorm: return GalImageFormat.RGB565 | Unorm; - case GalSurfaceFormat.BGR5A1Unorm: return GalImageFormat.BGR5A1 | Unorm; - case GalSurfaceFormat.RGBX8Unorm: return GalImageFormat.RGBX8 | Unorm; + case GalSurfaceFormat.B5G6R5Unorm: return GalImageFormat.Rgb565 | Unorm; + case GalSurfaceFormat.Bgr5A1Unorm: return GalImageFormat.Bgr5A1 | Unorm; + case GalSurfaceFormat.Rgbx8Unorm: return GalImageFormat.Rgbx8 | Unorm; } - throw new NotImplementedException(Format.ToString()); + throw new NotImplementedException(format.ToString()); } - public static GalImageFormat ConvertZeta(GalZetaFormat Format) + public static GalImageFormat ConvertZeta(GalZetaFormat format) { - switch (Format) + switch (format) { case GalZetaFormat.D32Float: return GalImageFormat.D32 | Float; case GalZetaFormat.S8D24Unorm: return GalImageFormat.D24S8 | Unorm; @@ -225,268 +224,268 @@ namespace Ryujinx.Graphics.Texture case GalZetaFormat.D32S8X24Float: return GalImageFormat.D32S8 | Float; } - throw new NotImplementedException(Format.ToString()); + throw new NotImplementedException(format.ToString()); } - public static byte[] ReadTexture(IMemory Memory, GalImage Image, long Position) + public static byte[] ReadTexture(IMemory memory, GalImage image, long position) { - MemoryManager CpuMemory; + MemoryManager cpuMemory; - if (Memory is NvGpuVmm Vmm) + if (memory is NvGpuVmm vmm) { - CpuMemory = Vmm.Memory; + cpuMemory = vmm.Memory; } else { - CpuMemory = (MemoryManager)Memory; + cpuMemory = (MemoryManager)memory; } - ISwizzle Swizzle = TextureHelper.GetSwizzle(Image); + ISwizzle swizzle = TextureHelper.GetSwizzle(image); - ImageDescriptor Desc = GetImageDescriptor(Image.Format); + ImageDescriptor desc = GetImageDescriptor(image.Format); - (int Width, int Height, int Depth) = GetImageSizeInBlocks(Image); + (int width, int height, int depth) = GetImageSizeInBlocks(image); - int BytesPerPixel = Desc.BytesPerPixel; + int bytesPerPixel = desc.BytesPerPixel; //Note: Each row of the texture needs to be aligned to 4 bytes. - int Pitch = (Width * BytesPerPixel + 3) & ~3; + int pitch = (width * bytesPerPixel + 3) & ~3; - int DataLayerSize = Height * Pitch * Depth; - byte[] Data = new byte[DataLayerSize * Image.LayerCount]; + int dataLayerSize = height * pitch * depth; + byte[] data = new byte[dataLayerSize * image.LayerCount]; - int TargetMipLevel = Image.MaxMipmapLevel <= 1 ? 1 : Image.MaxMipmapLevel - 1; - int LayerOffset = ImageUtils.GetLayerOffset(Image, TargetMipLevel); + int targetMipLevel = image.MaxMipmapLevel <= 1 ? 1 : image.MaxMipmapLevel - 1; + int layerOffset = GetLayerOffset(image, targetMipLevel); - for (int Layer = 0; Layer < Image.LayerCount; Layer++) + for (int layer = 0; layer < image.LayerCount; layer++) { - for (int Z = 0; Z < Depth; Z++) + for (int z = 0; z < depth; z++) { - for (int Y = 0; Y < Height; Y++) + for (int y = 0; y < height; y++) { - int OutOffs = (DataLayerSize * Layer) + Y * Pitch + (Z * Width * Height * BytesPerPixel); + int outOffs = (dataLayerSize * layer) + y * pitch + (z * width * height * bytesPerPixel); - for (int X = 0; X < Width; X++) + for (int x = 0; x < width; x++) { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y, Z); + long offset = (uint)swizzle.GetSwizzleOffset(x, y, z); - CpuMemory.ReadBytes(Position + (LayerOffset * Layer) + Offset, Data, OutOffs, BytesPerPixel); + cpuMemory.ReadBytes(position + (layerOffset * layer) + offset, data, outOffs, bytesPerPixel); - OutOffs += BytesPerPixel; + outOffs += bytesPerPixel; } } } } - return Data; + return data; } - public static void WriteTexture(NvGpuVmm Vmm, GalImage Image, long Position, byte[] Data) + public static void WriteTexture(NvGpuVmm vmm, GalImage image, long position, byte[] data) { - ISwizzle Swizzle = TextureHelper.GetSwizzle(Image); + ISwizzle swizzle = TextureHelper.GetSwizzle(image); - ImageDescriptor Desc = GetImageDescriptor(Image.Format); + ImageDescriptor desc = GetImageDescriptor(image.Format); - (int Width, int Height, int Depth) = ImageUtils.GetImageSizeInBlocks(Image); + (int width, int height, int depth) = GetImageSizeInBlocks(image); - int BytesPerPixel = Desc.BytesPerPixel; + int bytesPerPixel = desc.BytesPerPixel; - int InOffs = 0; + int inOffs = 0; - for (int Z = 0; Z < Depth; Z++) - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) + for (int z = 0; z < depth; z++) + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y, Z); + long offset = (uint)swizzle.GetSwizzleOffset(x, y, z); - Vmm.Memory.WriteBytes(Position + Offset, Data, InOffs, BytesPerPixel); + vmm.Memory.WriteBytes(position + offset, data, inOffs, bytesPerPixel); - InOffs += BytesPerPixel; + inOffs += bytesPerPixel; } } // TODO: Support non 2D public static bool CopyTexture( - NvGpuVmm Vmm, - GalImage SrcImage, - GalImage DstImage, - long SrcAddress, - long DstAddress, - int SrcX, - int SrcY, - int DstX, - int DstY, - int Width, - int Height) + NvGpuVmm vmm, + GalImage srcImage, + GalImage dstImage, + long srcAddress, + long dstAddress, + int srcX, + int srcY, + int dstX, + int dstY, + int width, + int height) { - ISwizzle SrcSwizzle = TextureHelper.GetSwizzle(SrcImage); - ISwizzle DstSwizzle = TextureHelper.GetSwizzle(DstImage); + ISwizzle srcSwizzle = TextureHelper.GetSwizzle(srcImage); + ISwizzle dstSwizzle = TextureHelper.GetSwizzle(dstImage); - ImageDescriptor Desc = GetImageDescriptor(SrcImage.Format); + ImageDescriptor desc = GetImageDescriptor(srcImage.Format); - if (GetImageDescriptor(DstImage.Format).BytesPerPixel != Desc.BytesPerPixel) + if (GetImageDescriptor(dstImage.Format).BytesPerPixel != desc.BytesPerPixel) { return false; } - int BytesPerPixel = Desc.BytesPerPixel; + int bytesPerPixel = desc.BytesPerPixel; - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) { - long SrcOffset = (uint)SrcSwizzle.GetSwizzleOffset(SrcX + X, SrcY + Y, 0); - long DstOffset = (uint)DstSwizzle.GetSwizzleOffset(DstX + X, DstY + Y, 0); + long srcOffset = (uint)srcSwizzle.GetSwizzleOffset(srcX + x, srcY + y, 0); + long dstOffset = (uint)dstSwizzle.GetSwizzleOffset(dstX + x, dstY + y, 0); - byte[] Texel = Vmm.ReadBytes(SrcAddress + SrcOffset, BytesPerPixel); + byte[] texel = vmm.ReadBytes(srcAddress + srcOffset, bytesPerPixel); - Vmm.WriteBytes(DstAddress + DstOffset, Texel); + vmm.WriteBytes(dstAddress + dstOffset, texel); } return true; } - public static int GetSize(GalImage Image) + public static int GetSize(GalImage image) { - ImageDescriptor Desc = GetImageDescriptor(Image.Format); + ImageDescriptor desc = GetImageDescriptor(image.Format); - int ComponentCount = GetCoordsCountTextureTarget(Image.TextureTarget); + int componentCount = GetCoordsCountTextureTarget(image.TextureTarget); - if (IsArray(Image.TextureTarget)) - ComponentCount--; + if (IsArray(image.TextureTarget)) + componentCount--; - int Width = DivRoundUp(Image.Width, Desc.BlockWidth); - int Height = DivRoundUp(Image.Height, Desc.BlockHeight); - int Depth = DivRoundUp(Image.Depth, Desc.BlockDepth); + int width = DivRoundUp(image.Width, desc.BlockWidth); + int height = DivRoundUp(image.Height, desc.BlockHeight); + int depth = DivRoundUp(image.Depth, desc.BlockDepth); - switch (ComponentCount) + switch (componentCount) { case 1: - return Desc.BytesPerPixel * Width * Image.LayerCount; + return desc.BytesPerPixel * width * image.LayerCount; case 2: - return Desc.BytesPerPixel * Width * Height * Image.LayerCount; + return desc.BytesPerPixel * width * height * image.LayerCount; case 3: - return Desc.BytesPerPixel * Width * Height * Depth * Image.LayerCount; + return desc.BytesPerPixel * width * height * depth * image.LayerCount; default: - throw new InvalidOperationException($"Invalid component count: {ComponentCount}"); + throw new InvalidOperationException($"Invalid component count: {componentCount}"); } } - public static int GetGpuSize(GalImage Image, bool forcePitch = false) + public static int GetGpuSize(GalImage image, bool forcePitch = false) { - return TextureHelper.GetSwizzle(Image).GetImageSize(Image.MaxMipmapLevel) * Image.LayerCount; + return TextureHelper.GetSwizzle(image).GetImageSize(image.MaxMipmapLevel) * image.LayerCount; } - public static int GetLayerOffset(GalImage Image, int MipLevel) + public static int GetLayerOffset(GalImage image, int mipLevel) { - if (MipLevel <= 0) + if (mipLevel <= 0) { - MipLevel = 1; + mipLevel = 1; } - return TextureHelper.GetSwizzle(Image).GetMipOffset(MipLevel); + return TextureHelper.GetSwizzle(image).GetMipOffset(mipLevel); } - public static int GetPitch(GalImageFormat Format, int Width) + public static int GetPitch(GalImageFormat format, int width) { - ImageDescriptor Desc = GetImageDescriptor(Format); + ImageDescriptor desc = GetImageDescriptor(format); - int Pitch = Desc.BytesPerPixel * DivRoundUp(Width, Desc.BlockWidth); + int pitch = desc.BytesPerPixel * DivRoundUp(width, desc.BlockWidth); - Pitch = (Pitch + 0x1f) & ~0x1f; + pitch = (pitch + 0x1f) & ~0x1f; - return Pitch; + return pitch; } - public static int GetBlockWidth(GalImageFormat Format) + public static int GetBlockWidth(GalImageFormat format) { - return GetImageDescriptor(Format).BlockWidth; + return GetImageDescriptor(format).BlockWidth; } - public static int GetBlockHeight(GalImageFormat Format) + public static int GetBlockHeight(GalImageFormat format) { - return GetImageDescriptor(Format).BlockHeight; + return GetImageDescriptor(format).BlockHeight; } - public static int GetBlockDepth(GalImageFormat Format) + public static int GetBlockDepth(GalImageFormat format) { - return GetImageDescriptor(Format).BlockDepth; + return GetImageDescriptor(format).BlockDepth; } - public static int GetAlignedWidth(GalImage Image) + public static int GetAlignedWidth(GalImage image) { - ImageDescriptor Desc = GetImageDescriptor(Image.Format); + ImageDescriptor desc = GetImageDescriptor(image.Format); - int AlignMask; + int alignMask; - if (Image.Layout == GalMemoryLayout.BlockLinear) + if (image.Layout == GalMemoryLayout.BlockLinear) { - AlignMask = Image.TileWidth * (64 / Desc.BytesPerPixel) - 1; + alignMask = image.TileWidth * (64 / desc.BytesPerPixel) - 1; } else { - AlignMask = (32 / Desc.BytesPerPixel) - 1; + alignMask = (32 / desc.BytesPerPixel) - 1; } - return (Image.Width + AlignMask) & ~AlignMask; + return (image.Width + alignMask) & ~alignMask; } - public static (int Width, int Height, int Depth) GetImageSizeInBlocks(GalImage Image) + public static (int Width, int Height, int Depth) GetImageSizeInBlocks(GalImage image) { - ImageDescriptor Desc = GetImageDescriptor(Image.Format); + ImageDescriptor desc = GetImageDescriptor(image.Format); - return (DivRoundUp(Image.Width, Desc.BlockWidth), - DivRoundUp(Image.Height, Desc.BlockHeight), - DivRoundUp(Image.Depth, Desc.BlockDepth)); + return (DivRoundUp(image.Width, desc.BlockWidth), + DivRoundUp(image.Height, desc.BlockHeight), + DivRoundUp(image.Depth, desc.BlockDepth)); } - public static int GetBytesPerPixel(GalImageFormat Format) + public static int GetBytesPerPixel(GalImageFormat format) { - return GetImageDescriptor(Format).BytesPerPixel; + return GetImageDescriptor(format).BytesPerPixel; } - private static int DivRoundUp(int LHS, int RHS) + private static int DivRoundUp(int lhs, int rhs) { - return (LHS + (RHS - 1)) / RHS; + return (lhs + (rhs - 1)) / rhs; } - public static bool HasColor(GalImageFormat Format) + public static bool HasColor(GalImageFormat format) { - return (GetImageDescriptor(Format).Target & TargetBuffer.Color) != 0; + return (GetImageDescriptor(format).Target & TargetBuffer.Color) != 0; } - public static bool HasDepth(GalImageFormat Format) + public static bool HasDepth(GalImageFormat format) { - return (GetImageDescriptor(Format).Target & TargetBuffer.Depth) != 0; + return (GetImageDescriptor(format).Target & TargetBuffer.Depth) != 0; } - public static bool HasStencil(GalImageFormat Format) + public static bool HasStencil(GalImageFormat format) { - return (GetImageDescriptor(Format).Target & TargetBuffer.Stencil) != 0; + return (GetImageDescriptor(format).Target & TargetBuffer.Stencil) != 0; } - public static bool IsCompressed(GalImageFormat Format) + public static bool IsCompressed(GalImageFormat format) { - ImageDescriptor Desc = GetImageDescriptor(Format); + ImageDescriptor desc = GetImageDescriptor(format); - return (Desc.BlockWidth | Desc.BlockHeight) != 1; + return (desc.BlockWidth | desc.BlockHeight) != 1; } - private static ImageDescriptor GetImageDescriptor(GalImageFormat Format) + private static ImageDescriptor GetImageDescriptor(GalImageFormat format) { - GalImageFormat PixelFormat = Format & GalImageFormat.FormatMask; + GalImageFormat pixelFormat = format & GalImageFormat.FormatMask; - if (s_ImageTable.TryGetValue(PixelFormat, out ImageDescriptor Descriptor)) + if (ImageTable.TryGetValue(pixelFormat, out ImageDescriptor descriptor)) { - return Descriptor; + return descriptor; } - throw new NotImplementedException($"Format \"{PixelFormat}\" not implemented!"); + throw new NotImplementedException($"Format \"{pixelFormat}\" not implemented!"); } - private static GalImageFormat GetFormatType(GalTextureType Type) + private static GalImageFormat GetFormatType(GalTextureType type) { - switch (Type) + switch (type) { case GalTextureType.Snorm: return Snorm; case GalTextureType.Unorm: return Unorm; @@ -494,13 +493,13 @@ namespace Ryujinx.Graphics.Texture case GalTextureType.Uint: return Uint; case GalTextureType.Float: return Float; - default: throw new NotImplementedException(((int)Type).ToString()); + default: throw new NotImplementedException(((int)type).ToString()); } } - public static TextureTarget GetTextureTarget(GalTextureTarget GalTextureTarget) + public static TextureTarget GetTextureTarget(GalTextureTarget galTextureTarget) { - switch (GalTextureTarget) + switch (galTextureTarget) { case GalTextureTarget.OneD: return TextureTarget.Texture1D; @@ -520,13 +519,13 @@ namespace Ryujinx.Graphics.Texture case GalTextureTarget.CubeArray: return TextureTarget.TextureCubeMapArray; default: - throw new NotSupportedException($"Texture target {GalTextureTarget} currently not supported!"); + throw new NotSupportedException($"Texture target {galTextureTarget} currently not supported!"); } } - public static bool IsArray(GalTextureTarget TextureTarget) + public static bool IsArray(GalTextureTarget textureTarget) { - switch (TextureTarget) + switch (textureTarget) { case GalTextureTarget.OneDArray: case GalTextureTarget.TwoDArray: @@ -537,9 +536,9 @@ namespace Ryujinx.Graphics.Texture } } - public static int GetCoordsCountTextureTarget(GalTextureTarget TextureTarget) + public static int GetCoordsCountTextureTarget(GalTextureTarget textureTarget) { - switch (TextureTarget) + switch (textureTarget) { case GalTextureTarget.OneD: return 1; @@ -555,7 +554,7 @@ namespace Ryujinx.Graphics.Texture case GalTextureTarget.CubeArray: return 4; default: - throw new NotImplementedException($"TextureTarget.{TextureTarget} not implemented yet."); + throw new NotImplementedException($"TextureTarget.{textureTarget} not implemented yet."); } } } diff --git a/Ryujinx.Graphics/Graphics3d/Texture/IntegerEncoded.cs b/Ryujinx.Graphics/Graphics3d/Texture/IntegerEncoded.cs index 683cb770..e6d67058 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/IntegerEncoded.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/IntegerEncoded.cs @@ -12,81 +12,81 @@ namespace Ryujinx.Graphics.Texture Trit } - EIntegerEncoding Encoding; + EIntegerEncoding _encoding; public int NumberBits { get; private set; } public int BitValue { get; private set; } public int TritValue { get; private set; } public int QuintValue { get; private set; } - public IntegerEncoded(EIntegerEncoding _Encoding, int NumBits) + public IntegerEncoded(EIntegerEncoding encoding, int numBits) { - Encoding = _Encoding; - NumberBits = NumBits; + _encoding = encoding; + NumberBits = numBits; BitValue = 0; TritValue = 0; QuintValue = 0; } - public bool MatchesEncoding(IntegerEncoded Other) + public bool MatchesEncoding(IntegerEncoded other) { - return Encoding == Other.Encoding && NumberBits == Other.NumberBits; + return _encoding == other._encoding && NumberBits == other.NumberBits; } public EIntegerEncoding GetEncoding() { - return Encoding; + return _encoding; } - public int GetBitLength(int NumberVals) + public int GetBitLength(int numberVals) { - int TotalBits = NumberBits * NumberVals; - if (Encoding == EIntegerEncoding.Trit) + int totalBits = NumberBits * numberVals; + if (_encoding == EIntegerEncoding.Trit) { - TotalBits += (NumberVals * 8 + 4) / 5; + totalBits += (numberVals * 8 + 4) / 5; } - else if (Encoding == EIntegerEncoding.Quint) + else if (_encoding == EIntegerEncoding.Quint) { - TotalBits += (NumberVals * 7 + 2) / 3; + totalBits += (numberVals * 7 + 2) / 3; } - return TotalBits; + return totalBits; } - public static IntegerEncoded CreateEncoding(int MaxVal) + public static IntegerEncoded CreateEncoding(int maxVal) { - while (MaxVal > 0) + while (maxVal > 0) { - int Check = MaxVal + 1; + int check = maxVal + 1; // Is maxVal a power of two? - if ((Check & (Check - 1)) == 0) + if ((check & (check - 1)) == 0) { - return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(MaxVal)); + return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(maxVal)); } // Is maxVal of the type 3*2^n - 1? - if ((Check % 3 == 0) && ((Check / 3) & ((Check / 3) - 1)) == 0) + if ((check % 3 == 0) && ((check / 3) & ((check / 3) - 1)) == 0) { - return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(Check / 3 - 1)); + return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(check / 3 - 1)); } // Is maxVal of the type 5*2^n - 1? - if ((Check % 5 == 0) && ((Check / 5) & ((Check / 5) - 1)) == 0) + if ((check % 5 == 0) && ((check / 5) & ((check / 5) - 1)) == 0) { - return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(Check / 5 - 1)); + return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(check / 5 - 1)); } // Apparently it can't be represented with a bounded integer sequence... // just iterate. - MaxVal--; + maxVal--; } return new IntegerEncoded(EIntegerEncoding.JustBits, 0); } public static void DecodeTritBlock( - BitArrayStream BitStream, - List<IntegerEncoded> ListIntegerEncoded, - int NumberBitsPerValue) + BitArrayStream bitStream, + List<IntegerEncoded> listIntegerEncoded, + int numberBitsPerValue) { // Implement the algorithm in section C.2.12 int[] m = new int[5]; @@ -95,170 +95,170 @@ namespace Ryujinx.Graphics.Texture // Read the trit encoded block according to // table C.2.14 - m[0] = BitStream.ReadBits(NumberBitsPerValue); - T = BitStream.ReadBits(2); - m[1] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(2) << 2; - m[2] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(1) << 4; - m[3] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(2) << 5; - m[4] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(1) << 7; + m[0] = bitStream.ReadBits(numberBitsPerValue); + T = bitStream.ReadBits(2); + m[1] = bitStream.ReadBits(numberBitsPerValue); + T |= bitStream.ReadBits(2) << 2; + m[2] = bitStream.ReadBits(numberBitsPerValue); + T |= bitStream.ReadBits(1) << 4; + m[3] = bitStream.ReadBits(numberBitsPerValue); + T |= bitStream.ReadBits(2) << 5; + m[4] = bitStream.ReadBits(numberBitsPerValue); + T |= bitStream.ReadBits(1) << 7; - int C = 0; + int c = 0; - BitArrayStream Tb = new BitArrayStream(new BitArray(new int[] { T })); - if (Tb.ReadBits(2, 4) == 7) + BitArrayStream tb = new BitArrayStream(new BitArray(new int[] { T })); + if (tb.ReadBits(2, 4) == 7) { - C = (Tb.ReadBits(5, 7) << 2) | Tb.ReadBits(0, 1); + c = (tb.ReadBits(5, 7) << 2) | tb.ReadBits(0, 1); t[4] = t[3] = 2; } else { - C = Tb.ReadBits(0, 4); - if (Tb.ReadBits(5, 6) == 3) + c = tb.ReadBits(0, 4); + if (tb.ReadBits(5, 6) == 3) { t[4] = 2; - t[3] = Tb.ReadBit(7); + t[3] = tb.ReadBit(7); } else { - t[4] = Tb.ReadBit(7); - t[3] = Tb.ReadBits(5, 6); + t[4] = tb.ReadBit(7); + t[3] = tb.ReadBits(5, 6); } } - BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); - if (Cb.ReadBits(0, 1) == 3) + BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c })); + if (cb.ReadBits(0, 1) == 3) { t[2] = 2; - t[1] = Cb.ReadBit(4); - t[0] = (Cb.ReadBit(3) << 1) | (Cb.ReadBit(2) & ~Cb.ReadBit(3)); + t[1] = cb.ReadBit(4); + t[0] = (cb.ReadBit(3) << 1) | (cb.ReadBit(2) & ~cb.ReadBit(3)); } - else if (Cb.ReadBits(2, 3) == 3) + else if (cb.ReadBits(2, 3) == 3) { t[2] = 2; t[1] = 2; - t[0] = Cb.ReadBits(0, 1); + t[0] = cb.ReadBits(0, 1); } else { - t[2] = Cb.ReadBit(4); - t[1] = Cb.ReadBits(2, 3); - t[0] = (Cb.ReadBit(1) << 1) | (Cb.ReadBit(0) & ~Cb.ReadBit(1)); + t[2] = cb.ReadBit(4); + t[1] = cb.ReadBits(2, 3); + t[0] = (cb.ReadBit(1) << 1) | (cb.ReadBit(0) & ~cb.ReadBit(1)); } for (int i = 0; i < 5; i++) { - IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Trit, NumberBitsPerValue) + IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Trit, numberBitsPerValue) { BitValue = m[i], TritValue = t[i] }; - ListIntegerEncoded.Add(IntEncoded); + listIntegerEncoded.Add(intEncoded); } } public static void DecodeQuintBlock( - BitArrayStream BitStream, - List<IntegerEncoded> ListIntegerEncoded, - int NumberBitsPerValue) + BitArrayStream bitStream, + List<IntegerEncoded> listIntegerEncoded, + int numberBitsPerValue) { // Implement the algorithm in section C.2.12 int[] m = new int[3]; - int[] q = new int[3]; - int Q; + int[] qa = new int[3]; + int q; // Read the trit encoded block according to // table C.2.15 - m[0] = BitStream.ReadBits(NumberBitsPerValue); - Q = BitStream.ReadBits(3); - m[1] = BitStream.ReadBits(NumberBitsPerValue); - Q |= BitStream.ReadBits(2) << 3; - m[2] = BitStream.ReadBits(NumberBitsPerValue); - Q |= BitStream.ReadBits(2) << 5; + m[0] = bitStream.ReadBits(numberBitsPerValue); + q = bitStream.ReadBits(3); + m[1] = bitStream.ReadBits(numberBitsPerValue); + q |= bitStream.ReadBits(2) << 3; + m[2] = bitStream.ReadBits(numberBitsPerValue); + q |= bitStream.ReadBits(2) << 5; - BitArrayStream Qb = new BitArrayStream(new BitArray(new int[] { Q })); - if (Qb.ReadBits(1, 2) == 3 && Qb.ReadBits(5, 6) == 0) + BitArrayStream qb = new BitArrayStream(new BitArray(new int[] { q })); + if (qb.ReadBits(1, 2) == 3 && qb.ReadBits(5, 6) == 0) { - q[0] = q[1] = 4; - q[2] = (Qb.ReadBit(0) << 2) | ((Qb.ReadBit(4) & ~Qb.ReadBit(0)) << 1) | (Qb.ReadBit(3) & ~Qb.ReadBit(0)); + qa[0] = qa[1] = 4; + qa[2] = (qb.ReadBit(0) << 2) | ((qb.ReadBit(4) & ~qb.ReadBit(0)) << 1) | (qb.ReadBit(3) & ~qb.ReadBit(0)); } else { - int C = 0; - if (Qb.ReadBits(1, 2) == 3) + int c = 0; + if (qb.ReadBits(1, 2) == 3) { - q[2] = 4; - C = (Qb.ReadBits(3, 4) << 3) | ((~Qb.ReadBits(5, 6) & 3) << 1) | Qb.ReadBit(0); + qa[2] = 4; + c = (qb.ReadBits(3, 4) << 3) | ((~qb.ReadBits(5, 6) & 3) << 1) | qb.ReadBit(0); } else { - q[2] = Qb.ReadBits(5, 6); - C = Qb.ReadBits(0, 4); + qa[2] = qb.ReadBits(5, 6); + c = qb.ReadBits(0, 4); } - BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); - if (Cb.ReadBits(0, 2) == 5) + BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c })); + if (cb.ReadBits(0, 2) == 5) { - q[1] = 4; - q[0] = Cb.ReadBits(3, 4); + qa[1] = 4; + qa[0] = cb.ReadBits(3, 4); } else { - q[1] = Cb.ReadBits(3, 4); - q[0] = Cb.ReadBits(0, 2); + qa[1] = cb.ReadBits(3, 4); + qa[0] = cb.ReadBits(0, 2); } } for (int i = 0; i < 3; i++) { - IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Quint, NumberBitsPerValue) + IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Quint, numberBitsPerValue) { BitValue = m[i], - QuintValue = q[i] + QuintValue = qa[i] }; - ListIntegerEncoded.Add(IntEncoded); + listIntegerEncoded.Add(intEncoded); } } public static void DecodeIntegerSequence( - List<IntegerEncoded> DecodeIntegerSequence, - BitArrayStream BitStream, - int MaxRange, - int NumberValues) + List<IntegerEncoded> decodeIntegerSequence, + BitArrayStream bitStream, + int maxRange, + int numberValues) { // Determine encoding parameters - IntegerEncoded IntEncoded = CreateEncoding(MaxRange); + IntegerEncoded intEncoded = CreateEncoding(maxRange); // Start decoding - int NumberValuesDecoded = 0; - while (NumberValuesDecoded < NumberValues) + int numberValuesDecoded = 0; + while (numberValuesDecoded < numberValues) { - switch (IntEncoded.GetEncoding()) + switch (intEncoded.GetEncoding()) { case EIntegerEncoding.Quint: { - DecodeQuintBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); - NumberValuesDecoded += 3; + DecodeQuintBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits); + numberValuesDecoded += 3; break; } case EIntegerEncoding.Trit: { - DecodeTritBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); - NumberValuesDecoded += 5; + DecodeTritBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits); + numberValuesDecoded += 5; break; } case EIntegerEncoding.JustBits: { - IntEncoded.BitValue = BitStream.ReadBits(IntEncoded.NumberBits); - DecodeIntegerSequence.Add(IntEncoded); - NumberValuesDecoded++; + intEncoded.BitValue = bitStream.ReadBits(intEncoded.NumberBits); + decodeIntegerSequence.Add(intEncoded); + numberValuesDecoded++; break; } diff --git a/Ryujinx.Graphics/Graphics3d/Texture/LinearSwizzle.cs b/Ryujinx.Graphics/Graphics3d/Texture/LinearSwizzle.cs index e6509baa..fb1bd098 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/LinearSwizzle.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/LinearSwizzle.cs @@ -4,42 +4,42 @@ namespace Ryujinx.Graphics.Texture { class LinearSwizzle : ISwizzle { - private int Pitch; - private int Bpp; + private int _pitch; + private int _bpp; - private int SliceSize; + private int _sliceSize; - public LinearSwizzle(int Pitch, int Bpp, int Width, int Height) + public LinearSwizzle(int pitch, int bpp, int width, int height) { - this.Pitch = Pitch; - this.Bpp = Bpp; - SliceSize = Width * Height * Bpp; + _pitch = pitch; + _bpp = bpp; + _sliceSize = width * height * bpp; } - public void SetMipLevel(int Level) + public void SetMipLevel(int level) { throw new NotImplementedException(); } - public int GetMipOffset(int Level) + public int GetMipOffset(int level) { - if (Level == 1) - return SliceSize; + if (level == 1) + return _sliceSize; throw new NotImplementedException(); } - public int GetImageSize(int MipsCount) + public int GetImageSize(int mipsCount) { - int Size = GetMipOffset(MipsCount); + int size = GetMipOffset(mipsCount); - Size = (Size + 0x1fff) & ~0x1fff; + size = (size + 0x1fff) & ~0x1fff; - return Size; + return size; } - public int GetSwizzleOffset(int X, int Y, int Z) + public int GetSwizzleOffset(int x, int y, int z) { - return Z * SliceSize + X * Bpp + Y * Pitch; + return z * _sliceSize + x * _bpp + y * _pitch; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/Texture/TextureFactory.cs b/Ryujinx.Graphics/Graphics3d/Texture/TextureFactory.cs index a2ce86f5..28c90502 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/TextureFactory.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/TextureFactory.cs @@ -6,161 +6,161 @@ namespace Ryujinx.Graphics.Texture { static class TextureFactory { - public static GalImage MakeTexture(NvGpuVmm Vmm, long TicPosition) + public static GalImage MakeTexture(NvGpuVmm vmm, long ticPosition) { - int[] Tic = ReadWords(Vmm, TicPosition, 8); + int[] tic = ReadWords(vmm, ticPosition, 8); - GalImageFormat Format = GetImageFormat(Tic); + GalImageFormat format = GetImageFormat(tic); - GalTextureTarget TextureTarget = (GalTextureTarget)((Tic[4] >> 23) & 0xF); + GalTextureTarget textureTarget = (GalTextureTarget)((tic[4] >> 23) & 0xF); - GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); - GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); - GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); - GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); + GalTextureSource xSource = (GalTextureSource)((tic[0] >> 19) & 7); + GalTextureSource ySource = (GalTextureSource)((tic[0] >> 22) & 7); + GalTextureSource zSource = (GalTextureSource)((tic[0] >> 25) & 7); + GalTextureSource wSource = (GalTextureSource)((tic[0] >> 28) & 7); - TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); + TextureSwizzle swizzle = (TextureSwizzle)((tic[2] >> 21) & 7); - int MaxMipmapLevel = (Tic[3] >> 28) & 0xF + 1; + int maxMipmapLevel = (tic[3] >> 28) & 0xF + 1; - GalMemoryLayout Layout; + GalMemoryLayout layout; - if (Swizzle == TextureSwizzle.BlockLinear || - Swizzle == TextureSwizzle.BlockLinearColorKey) + if (swizzle == TextureSwizzle.BlockLinear || + swizzle == TextureSwizzle.BlockLinearColorKey) { - Layout = GalMemoryLayout.BlockLinear; + layout = GalMemoryLayout.BlockLinear; } else { - Layout = GalMemoryLayout.Pitch; + layout = GalMemoryLayout.Pitch; } - int GobBlockHeightLog2 = (Tic[3] >> 3) & 7; - int GobBlockDepthLog2 = (Tic[3] >> 6) & 7; - int TileWidthLog2 = (Tic[3] >> 10) & 7; + int gobBlockHeightLog2 = (tic[3] >> 3) & 7; + int gobBlockDepthLog2 = (tic[3] >> 6) & 7; + int tileWidthLog2 = (tic[3] >> 10) & 7; - int GobBlockHeight = 1 << GobBlockHeightLog2; - int GobBlockDepth = 1 << GobBlockDepthLog2; - int TileWidth = 1 << TileWidthLog2; + int gobBlockHeight = 1 << gobBlockHeightLog2; + int gobBlockDepth = 1 << gobBlockDepthLog2; + int tileWidth = 1 << tileWidthLog2; - int Width = ((Tic[4] >> 0) & 0xffff) + 1; - int Height = ((Tic[5] >> 0) & 0xffff) + 1; - int Depth = ((Tic[5] >> 16) & 0x3fff) + 1; + int width = ((tic[4] >> 0) & 0xffff) + 1; + int height = ((tic[5] >> 0) & 0xffff) + 1; + int depth = ((tic[5] >> 16) & 0x3fff) + 1; - int LayoutCount = 1; + int layoutCount = 1; // TODO: check this - if (ImageUtils.IsArray(TextureTarget)) + if (ImageUtils.IsArray(textureTarget)) { - LayoutCount = Depth; - Depth = 1; + layoutCount = depth; + depth = 1; } - if (TextureTarget == GalTextureTarget.OneD) + if (textureTarget == GalTextureTarget.OneD) { - Height = 1; + height = 1; } - if (TextureTarget == GalTextureTarget.TwoD || TextureTarget == GalTextureTarget.OneD) + if (textureTarget == GalTextureTarget.TwoD || textureTarget == GalTextureTarget.OneD) { - Depth = 1; + depth = 1; } - else if (TextureTarget == GalTextureTarget.CubeMap) + else if (textureTarget == GalTextureTarget.CubeMap) { // FIXME: This is a bit hacky but I guess it's fine for now - LayoutCount = 6; - Depth = 1; + layoutCount = 6; + depth = 1; } - else if (TextureTarget == GalTextureTarget.CubeArray) + else if (textureTarget == GalTextureTarget.CubeArray) { // FIXME: This is a really really hacky but I guess it's fine for now - LayoutCount *= 6; - Depth = 1; + layoutCount *= 6; + depth = 1; } - GalImage Image = new GalImage( - Width, - Height, - Depth, - LayoutCount, - TileWidth, - GobBlockHeight, - GobBlockDepth, - Layout, - Format, - TextureTarget, - MaxMipmapLevel, - XSource, - YSource, - ZSource, - WSource); - - if (Layout == GalMemoryLayout.Pitch) + GalImage image = new GalImage( + width, + height, + depth, + layoutCount, + tileWidth, + gobBlockHeight, + gobBlockDepth, + layout, + format, + textureTarget, + maxMipmapLevel, + xSource, + ySource, + zSource, + wSource); + + if (layout == GalMemoryLayout.Pitch) { - Image.Pitch = (Tic[3] & 0xffff) << 5; + image.Pitch = (tic[3] & 0xffff) << 5; } - return Image; + return image; } - public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) + public static GalTextureSampler MakeSampler(NvGpu gpu, NvGpuVmm vmm, long tscPosition) { - int[] Tsc = ReadWords(Vmm, TscPosition, 8); + int[] tsc = ReadWords(vmm, tscPosition, 8); - GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); - GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); - GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); + GalTextureWrap addressU = (GalTextureWrap)((tsc[0] >> 0) & 7); + GalTextureWrap addressV = (GalTextureWrap)((tsc[0] >> 3) & 7); + GalTextureWrap addressP = (GalTextureWrap)((tsc[0] >> 6) & 7); - bool DepthCompare = ((Tsc[0] >> 9) & 1) == 1; + bool depthCompare = ((tsc[0] >> 9) & 1) == 1; - DepthCompareFunc DepthCompareFunc = (DepthCompareFunc)((Tsc[0] >> 10) & 7); + DepthCompareFunc depthCompareFunc = (DepthCompareFunc)((tsc[0] >> 10) & 7); - GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); - GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); - GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); + GalTextureFilter magFilter = (GalTextureFilter) ((tsc[1] >> 0) & 3); + GalTextureFilter minFilter = (GalTextureFilter) ((tsc[1] >> 4) & 3); + GalTextureMipFilter mipFilter = (GalTextureMipFilter)((tsc[1] >> 6) & 3); - GalColorF BorderColor = new GalColorF( - BitConverter.Int32BitsToSingle(Tsc[4]), - BitConverter.Int32BitsToSingle(Tsc[5]), - BitConverter.Int32BitsToSingle(Tsc[6]), - BitConverter.Int32BitsToSingle(Tsc[7])); + GalColorF borderColor = new GalColorF( + BitConverter.Int32BitsToSingle(tsc[4]), + BitConverter.Int32BitsToSingle(tsc[5]), + BitConverter.Int32BitsToSingle(tsc[6]), + BitConverter.Int32BitsToSingle(tsc[7])); return new GalTextureSampler( - AddressU, - AddressV, - AddressP, - MinFilter, - MagFilter, - MipFilter, - BorderColor, - DepthCompare, - DepthCompareFunc); + addressU, + addressV, + addressP, + minFilter, + magFilter, + mipFilter, + borderColor, + depthCompare, + depthCompareFunc); } - private static GalImageFormat GetImageFormat(int[] Tic) + private static GalImageFormat GetImageFormat(int[] tic) { - GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7); - GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7); - GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7); - GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7); + GalTextureType rType = (GalTextureType)((tic[0] >> 7) & 7); + GalTextureType gType = (GalTextureType)((tic[0] >> 10) & 7); + GalTextureType bType = (GalTextureType)((tic[0] >> 13) & 7); + GalTextureType aType = (GalTextureType)((tic[0] >> 16) & 7); - GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); + GalTextureFormat format = (GalTextureFormat)(tic[0] & 0x7f); - bool ConvSrgb = ((Tic[4] >> 22) & 1) != 0; + bool convSrgb = ((tic[4] >> 22) & 1) != 0; - return ImageUtils.ConvertTexture(Format, RType, GType, BType, AType, ConvSrgb); + return ImageUtils.ConvertTexture(format, rType, gType, bType, aType, convSrgb); } - private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) + private static int[] ReadWords(NvGpuVmm vmm, long position, int count) { - int[] Words = new int[Count]; + int[] words = new int[count]; - for (int Index = 0; Index < Count; Index++, Position += 4) + for (int index = 0; index < count; index++, position += 4) { - Words[Index] = Vmm.ReadInt32(Position); + words[index] = vmm.ReadInt32(position); } - return Words; + return words; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics/Graphics3d/Texture/TextureHelper.cs b/Ryujinx.Graphics/Graphics3d/Texture/TextureHelper.cs index 33ccb0aa..1de81008 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/TextureHelper.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/TextureHelper.cs @@ -7,47 +7,47 @@ namespace Ryujinx.Graphics.Texture { static class TextureHelper { - public static ISwizzle GetSwizzle(GalImage Image) + public static ISwizzle GetSwizzle(GalImage image) { - int BlockWidth = ImageUtils.GetBlockWidth (Image.Format); - int BlockHeight = ImageUtils.GetBlockHeight (Image.Format); - int BlockDepth = ImageUtils.GetBlockDepth (Image.Format); - int BytesPerPixel = ImageUtils.GetBytesPerPixel(Image.Format); + int blockWidth = ImageUtils.GetBlockWidth (image.Format); + int blockHeight = ImageUtils.GetBlockHeight (image.Format); + int blockDepth = ImageUtils.GetBlockDepth (image.Format); + int bytesPerPixel = ImageUtils.GetBytesPerPixel(image.Format); - int Width = BitUtils.DivRoundUp(Image.Width, BlockWidth); - int Height = BitUtils.DivRoundUp(Image.Height, BlockHeight); - int Depth = BitUtils.DivRoundUp(Image.Depth, BlockDepth); + int width = BitUtils.DivRoundUp(image.Width, blockWidth); + int height = BitUtils.DivRoundUp(image.Height, blockHeight); + int depth = BitUtils.DivRoundUp(image.Depth, blockDepth); - if (Image.Layout == GalMemoryLayout.BlockLinear) + if (image.Layout == GalMemoryLayout.BlockLinear) { - int AlignMask = Image.TileWidth * (64 / BytesPerPixel) - 1; + int alignMask = image.TileWidth * (64 / bytesPerPixel) - 1; - Width = (Width + AlignMask) & ~AlignMask; + width = (width + alignMask) & ~alignMask; return new BlockLinearSwizzle( - Width, - Height, - Depth, - Image.GobBlockHeight, - Image.GobBlockDepth, - BytesPerPixel); + width, + height, + depth, + image.GobBlockHeight, + image.GobBlockDepth, + bytesPerPixel); } else { - return new LinearSwizzle(Image.Pitch, BytesPerPixel, Width, Height); + return new LinearSwizzle(image.Pitch, bytesPerPixel, width, height); } } public static (MemoryManager Memory, long Position) GetMemoryAndPosition( - IMemory Memory, - long Position) + IMemory memory, + long position) { - if (Memory is NvGpuVmm Vmm) + if (memory is NvGpuVmm vmm) { - return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); + return (vmm.Memory, vmm.GetPhysicalAddress(position)); } - return ((MemoryManager)Memory, Position); + return ((MemoryManager)memory, position); } } } diff --git a/Ryujinx.Graphics/Graphics3d/Texture/TextureSwizzle.cs b/Ryujinx.Graphics/Graphics3d/Texture/TextureSwizzle.cs index c67a5367..2cc426ab 100644 --- a/Ryujinx.Graphics/Graphics3d/Texture/TextureSwizzle.cs +++ b/Ryujinx.Graphics/Graphics3d/Texture/TextureSwizzle.cs @@ -2,7 +2,7 @@ namespace Ryujinx.Graphics.Texture { public enum TextureSwizzle { - _1dBuffer = 0, + _1DBuffer = 0, PitchColorKey = 1, Pitch = 2, BlockLinear = 3, |