| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using Ryujinx.Graphics.Gal;
- using Ryujinx.Graphics.Memory;
- using Ryujinx.Graphics.Texture;
- using System;
- using System.Collections.Generic;
- namespace Ryujinx.Graphics
- {
- public class NvGpuEngine2d : INvGpuEngine
- {
- private enum CopyOperation
- {
- SrcCopyAnd,
- RopAnd,
- Blend,
- SrcCopy,
- Rop,
- SrcCopyPremult,
- BlendPremult
- }
- public int[] Registers { get; private set; }
- private NvGpu Gpu;
- private Dictionary<int, NvGpuMethod> Methods;
- public NvGpuEngine2d(NvGpu Gpu)
- {
- this.Gpu = Gpu;
- Registers = new int[0xe00];
- Methods = new Dictionary<int, NvGpuMethod>();
- void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
- {
- while (Count-- > 0)
- {
- Methods.Add(Meth, Method);
- Meth += Stride;
- }
- }
- AddMethod(0xb5, 1, 1, TextureCopy);
- }
- public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
- {
- if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
- {
- Method(Vmm, PBEntry);
- }
- else
- {
- WriteRegister(PBEntry);
- }
- }
- private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
- {
- CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation);
- bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0;
- int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth);
- int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight);
- int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch);
- int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions);
- bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0;
- int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth);
- int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight);
- int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch);
- int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions);
- TextureSwizzle SrcSwizzle = SrcLinear
- ? TextureSwizzle.Pitch
- : TextureSwizzle.BlockLinear;
- TextureSwizzle DstSwizzle = DstLinear
- ? TextureSwizzle.Pitch
- : TextureSwizzle.BlockLinear;
- int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf);
- int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
- long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress);
- long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress);
- long SrcKey = Vmm.GetPhysicalAddress(SrcAddress);
- long DstKey = Vmm.GetPhysicalAddress(DstAddress);
- bool IsSrcFb = Gpu.Engine3d.IsFrameBufferPosition(SrcKey);
- bool IsDstFb = Gpu.Engine3d.IsFrameBufferPosition(DstKey);
- TextureInfo SrcTexture()
- {
- return new TextureInfo(
- SrcAddress,
- SrcWidth,
- SrcHeight,
- SrcPitch,
- SrcBlockHeight, 1,
- SrcSwizzle,
- GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm);
- }
- TextureInfo DstTexture()
- {
- return new TextureInfo(
- DstAddress,
- DstWidth,
- DstHeight,
- DstPitch,
- DstBlockHeight, 1,
- DstSwizzle,
- GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm);
- }
- //TODO: fb -> fb copies, tex -> fb copies, formats other than RGBA8,
- //make it throw for unimpl stuff (like the copy mode)...
- if (IsSrcFb && IsDstFb)
- {
- //Frame Buffer -> Frame Buffer copy.
- Gpu.Renderer.RenderTarget.Copy(
- SrcKey,
- DstKey,
- 0,
- 0,
- SrcWidth,
- SrcHeight,
- 0,
- 0,
- DstWidth,
- DstHeight);
- }
- if (IsSrcFb)
- {
- //Frame Buffer -> Texture copy.
- Gpu.Renderer.RenderTarget.GetBufferData(SrcKey, (byte[] Buffer) =>
- {
- TextureInfo Src = SrcTexture();
- TextureInfo Dst = DstTexture();
- if (Src.Width != Dst.Width ||
- Src.Height != Dst.Height)
- {
- throw new NotImplementedException("Texture resizing is not supported");
- }
- TextureWriter.Write(Vmm, Dst, Buffer);
- });
- }
- else if (IsDstFb)
- {
- byte[] Buffer = TextureReader.Read(Vmm, SrcTexture());
- Gpu.Renderer.RenderTarget.SetBufferData(
- DstKey,
- DstWidth,
- DstHeight,
- Buffer);
- }
- else
- {
- //Texture -> Texture copy.
- TextureInfo Src = SrcTexture();
- TextureInfo Dst = DstTexture();
- if (Src.Width != Dst.Width ||
- Src.Height != Dst.Height)
- {
- throw new NotImplementedException("Texture resizing is not supported");
- }
- TextureWriter.Write(Vmm, Dst, TextureReader.Read(Vmm, Src));
- }
- }
- private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg)
- {
- return
- (long)Registers[(int)Reg + 0] << 32 |
- (uint)Registers[(int)Reg + 1];
- }
- private void WriteRegister(NvGpuPBEntry PBEntry)
- {
- int ArgsCount = PBEntry.Arguments.Count;
- if (ArgsCount > 0)
- {
- Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
- }
- }
- private int ReadRegister(NvGpuEngine2dReg Reg)
- {
- return Registers[(int)Reg];
- }
- private void WriteRegister(NvGpuEngine2dReg Reg, int Value)
- {
- Registers[(int)Reg] = Value;
- }
- }
- }
|