Update Fork

This commit is contained in:
John Clemis 2018-07-27 07:16:12 -05:00 committed by GitHub
commit 3b60723fad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 6070 additions and 2514 deletions

3
.gitignore vendored
View file

@ -158,3 +158,6 @@ $RECYCLE.BIN/
# Mac desktop service store files # Mac desktop service store files
.DS_Store .DS_Store
# VS Launch Settings
launchSettings.json

View file

@ -4,6 +4,7 @@ using ChocolArm64.Instruction;
using ChocolArm64.Instruction32; using ChocolArm64.Instruction32;
using ChocolArm64.State; using ChocolArm64.State;
using System; using System;
using System.Collections.Generic;
namespace ChocolArm64 namespace ChocolArm64
{ {
@ -44,7 +45,7 @@ namespace ChocolArm64
SetA64("11101010xx0xxxxxxxxxxxxxxxxxxxxx", AInstEmit.Ands, typeof(AOpCodeAluRs)); SetA64("11101010xx0xxxxxxxxxxxxxxxxxxxxx", AInstEmit.Ands, typeof(AOpCodeAluRs));
SetA64("x0011010110xxxxx001010xxxxxxxxxx", AInstEmit.Asrv, typeof(AOpCodeAluRs)); SetA64("x0011010110xxxxx001010xxxxxxxxxx", AInstEmit.Asrv, typeof(AOpCodeAluRs));
SetA64("000101xxxxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.B, typeof(AOpCodeBImmAl)); SetA64("000101xxxxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.B, typeof(AOpCodeBImmAl));
SetA64("01010100xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.B_Cond, typeof(AOpCodeBImmCond)); SetA64("01010100xxxxxxxxxxxxxxxxxxx0xxxx", AInstEmit.B_Cond, typeof(AOpCodeBImmCond));
SetA64("00110011000xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bfm, typeof(AOpCodeBfm)); SetA64("00110011000xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bfm, typeof(AOpCodeBfm));
SetA64("1011001101xxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bfm, typeof(AOpCodeBfm)); SetA64("1011001101xxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bfm, typeof(AOpCodeBfm));
SetA64("00001010xx1xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bic, typeof(AOpCodeAluRs)); SetA64("00001010xx1xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bic, typeof(AOpCodeAluRs));
@ -52,8 +53,8 @@ namespace ChocolArm64
SetA64("01101010xx1xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bics, typeof(AOpCodeAluRs)); SetA64("01101010xx1xxxxx0xxxxxxxxxxxxxxx", AInstEmit.Bics, typeof(AOpCodeAluRs));
SetA64("11101010xx1xxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bics, typeof(AOpCodeAluRs)); SetA64("11101010xx1xxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bics, typeof(AOpCodeAluRs));
SetA64("100101xxxxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bl, typeof(AOpCodeBImmAl)); SetA64("100101xxxxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Bl, typeof(AOpCodeBImmAl));
SetA64("11010110001xxxxx000000xxxxxxxxxx", AInstEmit.Blr, typeof(AOpCodeBReg)); SetA64("1101011000111111000000xxxxx00000", AInstEmit.Blr, typeof(AOpCodeBReg));
SetA64("11010110000xxxxx000000xxxxxxxxxx", AInstEmit.Br, typeof(AOpCodeBReg)); SetA64("1101011000011111000000xxxxx00000", AInstEmit.Br, typeof(AOpCodeBReg));
SetA64("11010100001xxxxxxxxxxxxxxxx00000", AInstEmit.Brk, typeof(AOpCodeException)); SetA64("11010100001xxxxxxxxxxxxxxxx00000", AInstEmit.Brk, typeof(AOpCodeException));
SetA64("x0110101xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Cbnz, typeof(AOpCodeBImmCmp)); SetA64("x0110101xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Cbnz, typeof(AOpCodeBImmCmp));
SetA64("x0110100xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Cbz, typeof(AOpCodeBImmCmp)); SetA64("x0110100xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Cbz, typeof(AOpCodeBImmCmp));
@ -126,7 +127,7 @@ namespace ChocolArm64
SetA64("11111000100xxxxxxxxx00xxxxxxxxxx", AInstEmit.Pfrm, typeof(AOpCodeMemImm)); SetA64("11111000100xxxxxxxxx00xxxxxxxxxx", AInstEmit.Pfrm, typeof(AOpCodeMemImm));
SetA64("11011000xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Pfrm, typeof(AOpCodeMemLit)); SetA64("11011000xxxxxxxxxxxxxxxxxxxxxxxx", AInstEmit.Pfrm, typeof(AOpCodeMemLit));
SetA64("x101101011000000000000xxxxxxxxxx", AInstEmit.Rbit, typeof(AOpCodeAlu)); SetA64("x101101011000000000000xxxxxxxxxx", AInstEmit.Rbit, typeof(AOpCodeAlu));
SetA64("11010110010xxxxx000000xxxxxxxxxx", AInstEmit.Ret, typeof(AOpCodeBReg)); SetA64("1101011001011111000000xxxxx00000", AInstEmit.Ret, typeof(AOpCodeBReg));
SetA64("x101101011000000000001xxxxxxxxxx", AInstEmit.Rev16, typeof(AOpCodeAlu)); SetA64("x101101011000000000001xxxxxxxxxx", AInstEmit.Rev16, typeof(AOpCodeAlu));
SetA64("x101101011000000000010xxxxxxxxxx", AInstEmit.Rev32, typeof(AOpCodeAlu)); SetA64("x101101011000000000010xxxxxxxxxx", AInstEmit.Rev32, typeof(AOpCodeAlu));
SetA64("1101101011000000000011xxxxxxxxxx", AInstEmit.Rev64, typeof(AOpCodeAlu)); SetA64("1101101011000000000011xxxxxxxxxx", AInstEmit.Rev64, typeof(AOpCodeAlu));
@ -387,6 +388,7 @@ namespace ChocolArm64
SetA64("0100111101xxxxxx000001xxxxxxxxxx", AInstEmit.Sshr_V, typeof(AOpCodeSimdShImm)); SetA64("0100111101xxxxxx000001xxxxxxxxxx", AInstEmit.Sshr_V, typeof(AOpCodeSimdShImm));
SetA64("0x00111100>>>xxx000101xxxxxxxxxx", AInstEmit.Ssra_V, typeof(AOpCodeSimdShImm)); SetA64("0x00111100>>>xxx000101xxxxxxxxxx", AInstEmit.Ssra_V, typeof(AOpCodeSimdShImm));
SetA64("0100111101xxxxxx000101xxxxxxxxxx", AInstEmit.Ssra_V, typeof(AOpCodeSimdShImm)); SetA64("0100111101xxxxxx000101xxxxxxxxxx", AInstEmit.Ssra_V, typeof(AOpCodeSimdShImm));
SetA64("0x001110<<1xxxxx001100xxxxxxxxxx", AInstEmit.Ssubw_V, typeof(AOpCodeSimdReg));
SetA64("0x00110000000000xxxxxxxxxxxxxxxx", AInstEmit.St__Vms, typeof(AOpCodeSimdMemMs)); SetA64("0x00110000000000xxxxxxxxxxxxxxxx", AInstEmit.St__Vms, typeof(AOpCodeSimdMemMs));
SetA64("0x001100100xxxxxxxxxxxxxxxxxxxxx", AInstEmit.St__Vms, typeof(AOpCodeSimdMemMs)); SetA64("0x001100100xxxxxxxxxxxxxxxxxxxxx", AInstEmit.St__Vms, typeof(AOpCodeSimdMemMs));
SetA64("0x00110100x00000xxxxxxxxxxxxxxxx", AInstEmit.St__Vss, typeof(AOpCodeSimdMemSs)); SetA64("0x00110100x00000xxxxxxxxxxxxxxxx", AInstEmit.St__Vss, typeof(AOpCodeSimdMemSs));
@ -430,24 +432,50 @@ namespace ChocolArm64
SetA64("0110111101xxxxxx000001xxxxxxxxxx", AInstEmit.Ushr_V, typeof(AOpCodeSimdShImm)); SetA64("0110111101xxxxxx000001xxxxxxxxxx", AInstEmit.Ushr_V, typeof(AOpCodeSimdShImm));
SetA64("0x10111100>>>xxx000101xxxxxxxxxx", AInstEmit.Usra_V, typeof(AOpCodeSimdShImm)); SetA64("0x10111100>>>xxx000101xxxxxxxxxx", AInstEmit.Usra_V, typeof(AOpCodeSimdShImm));
SetA64("0110111101xxxxxx000101xxxxxxxxxx", AInstEmit.Usra_V, typeof(AOpCodeSimdShImm)); SetA64("0110111101xxxxxx000101xxxxxxxxxx", AInstEmit.Usra_V, typeof(AOpCodeSimdShImm));
SetA64("0x101110<<1xxxxx001100xxxxxxxxxx", AInstEmit.Usubw_V, typeof(AOpCodeSimdReg));
SetA64("0>001110<<0xxxxx000110xxxxxxxxxx", AInstEmit.Uzp1_V, typeof(AOpCodeSimdReg)); SetA64("0>001110<<0xxxxx000110xxxxxxxxxx", AInstEmit.Uzp1_V, typeof(AOpCodeSimdReg));
SetA64("0>001110<<0xxxxx010110xxxxxxxxxx", AInstEmit.Uzp2_V, typeof(AOpCodeSimdReg)); SetA64("0>001110<<0xxxxx010110xxxxxxxxxx", AInstEmit.Uzp2_V, typeof(AOpCodeSimdReg));
SetA64("0x001110<<100001001010xxxxxxxxxx", AInstEmit.Xtn_V, typeof(AOpCodeSimd)); SetA64("0x001110<<100001001010xxxxxxxxxx", AInstEmit.Xtn_V, typeof(AOpCodeSimd));
SetA64("0>001110<<0xxxxx001110xxxxxxxxxx", AInstEmit.Zip1_V, typeof(AOpCodeSimdReg)); SetA64("0>001110<<0xxxxx001110xxxxxxxxxx", AInstEmit.Zip1_V, typeof(AOpCodeSimdReg));
SetA64("0>001110<<0xxxxx011110xxxxxxxxxx", AInstEmit.Zip2_V, typeof(AOpCodeSimdReg)); SetA64("0>001110<<0xxxxx011110xxxxxxxxxx", AInstEmit.Zip2_V, typeof(AOpCodeSimdReg));
#endregion #endregion
#region "Generate InstA64FastLookup Table (AArch64)"
var Tmp = new List<InstInfo>[FastLookupSize];
for (int i = 0; i < FastLookupSize; i++)
{
Tmp[i] = new List<InstInfo>();
}
foreach (var Inst in AllInstA64)
{
int Mask = ToFastLookupIndex(Inst.Mask);
int Value = ToFastLookupIndex(Inst.Value);
for (int i = 0; i < FastLookupSize; i++)
{
if ((i & Mask) == Value)
{
Tmp[i].Add(Inst);
}
}
}
for (int i = 0; i < FastLookupSize; i++)
{
InstA64FastLookup[i] = Tmp[i].ToArray();
}
#endregion
} }
private class TreeNode private class InstInfo
{ {
public int Mask; public int Mask;
public int Value; public int Value;
public TreeNode Next;
public AInst Inst; public AInst Inst;
public TreeNode(int Mask, int Value, AInst Inst) public InstInfo(int Mask, int Value, AInst Inst)
{ {
this.Mask = Mask; this.Mask = Mask;
this.Value = Value; this.Value = Value;
@ -455,8 +483,11 @@ namespace ChocolArm64
} }
} }
private static TreeNode InstHeadA32; private static List<InstInfo> AllInstA32 = new List<InstInfo>();
private static TreeNode InstHeadA64; private static List<InstInfo> AllInstA64 = new List<InstInfo>();
private static int FastLookupSize = 0x1000;
private static InstInfo[][] InstA64FastLookup = new InstInfo[FastLookupSize][];
private static void SetA32(string Encoding, AInstInterpreter Interpreter, Type Type) private static void SetA32(string Encoding, AInstInterpreter Interpreter, Type Type)
{ {
@ -517,7 +548,7 @@ namespace ChocolArm64
if (XBits == 0) if (XBits == 0)
{ {
InsertTop(XMask, Value, Inst, Mode); InsertInst(XMask, Value, Inst, Mode);
return; return;
} }
@ -533,55 +564,53 @@ namespace ChocolArm64
if (Mask != Blacklisted) if (Mask != Blacklisted)
{ {
InsertTop(XMask, Value | Mask, Inst, Mode); InsertInst(XMask, Value | Mask, Inst, Mode);
} }
} }
} }
private static void InsertTop( private static void InsertInst(
int XMask, int XMask,
int Value, int Value,
AInst Inst, AInst Inst,
AExecutionMode Mode) AExecutionMode Mode)
{ {
TreeNode Node = new TreeNode(XMask, Value, Inst); InstInfo Info = new InstInfo(XMask, Value, Inst);
if (Mode == AExecutionMode.AArch64) if (Mode == AExecutionMode.AArch64)
{ {
Node.Next = InstHeadA64; AllInstA64.Add(Info);
InstHeadA64 = Node;
} }
else else
{ {
Node.Next = InstHeadA32; AllInstA32.Add(Info);
InstHeadA32 = Node;
} }
} }
public static AInst GetInstA32(int OpCode) public static AInst GetInstA32(int OpCode)
{ {
return GetInst(InstHeadA32, OpCode); return GetInstFromList(AllInstA32, OpCode);
} }
public static AInst GetInstA64(int OpCode) public static AInst GetInstA64(int OpCode)
{ {
return GetInst(InstHeadA64, OpCode); return GetInstFromList(InstA64FastLookup[ToFastLookupIndex(OpCode)], OpCode);
} }
private static AInst GetInst(TreeNode Head, int OpCode) private static int ToFastLookupIndex(int Value)
{ {
TreeNode Node = Head; return ((Value >> 10) & 0x00F) | ((Value >> 18) & 0xFF0);
}
do private static AInst GetInstFromList(IEnumerable<InstInfo> InstList, int OpCode)
{
foreach (var Node in InstList)
{ {
if ((OpCode & Node.Mask) == Node.Value) if ((OpCode & Node.Mask) == Node.Value)
{ {
return Node.Inst; return Node.Inst;
} }
} }
while ((Node = Node.Next) != null);
return AInst.Undefined; return AInst.Undefined;
} }

View file

@ -48,18 +48,24 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeMemEx Op = (AOpCodeMemEx)Context.CurrOp; AOpCodeMemEx Op = (AOpCodeMemEx)Context.CurrOp;
if (AccType.HasFlag(AccessType.Ordered)) bool Ordered = (AccType & AccessType.Ordered) != 0;
bool Exclusive = (AccType & AccessType.Exclusive) != 0;
if (Ordered)
{ {
EmitBarrier(Context); EmitBarrier(Context);
} }
if (AccType.HasFlag(AccessType.Exclusive)) if (Exclusive)
{ {
EmitMemoryCall(Context, nameof(AMemory.SetExclusive), Op.Rn); EmitMemoryCall(Context, nameof(AMemory.SetExclusive), Op.Rn);
} }
Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
Context.EmitLdint(Op.Rn); Context.EmitLdint(Op.Rn);
Context.EmitSttmp();
Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
Context.EmitLdtmp();
EmitReadZxCall(Context, Op.Size); EmitReadZxCall(Context, Op.Size);
@ -68,7 +74,7 @@ namespace ChocolArm64.Instruction
if (Pair) if (Pair)
{ {
Context.EmitLdarg(ATranslatedSub.MemoryArgIdx); Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
Context.EmitLdint(Op.Rn); Context.EmitLdtmp();
Context.EmitLdc_I(8 << Op.Size); Context.EmitLdc_I(8 << Op.Size);
Context.Emit(OpCodes.Add); Context.Emit(OpCodes.Add);
@ -104,7 +110,10 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeMemEx Op = (AOpCodeMemEx)Context.CurrOp; AOpCodeMemEx Op = (AOpCodeMemEx)Context.CurrOp;
if (AccType.HasFlag(AccessType.Ordered)) bool Ordered = (AccType & AccessType.Ordered) != 0;
bool Exclusive = (AccType & AccessType.Exclusive) != 0;
if (Ordered)
{ {
EmitBarrier(Context); EmitBarrier(Context);
} }
@ -112,7 +121,7 @@ namespace ChocolArm64.Instruction
AILLabel LblEx = new AILLabel(); AILLabel LblEx = new AILLabel();
AILLabel LblEnd = new AILLabel(); AILLabel LblEnd = new AILLabel();
if (AccType.HasFlag(AccessType.Exclusive)) if (Exclusive)
{ {
EmitMemoryCall(Context, nameof(AMemory.TestExclusive), Op.Rn); EmitMemoryCall(Context, nameof(AMemory.TestExclusive), Op.Rn);
@ -145,7 +154,7 @@ namespace ChocolArm64.Instruction
EmitWriteCall(Context, Op.Size); EmitWriteCall(Context, Op.Size);
} }
if (AccType.HasFlag(AccessType.Exclusive)) if (Exclusive)
{ {
Context.EmitLdc_I8(0); Context.EmitLdc_I8(0);
Context.EmitStintzr(Op.Rs); Context.EmitStintzr(Op.Rs);

View file

@ -163,12 +163,19 @@ namespace ChocolArm64.Instruction
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Elems = 8 >> Op.Size; int Elems = 8 >> Op.Size;
int ESize = 8 << Op.Size; int ESize = 8 << Op.Size;
int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0; int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
long RoundConst = 1L << (ESize - 1); long RoundConst = 1L << (ESize - 1);
if (Part != 0)
{
Context.EmitLdvec(Op.Rd);
Context.EmitStvectmp();
}
for (int Index = 0; Index < Elems; Index++) for (int Index = 0; Index < Elems; Index++)
{ {
EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1); EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
@ -185,9 +192,12 @@ namespace ChocolArm64.Instruction
Context.EmitLsr(ESize); Context.EmitLsr(ESize);
EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size); EmitVectorInsertTmp(Context, Part + Index, Op.Size);
} }
Context.EmitLdvectmp();
Context.EmitStvec(Op.Rd);
if (Part == 0) if (Part == 0)
{ {
EmitVectorZeroUpper(Context, Op.Rd); EmitVectorZeroUpper(Context, Op.Rd);
@ -1062,6 +1072,11 @@ namespace ChocolArm64.Instruction
EmitVectorSaturatingNarrowOpSxZx(Context, () => { }); EmitVectorSaturatingNarrowOpSxZx(Context, () => { });
} }
public static void Ssubw_V(AILEmitterCtx Context)
{
EmitVectorWidenRmBinaryOpSx(Context, () => Context.Emit(OpCodes.Sub));
}
public static void Sub_S(AILEmitterCtx Context) public static void Sub_S(AILEmitterCtx Context)
{ {
EmitScalarBinaryOpZx(Context, () => Context.Emit(OpCodes.Sub)); EmitScalarBinaryOpZx(Context, () => Context.Emit(OpCodes.Sub));
@ -1215,5 +1230,10 @@ namespace ChocolArm64.Instruction
{ {
EmitVectorSaturatingNarrowOpZxZx(Context, () => { }); EmitVectorSaturatingNarrowOpZxZx(Context, () => { });
} }
public static void Usubw_V(AILEmitterCtx Context)
{
EmitVectorWidenRmBinaryOpZx(Context, () => Context.Emit(OpCodes.Sub));
}
} }
} }

View file

@ -364,7 +364,7 @@ namespace ChocolArm64.Instruction
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp; AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Bytes = Op.GetBitsCount() >> 3;
int Elems = (!Scalar ? Bytes >> Op.Size : 1); int Elems = !Scalar ? Bytes >> Op.Size : 1;
ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size)); ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
@ -408,7 +408,7 @@ namespace ChocolArm64.Instruction
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Bytes = Op.GetBitsCount() >> 3;
int Elems = (!Scalar ? Bytes >> Op.Size : 1); int Elems = !Scalar ? Bytes >> Op.Size : 1;
ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size)); ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));

View file

@ -419,20 +419,25 @@ namespace ChocolArm64.Instruction
int SizeF = Op.Size & 1; int SizeF = Op.Size & 1;
int Bytes = Op.GetBitsCount() >> 3; int Bytes = Op.GetBitsCount() >> 3;
int Elems = Bytes >> SizeF + 2;
for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++) bool Rd = (Opers & OperFlags.Rd) != 0;
bool Rn = (Opers & OperFlags.Rn) != 0;
bool Rm = (Opers & OperFlags.Rm) != 0;
for (int Index = 0; Index < Elems; Index++)
{ {
if (Opers.HasFlag(OperFlags.Rd)) if (Rd)
{ {
EmitVectorExtractF(Context, Op.Rd, Index, SizeF); EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
} }
if (Opers.HasFlag(OperFlags.Rn)) if (Rn)
{ {
EmitVectorExtractF(Context, Op.Rn, Index, SizeF); EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
} }
if (Opers.HasFlag(OperFlags.Rm)) if (Rm)
{ {
EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF); EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
} }
@ -469,8 +474,9 @@ namespace ChocolArm64.Instruction
int SizeF = Op.Size & 1; int SizeF = Op.Size & 1;
int Bytes = Op.GetBitsCount() >> 3; int Bytes = Op.GetBitsCount() >> 3;
int Elems = Bytes >> SizeF + 2;
for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++) for (int Index = 0; Index < Elems; Index++)
{ {
if (Ternary) if (Ternary)
{ {
@ -531,19 +537,23 @@ namespace ChocolArm64.Instruction
int Bytes = Op.GetBitsCount() >> 3; int Bytes = Op.GetBitsCount() >> 3;
int Elems = Bytes >> Op.Size; int Elems = Bytes >> Op.Size;
bool Rd = (Opers & OperFlags.Rd) != 0;
bool Rn = (Opers & OperFlags.Rn) != 0;
bool Rm = (Opers & OperFlags.Rm) != 0;
for (int Index = 0; Index < Elems; Index++) for (int Index = 0; Index < Elems; Index++)
{ {
if (Opers.HasFlag(OperFlags.Rd)) if (Rd)
{ {
EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed); EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
} }
if (Opers.HasFlag(OperFlags.Rn)) if (Rn)
{ {
EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed); EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
} }
if (Opers.HasFlag(OperFlags.Rm)) if (Rm)
{ {
EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed); EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
} }
@ -662,9 +672,6 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
Context.EmitLdvec(Op.Rd);
Context.EmitStvectmp();
int Elems = 8 >> Op.Size; int Elems = 8 >> Op.Size;
int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0; int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
@ -707,9 +714,6 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
Context.EmitLdvec(Op.Rd);
Context.EmitStvectmp();
int Elems = 8 >> Op.Size; int Elems = 8 >> Op.Size;
int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0; int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
@ -747,21 +751,25 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Words = Op.GetBitsCount() >> 4;
int Pairs = Words >> Op.Size;
int Elems = Bytes >> Op.Size; for (int Index = 0; Index < Pairs; Index++)
int Half = Elems >> 1;
for (int Index = 0; Index < Elems; Index++)
{ {
int Elem = (Index & (Half - 1)) << 1; int Idx = Index << 1;
EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size, Signed); EmitVectorExtract(Context, Op.Rn, Idx, Op.Size, Signed);
EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size, Signed); EmitVectorExtract(Context, Op.Rn, Idx + 1, Op.Size, Signed);
Emit(); Emit();
EmitVectorInsertTmp(Context, Index, Op.Size); EmitVectorExtract(Context, Op.Rm, Idx, Op.Size, Signed);
EmitVectorExtract(Context, Op.Rm, Idx + 1, Op.Size, Signed);
Emit();
EmitVectorInsertTmp(Context, Pairs + Index, Op.Size);
EmitVectorInsertTmp(Context, Index, Op.Size);
} }
Context.EmitLdvectmp(); Context.EmitLdvectmp();
@ -813,16 +821,23 @@ namespace ChocolArm64.Instruction
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp; AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
int Elems = !Scalar ? 8 >> Op.Size : 1; int Elems = !Scalar ? 8 >> Op.Size : 1;
int ESize = 8 << Op.Size; int ESize = 8 << Op.Size;
int Part = !Scalar && (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0; int Part = !Scalar && (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0;
long TMaxValue = SignedDst ? (1 << (ESize - 1)) - 1 : (1L << ESize) - 1L; long TMaxValue = SignedDst ? (1 << (ESize - 1)) - 1 : (long)(~0UL >> (64 - ESize));
long TMinValue = SignedDst ? -((1 << (ESize - 1))) : 0; long TMinValue = SignedDst ? -((1 << (ESize - 1))) : 0;
Context.EmitLdc_I8(0L); Context.EmitLdc_I8(0L);
Context.EmitSttmp(); Context.EmitSttmp();
if (Part != 0)
{
Context.EmitLdvec(Op.Rd);
Context.EmitStvectmp();
}
for (int Index = 0; Index < Elems; Index++) for (int Index = 0; Index < Elems; Index++)
{ {
AILLabel LblLe = new AILLabel(); AILLabel LblLe = new AILLabel();
@ -864,12 +879,15 @@ namespace ChocolArm64.Instruction
if (Scalar) if (Scalar)
{ {
EmitVectorZeroLower(Context, Op.Rd); EmitVectorZeroLowerTmp(Context);
} }
EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size); EmitVectorInsertTmp(Context, Part + Index, Op.Size);
} }
Context.EmitLdvectmp();
Context.EmitStvec(Op.Rd);
if (Part == 0) if (Part == 0)
{ {
EmitVectorZeroUpper(Context, Op.Rd); EmitVectorZeroUpper(Context, Op.Rd);
@ -953,6 +971,11 @@ namespace ChocolArm64.Instruction
EmitVectorInsert(Context, Rd, 0, 3, 0); EmitVectorInsert(Context, Rd, 0, 3, 0);
} }
public static void EmitVectorZeroLowerTmp(AILEmitterCtx Context)
{
EmitVectorInsertTmp(Context, 0, 3, 0);
}
public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd) public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
{ {
EmitVectorInsert(Context, Rd, 1, 3, 0); EmitVectorInsert(Context, Rd, 1, 3, 0);
@ -998,6 +1021,20 @@ namespace ChocolArm64.Instruction
Context.EmitStvec(Reg); Context.EmitStvec(Reg);
} }
public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size, long Value)
{
ThrowIfInvalid(Index, Size);
Context.EmitLdc_I8(Value);
Context.EmitLdvectmp();
Context.EmitLdc_I4(Index);
Context.EmitLdc_I4(Size);
AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
Context.EmitStvectmp();
}
public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size) public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
{ {
ThrowIfInvalidF(Index, Size); ThrowIfInvalidF(Index, Size);

View file

@ -295,13 +295,22 @@ namespace ChocolArm64.Instruction
int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0; int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
if (Part != 0)
{
Context.EmitLdvec(Op.Rd);
Context.EmitStvectmp();
}
for (int Index = 0; Index < Elems; Index++) for (int Index = 0; Index < Elems; Index++)
{ {
EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1); EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size); EmitVectorInsertTmp(Context, Part + Index, Op.Size);
} }
Context.EmitLdvectmp();
Context.EmitStvec(Op.Rd);
if (Part == 0) if (Part == 0)
{ {
EmitVectorZeroUpper(Context, Op.Rd); EmitVectorZeroUpper(Context, Op.Rd);
@ -331,17 +340,18 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Words = Op.GetBitsCount() >> 4;
int Pairs = Words >> Op.Size;
int Elems = Bytes >> Op.Size; for (int Index = 0; Index < Pairs; Index++)
for (int Index = 0; Index < Elems; Index++)
{ {
int Elem = (Index & ~1) + Part; int Idx = Index << 1;
EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size); EmitVectorExtractZx(Context, Op.Rn, Idx + Part, Op.Size);
EmitVectorExtractZx(Context, Op.Rm, Idx + Part, Op.Size);
EmitVectorInsertTmp(Context, Index, Op.Size); EmitVectorInsertTmp(Context, Idx + 1, Op.Size);
EmitVectorInsertTmp(Context, Idx, Op.Size);
} }
Context.EmitLdvectmp(); Context.EmitLdvectmp();
@ -357,18 +367,18 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Words = Op.GetBitsCount() >> 4;
int Pairs = Words >> Op.Size;
int Elems = Bytes >> Op.Size; for (int Index = 0; Index < Pairs; Index++)
int Half = Elems >> 1;
for (int Index = 0; Index < Elems; Index++)
{ {
int Elem = Part + ((Index & (Half - 1)) << 1); int Idx = Index << 1;
EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem, Op.Size); EmitVectorExtractZx(Context, Op.Rn, Idx + Part, Op.Size);
EmitVectorExtractZx(Context, Op.Rm, Idx + Part, Op.Size);
EmitVectorInsertTmp(Context, Index, Op.Size); EmitVectorInsertTmp(Context, Pairs + Index, Op.Size);
EmitVectorInsertTmp(Context, Index, Op.Size);
} }
Context.EmitLdvectmp(); Context.EmitLdvectmp();
@ -384,18 +394,20 @@ namespace ChocolArm64.Instruction
{ {
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp; AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
int Bytes = Op.GetBitsCount() >> 3; int Words = Op.GetBitsCount() >> 4;
int Pairs = Words >> Op.Size;
int Elems = Bytes >> Op.Size; int Base = Part != 0 ? Pairs : 0;
int Half = Elems >> 1;
for (int Index = 0; Index < Elems; Index++) for (int Index = 0; Index < Pairs; Index++)
{ {
int Elem = Part * Half + (Index >> 1); int Idx = Index << 1;
EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size); EmitVectorExtractZx(Context, Op.Rn, Base + Index, Op.Size);
EmitVectorExtractZx(Context, Op.Rm, Base + Index, Op.Size);
EmitVectorInsertTmp(Context, Index, Op.Size); EmitVectorInsertTmp(Context, Idx + 1, Op.Size);
EmitVectorInsertTmp(Context, Idx, Op.Size);
} }
Context.EmitLdvectmp(); Context.EmitLdvectmp();

View file

@ -204,6 +204,13 @@ namespace ChocolArm64.Memory
return Modified; return Modified;
} }
public IntPtr GetHostAddress(long Position, long Size)
{
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
return (IntPtr)(RamPtr + (ulong)Position);
}
public sbyte ReadSByte(long Position) public sbyte ReadSByte(long Position)
{ {
return (sbyte)ReadByte(Position); return (sbyte)ReadByte(Position);

View file

@ -0,0 +1,91 @@
namespace Ryujinx.Audio.Adpcm
{
public static class AdpcmDecoder
{
private const int SamplesPerFrame = 14;
private const int BytesPerFrame = 8;
public static int[] Decode(byte[] Buffer, AdpcmDecoderContext Context)
{
int Samples = GetSamplesCountFromSize(Buffer.Length);
int[] Pcm = new int[Samples * 2];
short History0 = Context.History0;
short History1 = Context.History1;
int InputOffset = 0;
int OutputOffset = 0;
while (InputOffset < Buffer.Length)
{
byte Header = Buffer[InputOffset++];
int Scale = 0x800 << (Header & 0xf);
int CoeffIndex = (Header >> 4) & 7;
short Coeff0 = Context.Coefficients[CoeffIndex * 2 + 0];
short Coeff1 = Context.Coefficients[CoeffIndex * 2 + 1];
int FrameSamples = SamplesPerFrame;
if (FrameSamples > Samples)
{
FrameSamples = Samples;
}
int Value = 0;
for (int SampleIndex = 0; SampleIndex < FrameSamples; SampleIndex++)
{
int Sample;
if ((SampleIndex & 1) == 0)
{
Value = Buffer[InputOffset++];
Sample = (Value << 24) >> 28;
}
else
{
Sample = (Value << 28) >> 28;
}
int Prediction = Coeff0 * History0 + Coeff1 * History1;
Sample = (Sample * Scale + Prediction + 0x400) >> 11;
short SaturatedSample = DspUtils.Saturate(Sample);
History1 = History0;
History0 = SaturatedSample;
Pcm[OutputOffset++] = SaturatedSample;
Pcm[OutputOffset++] = SaturatedSample;
}
Samples -= FrameSamples;
}
Context.History0 = History0;
Context.History1 = History1;
return Pcm;
}
public static long GetSizeFromSamplesCount(int SamplesCount)
{
int Frames = SamplesCount / SamplesPerFrame;
return Frames * BytesPerFrame;
}
public static int GetSamplesCountFromSize(long Size)
{
int Frames = (int)(Size / BytesPerFrame);
return Frames * SamplesPerFrame;
}
}
}

View file

@ -0,0 +1,10 @@
namespace Ryujinx.Audio.Adpcm
{
public class AdpcmDecoderContext
{
public short[] Coefficients;
public short History0;
public short History1;
}
}

16
Ryujinx.Audio/DspUtils.cs Normal file
View file

@ -0,0 +1,16 @@
namespace Ryujinx.Audio.Adpcm
{
public static class DspUtils
{
public static short Saturate(int Value)
{
if (Value > short.MaxValue)
Value = short.MaxValue;
if (Value < short.MinValue)
Value = short.MinValue;
return (short)Value;
}
}
}

View file

@ -2,11 +2,7 @@ namespace Ryujinx.Audio
{ {
public interface IAalOutput public interface IAalOutput
{ {
int OpenTrack( int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback);
int SampleRate,
int Channels,
ReleaseCallback Callback,
out AudioFormat Format);
void CloseTrack(int Track); void CloseTrack(int Track);
@ -14,7 +10,7 @@ namespace Ryujinx.Audio
long[] GetReleasedBuffers(int Track, int MaxCount); long[] GetReleasedBuffers(int Track, int MaxCount);
void AppendBuffer(int Track, long Tag, byte[] Buffer); void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct;
void Start(int Track); void Start(int Track);
void Stop(int Track); void Stop(int Track);

View file

@ -3,6 +3,7 @@ using OpenTK.Audio.OpenAL;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace Ryujinx.Audio.OpenAL namespace Ryujinx.Audio.OpenAL
@ -226,15 +227,9 @@ namespace Ryujinx.Audio.OpenAL
while (KeepPolling); while (KeepPolling);
} }
public int OpenTrack( public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
int SampleRate,
int Channels,
ReleaseCallback Callback,
out AudioFormat Format)
{ {
Format = AudioFormat.PcmInt16; Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
Track Td = new Track(SampleRate, GetALFormat(Channels, Format), Callback);
for (int Id = 0; Id < MaxTracks; Id++) for (int Id = 0; Id < MaxTracks; Id++)
{ {
@ -247,38 +242,16 @@ namespace Ryujinx.Audio.OpenAL
return -1; return -1;
} }
private ALFormat GetALFormat(int Channels, AudioFormat Format) private ALFormat GetALFormat(int Channels)
{ {
if (Channels == 1) switch (Channels)
{ {
switch (Format) case 1: return ALFormat.Mono16;
{ case 2: return ALFormat.Stereo16;
case AudioFormat.PcmInt8: return ALFormat.Mono8; case 6: return ALFormat.Multi51Chn16Ext;
case AudioFormat.PcmInt16: return ALFormat.Mono16;
}
}
else if (Channels == 2)
{
switch (Format)
{
case AudioFormat.PcmInt8: return ALFormat.Stereo8;
case AudioFormat.PcmInt16: return ALFormat.Stereo16;
}
}
else if (Channels == 6)
{
switch (Format)
{
case AudioFormat.PcmInt8: return ALFormat.Multi51Chn8Ext;
case AudioFormat.PcmInt16: return ALFormat.Multi51Chn16Ext;
}
}
else
{
throw new ArgumentOutOfRangeException(nameof(Channels));
} }
throw new ArgumentException(nameof(Format)); throw new ArgumentOutOfRangeException(nameof(Channels));
} }
public void CloseTrack(int Track) public void CloseTrack(int Track)
@ -309,13 +282,15 @@ namespace Ryujinx.Audio.OpenAL
return null; return null;
} }
public void AppendBuffer(int Track, long Tag, byte[] Buffer) public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
{ {
if (Tracks.TryGetValue(Track, out Track Td)) if (Tracks.TryGetValue(Track, out Track Td))
{ {
int BufferId = Td.AppendBuffer(Tag); int BufferId = Td.AppendBuffer(Tag);
AL.BufferData(BufferId, Td.Format, Buffer, Buffer.Length, Td.SampleRate); int Size = Buffer.Length * Marshal.SizeOf<T>();
AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
AL.SourceQueueBuffer(Td.SourceId, BufferId); AL.SourceQueueBuffer(Td.SourceId, BufferId);
@ -366,7 +341,5 @@ namespace Ryujinx.Audio.OpenAL
return PlaybackState.Stopped; return PlaybackState.Stopped;
} }
} }
} }

View file

@ -6,17 +6,21 @@ namespace Ryujinx.Graphics.Gal
R16G16B16A16 = 0x3, R16G16B16A16 = 0x3,
A8B8G8R8 = 0x8, A8B8G8R8 = 0x8,
R32 = 0xf, R32 = 0xf,
BC6H_SF16 = 0x10,
BC6H_UF16 = 0x11,
A1B5G5R5 = 0x14, A1B5G5R5 = 0x14,
B5G6R5 = 0x15, B5G6R5 = 0x15,
BC7U = 0x17, BC7U = 0x17,
G8R8 = 0x18, G8R8 = 0x18,
R16 = 0x1b, R16 = 0x1b,
R8 = 0x1d, R8 = 0x1d,
BF10GF11RF11 = 0x21,
BC1 = 0x24, BC1 = 0x24,
BC2 = 0x25, BC2 = 0x25,
BC3 = 0x26, BC3 = 0x26,
BC4 = 0x27, BC4 = 0x27,
BC5 = 0x28, BC5 = 0x28,
Z24S8 = 0x29,
ZF32 = 0x2f, ZF32 = 0x2f,
Astc2D4x4 = 0x40, Astc2D4x4 = 0x40,
Astc2D5x5 = 0x41, Astc2D5x5 = 0x41,

View file

@ -14,7 +14,7 @@ namespace Ryujinx.Graphics.Gal
void Set(byte[] Data, int Width, int Height); void Set(byte[] Data, int Width, int Height);
void SetTransform(float SX, float SY, float Rotate, float TX, float TY); void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom);
void SetWindowSize(int Width, int Height); void SetWindowSize(int Width, int Height);
@ -22,6 +22,25 @@ namespace Ryujinx.Graphics.Gal
void Render(); void Render();
void Copy(
long SrcKey,
long DstKey,
int SrcX0,
int SrcY0,
int SrcX1,
int SrcY1,
int DstX0,
int DstY0,
int DstX1,
int DstY1);
void GetBufferData(long Key, Action<byte[]> Callback); void GetBufferData(long Key, Action<byte[]> Callback);
void SetBufferData(
long Key,
int Width,
int Height,
GalTextureFormat Format,
byte[] Buffer);
} }
} }

View file

@ -1,3 +1,5 @@
using System;
namespace Ryujinx.Graphics.Gal namespace Ryujinx.Graphics.Gal
{ {
public interface IGalRasterizer public interface IGalRasterizer
@ -45,9 +47,9 @@ namespace Ryujinx.Graphics.Gal
void SetPrimitiveRestartIndex(uint Index); void SetPrimitiveRestartIndex(uint Index);
void CreateVbo(long Key, byte[] Buffer); void CreateVbo(long Key, int DataSize, IntPtr HostAddress);
void CreateIbo(long Key, byte[] Buffer); void CreateIbo(long Key, int DataSize, IntPtr HostAddress);
void SetVertexArray(int Stride, long VboKey, GalVertexAttrib[] Attribs); void SetVertexArray(int Stride, long VboKey, GalVertexAttrib[] Attribs);

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Ryujinx.Graphics.Gal namespace Ryujinx.Graphics.Gal
@ -10,7 +11,7 @@ namespace Ryujinx.Graphics.Gal
IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key); IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key);
void SetConstBuffer(long Key, int Cbuf, byte[] Data); void SetConstBuffer(long Key, int Cbuf, int DataSize, IntPtr HostAddress);
void EnsureTextureBinding(string UniformName, int Value); void EnsureTextureBinding(string UniformName, int Value);

View file

@ -1,13 +0,0 @@
#version 330 core
precision highp float;
uniform sampler2D tex;
in vec2 tex_coord;
out vec4 out_frag_color;
void main(void) {
out_frag_color = texture(tex, tex_coord);
}

View file

@ -1,28 +0,0 @@
#version 330 core
precision highp float;
uniform mat2 transform;
uniform vec2 window_size;
uniform vec2 offset;
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec2 in_tex_coord;
out vec2 tex_coord;
// Have a fixed aspect ratio, fit the image within the available space.
vec2 get_scale_ratio(void) {
vec2 native_size = vec2(1280, 720);
vec2 ratio = vec2(
(window_size.y * native_size.x) / (native_size.y * window_size.x),
(window_size.x * native_size.y) / (native_size.x * window_size.y)
);
return min(ratio, 1);
}
void main(void) {
tex_coord = in_tex_coord;
vec2 t_pos = (transform * in_position) + offset;
gl_Position = vec4(t_pos * get_scale_ratio(), 0, 1);
}

View file

@ -139,6 +139,8 @@ namespace Ryujinx.Graphics.Gal.OpenGL
case GalTextureFormat.R16: return (PixelFormat.Red, PixelType.HalfFloat); case GalTextureFormat.R16: return (PixelFormat.Red, PixelType.HalfFloat);
case GalTextureFormat.R8: return (PixelFormat.Red, PixelType.UnsignedByte); case GalTextureFormat.R8: return (PixelFormat.Red, PixelType.UnsignedByte);
case GalTextureFormat.ZF32: return (PixelFormat.DepthComponent, PixelType.Float); case GalTextureFormat.ZF32: return (PixelFormat.DepthComponent, PixelType.Float);
case GalTextureFormat.BF10GF11RF11: return (PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev);
case GalTextureFormat.Z24S8: return (PixelFormat.DepthStencil, PixelType.UnsignedInt248);
} }
throw new NotImplementedException(Format.ToString()); throw new NotImplementedException(Format.ToString());
@ -148,12 +150,14 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
switch (Format) switch (Format)
{ {
case GalTextureFormat.BC7U: return InternalFormat.CompressedRgbaBptcUnorm; case GalTextureFormat.BC6H_UF16: return InternalFormat.CompressedRgbBptcUnsignedFloat;
case GalTextureFormat.BC1: return InternalFormat.CompressedRgbaS3tcDxt1Ext; case GalTextureFormat.BC6H_SF16: return InternalFormat.CompressedRgbBptcSignedFloat;
case GalTextureFormat.BC2: return InternalFormat.CompressedRgbaS3tcDxt3Ext; case GalTextureFormat.BC7U: return InternalFormat.CompressedRgbaBptcUnorm;
case GalTextureFormat.BC3: return InternalFormat.CompressedRgbaS3tcDxt5Ext; case GalTextureFormat.BC1: return InternalFormat.CompressedRgbaS3tcDxt1Ext;
case GalTextureFormat.BC4: return InternalFormat.CompressedRedRgtc1; case GalTextureFormat.BC2: return InternalFormat.CompressedRgbaS3tcDxt3Ext;
case GalTextureFormat.BC5: return InternalFormat.CompressedRgRgtc2; case GalTextureFormat.BC3: return InternalFormat.CompressedRgbaS3tcDxt5Ext;
case GalTextureFormat.BC4: return InternalFormat.CompressedRedRgtc1;
case GalTextureFormat.BC5: return InternalFormat.CompressedRgRgtc2;
} }
throw new NotImplementedException(Format.ToString()); throw new NotImplementedException(Format.ToString());

View file

@ -0,0 +1,43 @@
using OpenTK.Graphics.OpenGL;
namespace Ryujinx.Graphics.Gal.OpenGL
{
static class OGLExtension
{
private static bool Initialized = false;
private static bool EnhancedLayouts;
public static bool HasEnhancedLayouts()
{
EnsureInitialized();
return EnhancedLayouts;
}
private static void EnsureInitialized()
{
if (Initialized)
{
return;
}
EnhancedLayouts = HasExtension("GL_ARB_enhanced_layouts");
}
private static bool HasExtension(string Name)
{
int NumExtensions = GL.GetInteger(GetPName.NumExtensions);
for (int Extension = 0; Extension < NumExtensions; Extension++)
{
if (GL.GetString(StringNameIndexed.Extensions, Extension) == Name)
{
return true;
}
}
return false;
}
}
}

View file

@ -32,57 +32,49 @@ namespace Ryujinx.Graphics.Gal.OpenGL
public int RbHandle { get; private set; } public int RbHandle { get; private set; }
public int TexHandle { get; private set; } public int TexHandle { get; private set; }
public FrameBuffer(int Width, int Height) public FrameBuffer(int Width, int Height, bool HasRenderBuffer)
{ {
this.Width = Width; this.Width = Width;
this.Height = Height; this.Height = Height;
Handle = GL.GenFramebuffer(); Handle = GL.GenFramebuffer();
RbHandle = GL.GenRenderbuffer();
TexHandle = GL.GenTexture(); TexHandle = GL.GenTexture();
if (HasRenderBuffer)
{
RbHandle = GL.GenRenderbuffer();
}
} }
} }
private struct ShaderProgram private const int NativeWidth = 1280;
{ private const int NativeHeight = 720;
public int Handle;
public int VpHandle;
public int FpHandle;
}
private Dictionary<long, FrameBuffer> Fbs; private Dictionary<long, FrameBuffer> Fbs;
private ShaderProgram Shader;
private Rect Viewport; private Rect Viewport;
private Rect Window; private Rect Window;
private bool IsInitialized; private FrameBuffer CurrFb;
private FrameBuffer CurrReadFb;
private int RawFbTexWidth; private FrameBuffer RawFb;
private int RawFbTexHeight;
private int RawFbTexHandle;
private int CurrFbHandle; private bool FlipX;
private int CurrTexHandle; private bool FlipY;
private int VaoHandle; private int CropTop;
private int VboHandle; private int CropLeft;
private int CropRight;
private int CropBottom;
public OGLFrameBuffer() public OGLFrameBuffer()
{ {
Fbs = new Dictionary<long, FrameBuffer>(); Fbs = new Dictionary<long, FrameBuffer>();
Shader = new ShaderProgram();
} }
public void Create(long Key, int Width, int Height) public void Create(long Key, int Width, int Height)
{ {
//TODO: We should either use the original frame buffer size,
//or just remove the Width/Height arguments.
Width = Window.Width;
Height = Window.Height;
if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
{ {
if (Fb.Width != Width || if (Fb.Width != Width ||
@ -97,7 +89,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
return; return;
} }
Fb = new FrameBuffer(Width, Height); Fb = new FrameBuffer(Width, Height, true);
SetupTexture(Fb.TexHandle, Width, Height); SetupTexture(Fb.TexHandle, Width, Height);
@ -125,8 +117,6 @@ namespace Ryujinx.Graphics.Gal.OpenGL
GL.DrawBuffer(DrawBufferMode.ColorAttachment0); GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
GL.Viewport(0, 0, Width, Height);
Fbs.Add(Key, Fb); Fbs.Add(Key, Fb);
} }
@ -136,7 +126,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
GL.BindFramebuffer(FramebufferTarget.Framebuffer, Fb.Handle); GL.BindFramebuffer(FramebufferTarget.Framebuffer, Fb.Handle);
CurrFbHandle = Fb.Handle; CurrFb = Fb;
} }
} }
@ -154,75 +144,50 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
{ {
CurrTexHandle = Fb.TexHandle; CurrReadFb = Fb;
} }
} }
public void Set(byte[] Data, int Width, int Height) public void Set(byte[] Data, int Width, int Height)
{ {
if (RawFbTexHandle == 0) if (RawFb == null)
{ {
RawFbTexHandle = GL.GenTexture(); CreateRawFb(Width, Height);
} }
if (RawFbTexWidth != Width || if (RawFb.Width != Width ||
RawFbTexHeight != Height) RawFb.Height != Height)
{ {
SetupTexture(RawFbTexHandle, Width, Height); SetupTexture(RawFb.TexHandle, Width, Height);
RawFbTexWidth = Width; RawFb.Width = Width;
RawFbTexHeight = Height; RawFb.Height = Height;
} }
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, RawFbTexHandle); GL.BindTexture(TextureTarget.Texture2D, RawFb.TexHandle);
(PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8); (PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, Width, Height, Format, Type, Data); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, Width, Height, Format, Type, Data);
CurrTexHandle = RawFbTexHandle; CurrReadFb = RawFb;
} }
public void SetTransform(float SX, float SY, float Rotate, float TX, float TY) public void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom)
{ {
EnsureInitialized(); this.FlipX = FlipX;
this.FlipY = FlipY;
Matrix2 Transform; CropTop = Top;
CropLeft = Left;
Transform = Matrix2.CreateScale(SX, SY); CropRight = Right;
Transform *= Matrix2.CreateRotation(Rotate); CropBottom = Bottom;
Vector2 Offs = new Vector2(TX, TY);
int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
GL.UseProgram(Shader.Handle);
int TransformUniformLocation = GL.GetUniformLocation(Shader.Handle, "transform");
GL.UniformMatrix2(TransformUniformLocation, false, ref Transform);
int OffsetUniformLocation = GL.GetUniformLocation(Shader.Handle, "offset");
GL.Uniform2(OffsetUniformLocation, ref Offs);
GL.UseProgram(CurrentProgram);
} }
public void SetWindowSize(int Width, int Height) public void SetWindowSize(int Width, int Height)
{ {
int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
GL.UseProgram(Shader.Handle);
int WindowSizeUniformLocation = GL.GetUniformLocation(Shader.Handle, "window_size");
GL.Uniform2(WindowSizeUniformLocation, new Vector2(Width, Height));
GL.UseProgram(CurrentProgram);
Window = new Rect(0, 0, Width, Height); Window = new Rect(0, 0, Width, Height);
} }
@ -230,80 +195,104 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
Viewport = new Rect(X, Y, Width, Height); Viewport = new Rect(X, Y, Width, Height);
//TODO SetViewport(Viewport);
}
private void SetViewport(Rect Viewport)
{
GL.Viewport(
Viewport.X,
Viewport.Y,
Viewport.Width,
Viewport.Height);
} }
public void Render() public void Render()
{ {
if (CurrTexHandle != 0) if (CurrReadFb != null)
{ {
EnsureInitialized(); int SrcX0, SrcX1, SrcY0, SrcY1;
//bool CullFaceEnable = GL.IsEnabled(EnableCap.CullFace); if (CropLeft == 0 && CropRight == 0)
{
SrcX0 = 0;
SrcX1 = CurrReadFb.Width;
}
else
{
SrcX0 = CropLeft;
SrcX1 = CropRight;
}
bool DepthTestEnable = GL.IsEnabled(EnableCap.DepthTest); if (CropTop == 0 && CropBottom == 0)
{
SrcY0 = 0;
SrcY1 = CurrReadFb.Height;
}
else
{
SrcY0 = CropTop;
SrcY1 = CropBottom;
}
bool StencilTestEnable = GL.IsEnabled(EnableCap.StencilTest); float RatioX = MathF.Min(1f, (Window.Height * (float)NativeWidth) / ((float)NativeHeight * Window.Width));
float RatioY = MathF.Min(1f, (Window.Width * (float)NativeHeight) / ((float)NativeWidth * Window.Height));
bool AlphaBlendEnable = GL.IsEnabled(EnableCap.Blend); int DstWidth = (int)(Window.Width * RatioX);
int DstHeight = (int)(Window.Height * RatioY);
//GL.Disable(EnableCap.CullFace); int DstPaddingX = (Window.Width - DstWidth) / 2;
int DstPaddingY = (Window.Height - DstHeight) / 2;
GL.Disable(EnableCap.DepthTest); int DstX0 = FlipX ? Window.Width - DstPaddingX : DstPaddingX;
int DstX1 = FlipX ? DstPaddingX : Window.Width - DstPaddingX;
GL.Disable(EnableCap.StencilTest); int DstY0 = FlipY ? DstPaddingY : Window.Height - DstPaddingY;
int DstY1 = FlipY ? Window.Height - DstPaddingY : DstPaddingY;
GL.Disable(EnableCap.Blend);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, CurrTexHandle);
int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
SetViewport(Window); GL.Viewport(0, 0, Window.Width, Window.Height);
GL.Clear( GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, CurrReadFb.Handle);
ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
GL.BindVertexArray(VaoHandle); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.UseProgram(Shader.Handle); GL.BlitFramebuffer(
SrcX0, SrcY0, SrcX1, SrcY1,
GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4); DstX0, DstY0, DstX1, DstY1,
ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);
//Restore the original state.
GL.BindFramebuffer(FramebufferTarget.Framebuffer, CurrFbHandle);
GL.UseProgram(CurrentProgram);
//if (CullFaceEnable)
//{
// GL.Enable(EnableCap.CullFace);
//}
if (DepthTestEnable)
{
GL.Enable(EnableCap.DepthTest);
}
if (StencilTestEnable)
{
GL.Enable(EnableCap.StencilTest);
}
if (AlphaBlendEnable)
{
GL.Enable(EnableCap.Blend);
}
//GL.Viewport(0, 0, 1280, 720);
} }
} }
public void Copy(
long SrcKey,
long DstKey,
int SrcX0,
int SrcY0,
int SrcX1,
int SrcY1,
int DstX0,
int DstY0,
int DstX1,
int DstY1)
{
if (Fbs.TryGetValue(SrcKey, out FrameBuffer SrcFb) &&
Fbs.TryGetValue(DstKey, out FrameBuffer DstFb))
{
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb.Handle);
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DstFb.Handle);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.BlitFramebuffer(
SrcX0, SrcY0, SrcX1, SrcY1,
DstX0, DstY0, DstX1, DstY1,
ClearBufferMask.ColorBufferBit,
BlitFramebufferFilter.Linear);
}
}
public void GetBufferData(long Key, Action<byte[]> Callback) public void GetBufferData(long Key, Action<byte[]> Callback)
{ {
if (Fbs.TryGetValue(Key, out FrameBuffer Fb)) if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
@ -324,98 +313,61 @@ namespace Ryujinx.Graphics.Gal.OpenGL
Data); Data);
Callback(Data); Callback(Data);
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, CurrFbHandle);
} }
} }
private void SetViewport(Rect Viewport) public void SetBufferData(
long Key,
int Width,
int Height,
GalTextureFormat Format,
byte[] Buffer)
{ {
GL.Viewport( if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
Viewport.X,
Viewport.Y,
Viewport.Width,
Viewport.Height);
}
private void EnsureInitialized()
{
if (!IsInitialized)
{ {
IsInitialized = true; GL.BindTexture(TextureTarget.Texture2D, Fb.TexHandle);
SetupShader(); const int Level = 0;
SetupVertex(); const int Border = 0;
const PixelInternalFormat InternalFmt = PixelInternalFormat.Rgba;
(PixelFormat GlFormat, PixelType Type) = OGLEnumConverter.GetTextureFormat(Format);
GL.TexImage2D(
TextureTarget.Texture2D,
Level,
InternalFmt,
Width,
Height,
Border,
GlFormat,
Type,
Buffer);
} }
} }
private void SetupShader() private void CreateRawFb(int Width, int Height)
{ {
Shader.VpHandle = GL.CreateShader(ShaderType.VertexShader); if (RawFb == null)
Shader.FpHandle = GL.CreateShader(ShaderType.FragmentShader);
string VpSource = EmbeddedResource.GetString("GlFbVtxShader");
string FpSource = EmbeddedResource.GetString("GlFbFragShader");
GL.ShaderSource(Shader.VpHandle, VpSource);
GL.ShaderSource(Shader.FpHandle, FpSource);
GL.CompileShader(Shader.VpHandle);
GL.CompileShader(Shader.FpHandle);
Shader.Handle = GL.CreateProgram();
GL.AttachShader(Shader.Handle, Shader.VpHandle);
GL.AttachShader(Shader.Handle, Shader.FpHandle);
GL.LinkProgram(Shader.Handle);
GL.UseProgram(Shader.Handle);
Matrix2 Transform = Matrix2.Identity;
int TexUniformLocation = GL.GetUniformLocation(Shader.Handle, "tex");
GL.Uniform1(TexUniformLocation, 0);
int WindowSizeUniformLocation = GL.GetUniformLocation(Shader.Handle, "window_size");
GL.Uniform2(WindowSizeUniformLocation, new Vector2(1280.0f, 720.0f));
int TransformUniformLocation = GL.GetUniformLocation(Shader.Handle, "transform");
GL.UniformMatrix2(TransformUniformLocation, false, ref Transform);
}
private void SetupVertex()
{
VaoHandle = GL.GenVertexArray();
VboHandle = GL.GenBuffer();
float[] Buffer = new float[]
{ {
-1, 1, 0, 0, RawFb = new FrameBuffer(Width, Height, false);
1, 1, 1, 0,
-1, -1, 0, 1,
1, -1, 1, 1
};
IntPtr Length = new IntPtr(Buffer.Length * 4); SetupTexture(RawFb.TexHandle, Width, Height);
GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle); RawFb.Width = Width;
GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw); RawFb.Height = Height;
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(VaoHandle); GL.BindFramebuffer(FramebufferTarget.Framebuffer, RawFb.Handle);
GL.EnableVertexAttribArray(0); GL.FramebufferTexture(
FramebufferTarget.Framebuffer,
FramebufferAttachment.ColorAttachment0,
RawFb.TexHandle,
0);
GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle); GL.Viewport(0, 0, Width, Height);
}
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 16, 0);
GL.EnableVertexAttribArray(1);
GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 16, 8);
} }
private void SetupTexture(int Handle, int Width, int Height) private void SetupTexture(int Handle, int Width, int Height)

View file

@ -211,28 +211,28 @@ namespace Ryujinx.Graphics.Gal.OpenGL
GL.PrimitiveRestartIndex(Index); GL.PrimitiveRestartIndex(Index);
} }
public void CreateVbo(long Key, byte[] Buffer) public void CreateVbo(long Key, int DataSize, IntPtr HostAddress)
{ {
int Handle = GL.GenBuffer(); int Handle = GL.GenBuffer();
VboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length); VboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
IntPtr Length = new IntPtr(Buffer.Length); IntPtr Length = new IntPtr(DataSize);
GL.BindBuffer(BufferTarget.ArrayBuffer, Handle); GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw); GL.BufferData(BufferTarget.ArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
} }
public void CreateIbo(long Key, byte[] Buffer) public void CreateIbo(long Key, int DataSize, IntPtr HostAddress)
{ {
int Handle = GL.GenBuffer(); int Handle = GL.GenBuffer();
IboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length); IboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
IntPtr Length = new IntPtr(Buffer.Length); IntPtr Length = new IntPtr(DataSize);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle); GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw); GL.BufferData(BufferTarget.ElementArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
} }
public void SetVertexArray(int Stride, long VboKey, GalVertexAttrib[] Attribs) public void SetVertexArray(int Stride, long VboKey, GalVertexAttrib[] Attribs)

View file

@ -5,6 +5,8 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Buffer = System.Buffer;
namespace Ryujinx.Graphics.Gal.OpenGL namespace Ryujinx.Graphics.Gal.OpenGL
{ {
public class OGLShader : IGalShader public class OGLShader : IGalShader
@ -118,15 +120,20 @@ namespace Ryujinx.Graphics.Gal.OpenGL
if (IsDualVp) if (IsDualVp)
{ {
ShaderDumper.Dump(Memory, Position, Type, "a");
ShaderDumper.Dump(Memory, PositionB, Type, "b");
Program = Decompiler.Decompile( Program = Decompiler.Decompile(
Memory, Memory,
Position + 0x50, Position,
PositionB + 0x50, PositionB,
Type); Type);
} }
else else
{ {
Program = Decompiler.Decompile(Memory, Position + 0x50, Type); ShaderDumper.Dump(Memory, Position, Type);
Program = Decompiler.Decompile(Memory, Position, Type);
} }
return new ShaderStage( return new ShaderStage(
@ -146,7 +153,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
return Enumerable.Empty<ShaderDeclInfo>(); return Enumerable.Empty<ShaderDeclInfo>();
} }
public void SetConstBuffer(long Key, int Cbuf, byte[] Data) public void SetConstBuffer(long Key, int Cbuf, int DataSize, IntPtr HostAddress)
{ {
if (Stages.TryGetValue(Key, out ShaderStage Stage)) if (Stages.TryGetValue(Key, out ShaderStage Stage))
{ {
@ -154,13 +161,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
OGLStreamBuffer Buffer = GetConstBuffer(Stage.Type, Cbuf); OGLStreamBuffer Buffer = GetConstBuffer(Stage.Type, Cbuf);
int Size = Math.Min(Data.Length, Buffer.Size); int Size = Math.Min(DataSize, Buffer.Size);
byte[] Destiny = Buffer.Map(Size); Buffer.SetData(Size, HostAddress);
Array.Copy(Data, Destiny, Size);
Buffer.Unmap(Size);
} }
} }
} }
@ -193,6 +196,16 @@ namespace Ryujinx.Graphics.Gal.OpenGL
private void Bind(ShaderStage Stage) private void Bind(ShaderStage Stage)
{ {
if (Stage.Type == GalShaderType.Geometry)
{
//Enhanced layouts are required for Geometry shaders
//skip this stage if current driver has no ARB_enhanced_layouts
if (!OGLExtension.HasEnhancedLayouts())
{
return;
}
}
switch (Stage.Type) switch (Stage.Type)
{ {
case GalShaderType.Vertex: Current.Vertex = Stage; break; case GalShaderType.Vertex: Current.Vertex = Stage; break;
@ -244,7 +257,10 @@ namespace Ryujinx.Graphics.Gal.OpenGL
GL.UseProgram(Handle); GL.UseProgram(Handle);
BindUniformBuffers(Handle); if (CurrentProgramHandle != Handle)
{
BindUniformBuffers(Handle);
}
CurrentProgramHandle = Handle; CurrentProgramHandle = Handle;
} }
@ -263,7 +279,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
int FreeBinding = 0; int FreeBinding = 0;
int BindUniformBlocksIfNotNull(ShaderStage Stage) void BindUniformBlocksIfNotNull(ShaderStage Stage)
{ {
if (Stage != null) if (Stage != null)
{ {
@ -282,8 +298,6 @@ namespace Ryujinx.Graphics.Gal.OpenGL
FreeBinding++; FreeBinding++;
} }
} }
return FreeBinding;
} }
BindUniformBlocksIfNotNull(Current.Vertex); BindUniformBlocksIfNotNull(Current.Vertex);
@ -297,7 +311,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
int FreeBinding = 0; int FreeBinding = 0;
int BindUniformBuffersIfNotNull(ShaderStage Stage) void BindUniformBuffersIfNotNull(ShaderStage Stage)
{ {
if (Stage != null) if (Stage != null)
{ {
@ -310,8 +324,6 @@ namespace Ryujinx.Graphics.Gal.OpenGL
FreeBinding++; FreeBinding++;
} }
} }
return FreeBinding;
} }
BindUniformBuffersIfNotNull(Current.Vertex); BindUniformBuffersIfNotNull(Current.Vertex);
@ -332,7 +344,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
//Allocate a maximum of 64 KiB //Allocate a maximum of 64 KiB
int Size = Math.Min(GL.GetInteger(GetPName.MaxUniformBlockSize), 64 * 1024); int Size = Math.Min(GL.GetInteger(GetPName.MaxUniformBlockSize), 64 * 1024);
Buffer = OGLStreamBuffer.Create(BufferTarget.UniformBuffer, Size); Buffer = new OGLStreamBuffer(BufferTarget.UniformBuffer, Size);
ConstBuffers[StageIndex][Cbuf] = Buffer; ConstBuffers[StageIndex][Cbuf] = Buffer;
} }

View file

@ -1,9 +1,9 @@
using System;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
using System;
namespace Ryujinx.Graphics.Gal.OpenGL namespace Ryujinx.Graphics.Gal.OpenGL
{ {
abstract class OGLStreamBuffer : IDisposable class OGLStreamBuffer : IDisposable
{ {
public int Handle { get; protected set; } public int Handle { get; protected set; }
@ -11,53 +11,25 @@ namespace Ryujinx.Graphics.Gal.OpenGL
protected BufferTarget Target { get; private set; } protected BufferTarget Target { get; private set; }
private bool Mapped = false; public OGLStreamBuffer(BufferTarget Target, int Size)
public OGLStreamBuffer(BufferTarget Target, int MaxSize)
{ {
Handle = 0;
Mapped = false;
this.Target = Target; this.Target = Target;
this.Size = MaxSize; this.Size = Size;
Handle = GL.GenBuffer();
GL.BindBuffer(Target, Handle);
GL.BufferData(Target, Size, IntPtr.Zero, BufferUsageHint.StreamDraw);
} }
public static OGLStreamBuffer Create(BufferTarget Target, int MaxSize) public void SetData(int Size, IntPtr HostAddress)
{ {
//TODO: Query here for ARB_buffer_storage and use when available GL.BindBuffer(Target, Handle);
return new SubDataBuffer(Target, MaxSize);
GL.BufferSubData(Target, IntPtr.Zero, Size, HostAddress);
} }
public byte[] Map(int Size)
{
if (Handle == 0 || Mapped || Size > this.Size)
{
throw new InvalidOperationException();
}
byte[] Memory = InternMap(Size);
Mapped = true;
return Memory;
}
public void Unmap(int UsedSize)
{
if (Handle == 0 || !Mapped)
{
throw new InvalidOperationException();
}
InternUnmap(UsedSize);
Mapped = false;
}
protected abstract byte[] InternMap(int Size);
protected abstract void InternUnmap(int UsedSize);
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
@ -73,41 +45,4 @@ namespace Ryujinx.Graphics.Gal.OpenGL
} }
} }
} }
class SubDataBuffer : OGLStreamBuffer
{
private byte[] Memory;
public SubDataBuffer(BufferTarget Target, int MaxSize)
: base(Target, MaxSize)
{
Memory = new byte[MaxSize];
GL.GenBuffers(1, out int Handle);
GL.BindBuffer(Target, Handle);
GL.BufferData(Target, Size, IntPtr.Zero, BufferUsageHint.StreamDraw);
this.Handle = Handle;
}
protected override byte[] InternMap(int Size)
{
return Memory;
}
protected override void InternUnmap(int UsedSize)
{
GL.BindBuffer(Target, Handle);
unsafe
{
fixed (byte* MemoryPtr = Memory)
{
GL.BufferSubData(Target, IntPtr.Zero, UsedSize, (IntPtr)MemoryPtr);
}
}
}
}
} }

View file

@ -212,6 +212,8 @@ namespace Ryujinx.Graphics.Gal.OpenGL
{ {
switch (Format) switch (Format)
{ {
case GalTextureFormat.BC6H_UF16:
case GalTextureFormat.BC6H_SF16:
case GalTextureFormat.BC7U: case GalTextureFormat.BC7U:
case GalTextureFormat.BC1: case GalTextureFormat.BC1:
case GalTextureFormat.BC2: case GalTextureFormat.BC2:

View file

@ -4,13 +4,13 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
class GlslDecl class GlslDecl
{ {
public const int LayerAttr = 0x064;
public const int TessCoordAttrX = 0x2f0; public const int TessCoordAttrX = 0x2f0;
public const int TessCoordAttrY = 0x2f4; public const int TessCoordAttrY = 0x2f4;
public const int TessCoordAttrZ = 0x2f8; public const int TessCoordAttrZ = 0x2f8;
public const int InstanceIdAttr = 0x2f8; public const int InstanceIdAttr = 0x2f8;
public const int VertexIdAttr = 0x2fc; public const int VertexIdAttr = 0x2fc;
public const int FaceAttr = 0x3fc; public const int FaceAttr = 0x3fc;
public const int GlPositionWAttr = 0x7c;
public const int MaxUboSize = 1024; public const int MaxUboSize = 1024;
@ -210,7 +210,8 @@ namespace Ryujinx.Graphics.Gal.Shader
//This is a built-in input variable. //This is a built-in input variable.
if (Abuf.Offs == VertexIdAttr || if (Abuf.Offs == VertexIdAttr ||
Abuf.Offs == InstanceIdAttr || Abuf.Offs == InstanceIdAttr ||
Abuf.Offs == FaceAttr) Abuf.Offs == FaceAttr ||
Abuf.Offs == LayerAttr)
{ {
break; break;
} }
@ -254,6 +255,8 @@ namespace Ryujinx.Graphics.Gal.Shader
m_Attributes.Add(Index, DeclInfo); m_Attributes.Add(Index, DeclInfo);
} }
Traverse(Abuf, Abuf.Vertex);
break; break;
} }

View file

@ -21,10 +21,14 @@ namespace Ryujinx.Graphics.Gal.Shader
private const string IdentationStr = " "; private const string IdentationStr = " ";
private const int MaxVertexInput = 3;
private static string[] ElemTypes = new string[] { "float", "vec2", "vec3", "vec4" }; private static string[] ElemTypes = new string[] { "float", "vec2", "vec3", "vec4" };
private GlslDecl Decl; private GlslDecl Decl;
private ShaderHeader Header, HeaderB;
private ShaderIrBlock[] Blocks, BlocksB; private ShaderIrBlock[] Blocks, BlocksB;
private StringBuilder SB; private StringBuilder SB;
@ -50,6 +54,7 @@ namespace Ryujinx.Graphics.Gal.Shader
{ ShaderIrInst.Cle, GetCleExpr }, { ShaderIrInst.Cle, GetCleExpr },
{ ShaderIrInst.Clt, GetCltExpr }, { ShaderIrInst.Clt, GetCltExpr },
{ ShaderIrInst.Cne, GetCneExpr }, { ShaderIrInst.Cne, GetCneExpr },
{ ShaderIrInst.Cut, GetCutExpr },
{ ShaderIrInst.Exit, GetExitExpr }, { ShaderIrInst.Exit, GetExitExpr },
{ ShaderIrInst.Fabs, GetAbsExpr }, { ShaderIrInst.Fabs, GetAbsExpr },
{ ShaderIrInst.Fadd, GetAddExpr }, { ShaderIrInst.Fadd, GetAddExpr },
@ -110,6 +115,9 @@ namespace Ryujinx.Graphics.Gal.Shader
long VpBPosition, long VpBPosition,
GalShaderType ShaderType) GalShaderType ShaderType)
{ {
Header = new ShaderHeader(Memory, VpAPosition);
HeaderB = new ShaderHeader(Memory, VpBPosition);
Blocks = ShaderDecoder.Decode(Memory, VpAPosition); Blocks = ShaderDecoder.Decode(Memory, VpAPosition);
BlocksB = ShaderDecoder.Decode(Memory, VpBPosition); BlocksB = ShaderDecoder.Decode(Memory, VpBPosition);
@ -123,6 +131,9 @@ namespace Ryujinx.Graphics.Gal.Shader
public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType) public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType)
{ {
Header = new ShaderHeader(Memory, Position);
HeaderB = null;
Blocks = ShaderDecoder.Decode(Memory, Position); Blocks = ShaderDecoder.Decode(Memory, Position);
BlocksB = null; BlocksB = null;
@ -137,6 +148,7 @@ namespace Ryujinx.Graphics.Gal.Shader
SB.AppendLine("#version 410 core"); SB.AppendLine("#version 410 core");
PrintDeclHeader();
PrintDeclTextures(); PrintDeclTextures();
PrintDeclUniforms(); PrintDeclUniforms();
PrintDeclAttributes(); PrintDeclAttributes();
@ -170,6 +182,37 @@ namespace Ryujinx.Graphics.Gal.Shader
Decl.Uniforms.Values); Decl.Uniforms.Values);
} }
private void PrintDeclHeader()
{
if (Decl.ShaderType == GalShaderType.Geometry)
{
int MaxVertices = Header.MaxOutputVertexCount;
string OutputTopology;
switch (Header.OutputTopology)
{
case ShaderHeader.PointList: OutputTopology = "points"; break;
case ShaderHeader.LineStrip: OutputTopology = "line_strip"; break;
case ShaderHeader.TriangleStrip: OutputTopology = "triangle_strip"; break;
default: throw new InvalidOperationException();
}
SB.AppendLine("#extension GL_ARB_enhanced_layouts : require");
SB.AppendLine();
SB.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
SB.AppendLine("layout(triangles) in;" + Environment.NewLine);
SB.AppendLine($"layout({OutputTopology}, max_vertices = {MaxVertices}) out;");
SB.AppendLine();
}
}
private void PrintDeclTextures() private void PrintDeclTextures()
{ {
PrintDecls(Decl.Textures, "uniform sampler2D"); PrintDecls(Decl.Textures, "uniform sampler2D");
@ -201,7 +244,9 @@ namespace Ryujinx.Graphics.Gal.Shader
private void PrintDeclAttributes() private void PrintDeclAttributes()
{ {
PrintDecls(Decl.Attributes); string GeometryArray = (Decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
PrintDecls(Decl.Attributes, Suffix: GeometryArray);
} }
private void PrintDeclInAttributes() private void PrintDeclInAttributes()
@ -211,7 +256,27 @@ namespace Ryujinx.Graphics.Gal.Shader
SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";"); SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
} }
PrintDeclAttributes(Decl.InAttributes.Values, "in"); if (Decl.ShaderType == GalShaderType.Geometry)
{
if (Decl.InAttributes.Count > 0)
{
SB.AppendLine("in Vertex {");
foreach (ShaderDeclInfo DeclInfo in Decl.InAttributes.Values.OrderBy(DeclKeySelector))
{
if (DeclInfo.Index >= 0)
{
SB.AppendLine(IdentationStr + "layout (location = " + DeclInfo.Index + ") " + GetDecl(DeclInfo) + "; ");
}
}
SB.AppendLine("} block_in[];" + Environment.NewLine);
}
}
else
{
PrintDeclAttributes(Decl.InAttributes.Values, "in");
}
} }
private void PrintDeclOutAttributes() private void PrintDeclOutAttributes()
@ -254,7 +319,7 @@ namespace Ryujinx.Graphics.Gal.Shader
PrintDecls(Decl.Preds, "bool"); PrintDecls(Decl.Preds, "bool");
} }
private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null) private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null, string Suffix = "")
{ {
foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector)) foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
{ {
@ -262,15 +327,15 @@ namespace Ryujinx.Graphics.Gal.Shader
if (CustomType != null) if (CustomType != null)
{ {
Name = CustomType + " " + DeclInfo.Name + ";"; Name = CustomType + " " + DeclInfo.Name + Suffix + ";";
} }
else if (DeclInfo.Name == GlslDecl.FragmentOutputName) else if (DeclInfo.Name == GlslDecl.FragmentOutputName)
{ {
Name = "layout (location = 0) out " + GetDecl(DeclInfo) + ";" + Environment.NewLine; Name = "layout (location = 0) out " + GetDecl(DeclInfo) + Suffix + ";" + Environment.NewLine;
} }
else else
{ {
Name = GetDecl(DeclInfo) + ";"; Name = GetDecl(DeclInfo) + Suffix + ";";
} }
SB.AppendLine(Name); SB.AppendLine(Name);
@ -307,7 +372,21 @@ namespace Ryujinx.Graphics.Gal.Shader
string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1); string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1);
SB.AppendLine(IdentationStr + Attr.Name + Swizzle + " = " + DeclInfo.Name + ";"); if (Decl.ShaderType == GalShaderType.Geometry)
{
for (int Vertex = 0; Vertex < MaxVertexInput; Vertex++)
{
string Dst = Attr.Name + "[" + Vertex + "]" + Swizzle;
string Src = "block_in[" + Vertex + "]." + DeclInfo.Name;
SB.AppendLine(IdentationStr + Dst + " = " + Src + ";");
}
}
else
{
SB.AppendLine(IdentationStr + Attr.Name + Swizzle + " = " + DeclInfo.Name + ";");
}
} }
if (BlocksB != null) if (BlocksB != null)
@ -320,6 +399,16 @@ namespace Ryujinx.Graphics.Gal.Shader
SB.AppendLine(IdentationStr + GlslDecl.ProgramName + "();"); SB.AppendLine(IdentationStr + GlslDecl.ProgramName + "();");
} }
if (Decl.ShaderType != GalShaderType.Geometry)
{
PrintAttrToOutput();
}
SB.AppendLine("}");
}
private void PrintAttrToOutput(string Identation = IdentationStr)
{
foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes) foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes)
{ {
if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr)) if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
@ -331,21 +420,26 @@ namespace Ryujinx.Graphics.Gal.Shader
string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1); string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1);
SB.AppendLine(IdentationStr + DeclInfo.Name + " = " + Attr.Name + Swizzle + ";"); string Name = Attr.Name;
if (Decl.ShaderType == GalShaderType.Geometry)
{
Name += "[0]";
}
SB.AppendLine(Identation + DeclInfo.Name + " = " + Name + Swizzle + ";");
} }
if (Decl.ShaderType == GalShaderType.Vertex) if (Decl.ShaderType == GalShaderType.Vertex)
{ {
SB.AppendLine(IdentationStr + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";"); SB.AppendLine(Identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
} }
if (Decl.ShaderType != GalShaderType.Fragment) if (Decl.ShaderType != GalShaderType.Fragment)
{ {
SB.AppendLine(IdentationStr + GlslDecl.PositionOutAttrName + " = gl_Position;"); SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
SB.AppendLine(IdentationStr + GlslDecl.PositionOutAttrName + ".w = 1;"); SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + ".w = 1;");
} }
SB.AppendLine("}");
} }
private void PrintBlockScope( private void PrintBlockScope(
@ -484,11 +578,17 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
SB.AppendLine(Identation + "continue;"); SB.AppendLine(Identation + "continue;");
} }
continue;
} }
else if (Op.Inst == ShaderIrInst.Emit)
{
PrintAttrToOutput(Identation);
SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";"); SB.AppendLine(Identation + "EmitVertex();");
}
else
{
SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
}
} }
else if (Node is ShaderIrCmnt Cmnt) else if (Node is ShaderIrCmnt Cmnt)
{ {
@ -634,6 +734,14 @@ namespace Ryujinx.Graphics.Gal.Shader
private string GetOutAbufName(ShaderIrOperAbuf Abuf) private string GetOutAbufName(ShaderIrOperAbuf Abuf)
{ {
if (Decl.ShaderType == GalShaderType.Geometry)
{
switch (Abuf.Offs)
{
case GlslDecl.LayerAttr: return "gl_Layer";
}
}
return GetAttrTempName(Abuf); return GetAttrTempName(Abuf);
} }
@ -692,7 +800,16 @@ namespace Ryujinx.Graphics.Gal.Shader
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
return DeclInfo.Name + Swizzle; if (Decl.ShaderType == GalShaderType.Geometry)
{
string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
}
else
{
return DeclInfo.Name + Swizzle;
}
} }
private string GetName(ShaderIrOperGpr Gpr) private string GetName(ShaderIrOperGpr Gpr)
@ -805,6 +922,8 @@ namespace Ryujinx.Graphics.Gal.Shader
private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!="); private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!="); private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan"); private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
@ -1104,8 +1223,9 @@ namespace Ryujinx.Graphics.Gal.Shader
switch (Node) switch (Node)
{ {
case ShaderIrOperAbuf Abuf: case ShaderIrOperAbuf Abuf:
return Abuf.Offs == GlslDecl.VertexIdAttr || return Abuf.Offs == GlslDecl.LayerAttr ||
Abuf.Offs == GlslDecl.InstanceIdAttr || Abuf.Offs == GlslDecl.InstanceIdAttr ||
Abuf.Offs == GlslDecl.VertexIdAttr ||
Abuf.Offs == GlslDecl.FaceAttr Abuf.Offs == GlslDecl.FaceAttr
? OperType.I32 ? OperType.I32
: OperType.F32; : OperType.F32;

View file

@ -144,6 +144,50 @@ namespace Ryujinx.Graphics.Gal.Shader
EmitFsetp(Block, OpCode, ShaderOper.RR); EmitFsetp(Block, OpCode, ShaderOper.RR);
} }
public static void Iadd_C(ShaderIrBlock Block, long OpCode)
{
EmitIadd(Block, OpCode, ShaderOper.CR);
}
public static void Iadd_I(ShaderIrBlock Block, long OpCode)
{
EmitIadd(Block, OpCode, ShaderOper.Imm);
}
public static void Iadd_I32(ShaderIrBlock Block, long OpCode)
{
ShaderIrNode OperA = GetOperGpr8 (OpCode);
ShaderIrNode OperB = GetOperImm32_20(OpCode);
bool NegA = ((OpCode >> 56) & 1) != 0;
OperA = GetAluIneg(OperA, NegA);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB);
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode));
}
public static void Iadd_R(ShaderIrBlock Block, long OpCode)
{
EmitIadd(Block, OpCode, ShaderOper.RR);
}
public static void Iadd3_C(ShaderIrBlock Block, long OpCode)
{
EmitIadd3(Block, OpCode, ShaderOper.CR);
}
public static void Iadd3_I(ShaderIrBlock Block, long OpCode)
{
EmitIadd3(Block, OpCode, ShaderOper.Imm);
}
public static void Iadd3_R(ShaderIrBlock Block, long OpCode)
{
EmitIadd3(Block, OpCode, ShaderOper.RR);
}
public static void Imnmx_C(ShaderIrBlock Block, long OpCode) public static void Imnmx_C(ShaderIrBlock Block, long OpCode)
{ {
EmitImnmx(Block, OpCode, ShaderOper.CR); EmitImnmx(Block, OpCode, ShaderOper.CR);
@ -184,6 +228,21 @@ namespace Ryujinx.Graphics.Gal.Shader
EmitIscadd(Block, OpCode, ShaderOper.RR); EmitIscadd(Block, OpCode, ShaderOper.RR);
} }
public static void Iset_C(ShaderIrBlock Block, long OpCode)
{
EmitIset(Block, OpCode, ShaderOper.CR);
}
public static void Iset_I(ShaderIrBlock Block, long OpCode)
{
EmitIset(Block, OpCode, ShaderOper.Imm);
}
public static void Iset_R(ShaderIrBlock Block, long OpCode)
{
EmitIset(Block, OpCode, ShaderOper.RR);
}
public static void Isetp_C(ShaderIrBlock Block, long OpCode) public static void Isetp_C(ShaderIrBlock Block, long OpCode)
{ {
EmitIsetp(Block, OpCode, ShaderOper.CR); EmitIsetp(Block, OpCode, ShaderOper.CR);
@ -215,13 +274,13 @@ namespace Ryujinx.Graphics.Gal.Shader
case 2: Inst = ShaderIrInst.Xor; break; case 2: Inst = ShaderIrInst.Xor; break;
} }
ShaderIrNode OperA = GetAluNot(GetOperGpr8(OpCode), InvA); ShaderIrNode OperB = GetAluNot(GetOperImm32_20(OpCode), InvB);
//SubOp == 3 is pass, used by the not instruction //SubOp == 3 is pass, used by the not instruction
//which just moves the inverted register value. //which just moves the inverted register value.
if (SubOp < 3) if (SubOp < 3)
{ {
ShaderIrNode OperB = GetAluNot(GetOperImm32_20(OpCode), InvB); ShaderIrNode OperA = GetAluNot(GetOperGpr8(OpCode), InvA);
ShaderIrOp Op = new ShaderIrOp(Inst, OperA, OperB); ShaderIrOp Op = new ShaderIrOp(Inst, OperA, OperB);
@ -229,10 +288,25 @@ namespace Ryujinx.Graphics.Gal.Shader
} }
else else
{ {
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperB), OpCode));
} }
} }
public static void Lop_C(ShaderIrBlock Block, long OpCode)
{
EmitLop(Block, OpCode, ShaderOper.CR);
}
public static void Lop_I(ShaderIrBlock Block, long OpCode)
{
EmitLop(Block, OpCode, ShaderOper.Imm);
}
public static void Lop_R(ShaderIrBlock Block, long OpCode)
{
EmitLop(Block, OpCode, ShaderOper.RR);
}
public static void Mufu(ShaderIrBlock Block, long OpCode) public static void Mufu(ShaderIrBlock Block, long OpCode)
{ {
int SubOp = (int)(OpCode >> 20) & 0xf; int SubOp = (int)(OpCode >> 20) & 0xf;
@ -368,6 +442,41 @@ namespace Ryujinx.Graphics.Gal.Shader
return Signed ? ShaderIrInst.Asr : ShaderIrInst.Lsr; return Signed ? ShaderIrInst.Asr : ShaderIrInst.Lsr;
} }
public static void Vmad(ShaderIrBlock Block, long OpCode)
{
ShaderIrNode OperA = GetOperGpr8(OpCode);
ShaderIrNode OperB;
if (((OpCode >> 50) & 1) != 0)
{
OperB = GetOperGpr20(OpCode);
}
else
{
OperB = GetOperImm19_20(OpCode);
}
ShaderIrOperGpr OperC = GetOperGpr39(OpCode);
ShaderIrNode Tmp = new ShaderIrOp(ShaderIrInst.Mul, OperA, OperB);
ShaderIrNode Final = new ShaderIrOp(ShaderIrInst.Add, Tmp, OperC);
int Shr = (int)((OpCode >> 51) & 3);
if (Shr != 0)
{
int Shift = (Shr == 2) ? 15 : 7;
Final = new ShaderIrOp(ShaderIrInst.Lsr, Final, new ShaderIrOperImm(Shift));
}
Block.AddNode(new ShaderIrCmnt("Stubbed. Instruction is reduced to a * b + c"));
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Final), OpCode));
}
public static void Xmad_CR(ShaderIrBlock Block, long OpCode) public static void Xmad_CR(ShaderIrBlock Block, long OpCode)
{ {
EmitXmad(Block, OpCode, ShaderOper.CR); EmitXmad(Block, OpCode, ShaderOper.CR);
@ -533,6 +642,92 @@ namespace Ryujinx.Graphics.Gal.Shader
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode));
} }
private static void EmitIadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
ShaderIrNode OperA = GetOperGpr8(OpCode);
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break;
case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break;
case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break;
default: throw new ArgumentException(nameof(Oper));
}
bool NegA = ((OpCode >> 49) & 1) != 0;
bool NegB = ((OpCode >> 48) & 1) != 0;
OperA = GetAluIneg(OperA, NegA);
OperB = GetAluIneg(OperB, NegB);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB);
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode));
}
private static void EmitIadd3(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
int Mode = (int)((OpCode >> 37) & 3);
bool Neg1 = ((OpCode >> 51) & 1) != 0;
bool Neg2 = ((OpCode >> 50) & 1) != 0;
bool Neg3 = ((OpCode >> 49) & 1) != 0;
int Height1 = (int)((OpCode >> 35) & 3);
int Height2 = (int)((OpCode >> 33) & 3);
int Height3 = (int)((OpCode >> 31) & 3);
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break;
case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break;
case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrNode ApplyHeight(ShaderIrNode Src, int Height)
{
if (Oper != ShaderOper.RR)
{
return Src;
}
switch (Height)
{
case 0: return Src;
case 1: return new ShaderIrOp(ShaderIrInst.And, Src, new ShaderIrOperImm(0xffff));
case 2: return new ShaderIrOp(ShaderIrInst.Lsr, Src, new ShaderIrOperImm(16));
default: throw new InvalidOperationException();
}
}
ShaderIrNode Src1 = GetAluIneg(ApplyHeight(GetOperGpr8(OpCode), Height1), Neg1);
ShaderIrNode Src2 = GetAluIneg(ApplyHeight(OperB, Height2), Neg2);
ShaderIrNode Src3 = GetAluIneg(ApplyHeight(GetOperGpr39(OpCode), Height3), Neg3);
ShaderIrOp Sum = new ShaderIrOp(ShaderIrInst.Add, Src1, Src2);
if (Oper == ShaderOper.RR)
{
switch (Mode)
{
case 1: Sum = new ShaderIrOp(ShaderIrInst.Lsr, Sum, new ShaderIrOperImm(16)); break;
case 2: Sum = new ShaderIrOp(ShaderIrInst.Lsl, Sum, new ShaderIrOperImm(16)); break;
}
}
//Note: Here there should be a "+ 1" when carry flag is set
//but since carry is mostly ignored by other instructions, it's excluded for now
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), new ShaderIrOp(ShaderIrInst.Add, Sum, Src3)), OpCode));
}
private static void EmitIscadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper) private static void EmitIscadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{ {
bool NegB = ((OpCode >> 48) & 1) != 0; bool NegB = ((OpCode >> 48) & 1) != 0;
@ -659,6 +854,8 @@ namespace Ryujinx.Graphics.Gal.Shader
OperA = GetAluFabsFneg(OperA, AbsA, NegA); OperA = GetAluFabsFneg(OperA, AbsA, NegA);
Block.AddNode(new ShaderIrCmnt("Stubbed."));
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode));
} }
@ -821,6 +1018,54 @@ namespace Ryujinx.Graphics.Gal.Shader
Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode));
} }
private static void EmitLop(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
int SubOp = (int)(OpCode >> 41) & 3;
bool InvA = ((OpCode >> 39) & 1) != 0;
bool InvB = ((OpCode >> 40) & 1) != 0;
ShaderIrInst Inst = 0;
switch (SubOp)
{
case 0: Inst = ShaderIrInst.And; break;
case 1: Inst = ShaderIrInst.Or; break;
case 2: Inst = ShaderIrInst.Xor; break;
}
ShaderIrNode OperA = GetAluNot(GetOperGpr8(OpCode), InvA);
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break;
case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break;
case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break;
default: throw new ArgumentException(nameof(Oper));
}
OperB = GetAluNot(OperB, InvB);
ShaderIrNode Op;
if (SubOp < 3)
{
Op = new ShaderIrOp(Inst, OperA, OperB);
}
else
{
Op = OperB;
}
ShaderIrNode Compare = new ShaderIrOp(ShaderIrInst.Cne, Op, new ShaderIrOperImm(0));
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperPred48(OpCode), Compare), OpCode));
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode));
}
private static void EmitXmad(ShaderIrBlock Block, long OpCode, ShaderOper Oper) private static void EmitXmad(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{ {
//TODO: Confirm SignAB/C, it is just a guess. //TODO: Confirm SignAB/C, it is just a guess.

View file

@ -7,14 +7,15 @@ namespace Ryujinx.Graphics.Gal.Shader
public static ShaderIrOperAbuf[] GetOperAbuf20(long OpCode) public static ShaderIrOperAbuf[] GetOperAbuf20(long OpCode)
{ {
int Abuf = (int)(OpCode >> 20) & 0x3ff; int Abuf = (int)(OpCode >> 20) & 0x3ff;
int Reg = (int)(OpCode >> 39) & 0xff;
int Size = (int)(OpCode >> 47) & 3; int Size = (int)(OpCode >> 47) & 3;
ShaderIrOperGpr Vertex = GetOperGpr39(OpCode);
ShaderIrOperAbuf[] Opers = new ShaderIrOperAbuf[Size + 1]; ShaderIrOperAbuf[] Opers = new ShaderIrOperAbuf[Size + 1];
for (int Index = 0; Index <= Size; Index++) for (int Index = 0; Index <= Size; Index++)
{ {
Opers[Index] = new ShaderIrOperAbuf(Abuf + Index * 4, Reg); Opers[Index] = new ShaderIrOperAbuf(Abuf + Index * 4, Vertex);
} }
return Opers; return Opers;
@ -23,9 +24,8 @@ namespace Ryujinx.Graphics.Gal.Shader
public static ShaderIrOperAbuf GetOperAbuf28(long OpCode) public static ShaderIrOperAbuf GetOperAbuf28(long OpCode)
{ {
int Abuf = (int)(OpCode >> 28) & 0x3ff; int Abuf = (int)(OpCode >> 28) & 0x3ff;
int Reg = (int)(OpCode >> 39) & 0xff;
return new ShaderIrOperAbuf(Abuf, Reg); return new ShaderIrOperAbuf(Abuf, GetOperGpr39(OpCode));
} }
public static ShaderIrOperCbuf GetOperCbuf34(long OpCode) public static ShaderIrOperCbuf GetOperCbuf34(long OpCode)
@ -156,6 +156,11 @@ namespace Ryujinx.Graphics.Gal.Shader
return new ShaderIrOperPred((int)(OpCode >> 39) & 7); return new ShaderIrOperPred((int)(OpCode >> 39) & 7);
} }
public static ShaderIrOperPred GetOperPred48(long OpCode)
{
return new ShaderIrOperPred((int)((OpCode >> 48) & 7));
}
public static ShaderIrInst GetCmp(long OpCode) public static ShaderIrInst GetCmp(long OpCode)
{ {
switch ((int)(OpCode >> 49) & 7) switch ((int)(OpCode >> 49) & 7)

View file

@ -35,6 +35,9 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
ShaderIrNode[] Opers = GetOperAbuf20(OpCode); ShaderIrNode[] Opers = GetOperAbuf20(OpCode);
//Used by GS
ShaderIrOperGpr Vertex = GetOperGpr39(OpCode);
int Index = 0; int Index = 0;
foreach (ShaderIrNode OperA in Opers) foreach (ShaderIrNode OperA in Opers)

View file

@ -85,6 +85,16 @@ namespace Ryujinx.Graphics.Gal.Shader
EmitI2i(Block, OpCode, ShaderOper.RR); EmitI2i(Block, OpCode, ShaderOper.RR);
} }
public static void Isberd(ShaderIrBlock Block, long OpCode)
{
//This instruction seems to be used to translate from an address to a vertex index in a GS
//Stub it as such
Block.AddNode(new ShaderIrCmnt("Stubbed."));
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), GetOperGpr8(OpCode)), OpCode));
}
public static void Mov_C(ShaderIrBlock Block, long OpCode) public static void Mov_C(ShaderIrBlock Block, long OpCode)
{ {
ShaderIrOperCbuf Cbuf = GetOperCbuf34(OpCode); ShaderIrOperCbuf Cbuf = GetOperCbuf34(OpCode);
@ -113,6 +123,31 @@ namespace Ryujinx.Graphics.Gal.Shader
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Gpr), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Gpr), OpCode));
} }
public static void Sel_C(ShaderIrBlock Block, long OpCode)
{
EmitSel(Block, OpCode, ShaderOper.CR);
}
public static void Sel_I(ShaderIrBlock Block, long OpCode)
{
EmitSel(Block, OpCode, ShaderOper.Imm);
}
public static void Sel_R(ShaderIrBlock Block, long OpCode)
{
EmitSel(Block, OpCode, ShaderOper.RR);
}
public static void Mov_S(ShaderIrBlock Block, long OpCode)
{
Block.AddNode(new ShaderIrCmnt("Stubbed."));
//Zero is used as a special number to get a valid "0 * 0 + VertexIndex" in a GS
ShaderIrNode Source = new ShaderIrOperImm(0);
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Source), OpCode));
}
private static void EmitF2f(ShaderIrBlock Block, long OpCode, ShaderOper Oper) private static void EmitF2f(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{ {
bool NegA = ((OpCode >> 45) & 1) != 0; bool NegA = ((OpCode >> 45) & 1) != 0;
@ -340,6 +375,28 @@ namespace Ryujinx.Graphics.Gal.Shader
Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode));
} }
private static void EmitSel(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
ShaderIrOperGpr Dst = GetOperGpr0 (OpCode);
ShaderIrNode Pred = GetOperPred39N(OpCode);
ShaderIrNode ResultA = GetOperGpr8(OpCode);
ShaderIrNode ResultB;
switch (Oper)
{
case ShaderOper.CR: ResultB = GetOperCbuf34 (OpCode); break;
case ShaderOper.Imm: ResultB = GetOperImm19_20(OpCode); break;
case ShaderOper.RR: ResultB = GetOperGpr20 (OpCode); break;
default: throw new ArgumentException(nameof(Oper));
}
Block.AddNode(GetPredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultA), false), OpCode));
Block.AddNode(GetPredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultB), true), OpCode));
}
private static IntType GetIntType(long OpCode) private static IntType GetIntType(long OpCode)
{ {
bool Signed = ((OpCode >> 13) & 1) != 0; bool Signed = ((OpCode >> 13) & 1) != 0;

View file

@ -0,0 +1,29 @@
using System;
using static Ryujinx.Graphics.Gal.Shader.ShaderDecodeHelper;
namespace Ryujinx.Graphics.Gal.Shader
{
static partial class ShaderDecode
{
public static void Out_R(ShaderIrBlock Block, long OpCode)
{
//TODO: Those registers have to be used for something
ShaderIrOperGpr Gpr0 = GetOperGpr0(OpCode);
ShaderIrOperGpr Gpr8 = GetOperGpr8(OpCode);
ShaderIrOperGpr Gpr20 = GetOperGpr20(OpCode);
int Type = (int)((OpCode >> 39) & 3);
if ((Type & 1) != 0)
{
Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Emit), OpCode));
}
if ((Type & 2) != 0)
{
Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Cut), OpCode));
}
}
}
}

View file

@ -4,6 +4,8 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
static class ShaderDecoder static class ShaderDecoder
{ {
private const long HeaderSize = 0x50;
private const bool AddDbgComments = true; private const bool AddDbgComments = true;
public static ShaderIrBlock[] Decode(IGalMemory Memory, long Start) public static ShaderIrBlock[] Decode(IGalMemory Memory, long Start)
@ -32,13 +34,13 @@ namespace Ryujinx.Graphics.Gal.Shader
return Output; return Output;
} }
ShaderIrBlock Entry = Enqueue(Start); ShaderIrBlock Entry = Enqueue(Start + HeaderSize);
while (Blocks.Count > 0) while (Blocks.Count > 0)
{ {
ShaderIrBlock Current = Blocks.Dequeue(); ShaderIrBlock Current = Blocks.Dequeue();
FillBlock(Memory, Current); FillBlock(Memory, Current, Start + HeaderSize);
//Set child blocks. "Branch" is the block the branch instruction //Set child blocks. "Branch" is the block the branch instruction
//points to (when taken), "Next" is the block at the next address, //points to (when taken), "Next" is the block at the next address,
@ -122,14 +124,14 @@ namespace Ryujinx.Graphics.Gal.Shader
return Graph; return Graph;
} }
private static void FillBlock(IGalMemory Memory, ShaderIrBlock Block) private static void FillBlock(IGalMemory Memory, ShaderIrBlock Block, long Beginning)
{ {
long Position = Block.Position; long Position = Block.Position;
do do
{ {
//Ignore scheduling instructions, which are written every 32 bytes. //Ignore scheduling instructions, which are written every 32 bytes.
if ((Position & 0x1f) == 0) if (((Position - Beginning) & 0x1f) == 0)
{ {
Position += 8; Position += 8;
@ -147,7 +149,7 @@ namespace Ryujinx.Graphics.Gal.Shader
if (AddDbgComments) if (AddDbgComments)
{ {
string DbgOpCode = $"0x{(Position - 8):x16}: 0x{OpCode:x16} "; string DbgOpCode = $"0x{(Position - Beginning - 8):x16}: 0x{OpCode:x16} ";
DbgOpCode += (Decode?.Method.Name ?? "???"); DbgOpCode += (Decode?.Method.Name ?? "???");

View file

@ -0,0 +1,73 @@
namespace Ryujinx.Graphics.Gal.Shader
{
class ShaderHeader
{
public const int PointList = 1;
public const int LineStrip = 6;
public const int TriangleStrip = 7;
public int SphType { get; private set; }
public int Version { get; private set; }
public int ShaderType { get; private set; }
public bool MrtEnable { get; private set; }
public bool KillsPixels { get; private set; }
public bool DoesGlobalStore { get; private set; }
public int SassVersion { get; private set; }
public bool DoesLoadOrStore { get; private set; }
public bool DoesFp64 { get; private set; }
public int StreamOutMask { get; private set; }
public int ShaderLocalMemoryLowSize { get; private set; }
public int PerPatchAttributeCount { get; private set; }
public int ShaderLocalMemoryHighSize { get; private set; }
public int ThreadsPerInputPrimitive { get; private set; }
public int ShaderLocalMemoryCrsSize { get; private set; }
public int OutputTopology { get; private set; }
public int MaxOutputVertexCount { get; private set; }
public int StoreReqStart { get; private set; }
public int StoreReqEnd { get; private set; }
public ShaderHeader(IGalMemory Memory, long Position)
{
uint CommonWord0 = (uint)Memory.ReadInt32(Position + 0);
uint CommonWord1 = (uint)Memory.ReadInt32(Position + 4);
uint CommonWord2 = (uint)Memory.ReadInt32(Position + 8);
uint CommonWord3 = (uint)Memory.ReadInt32(Position + 12);
uint CommonWord4 = (uint)Memory.ReadInt32(Position + 16);
SphType = ReadBits(CommonWord0, 0, 5);
Version = ReadBits(CommonWord0, 5, 5);
ShaderType = ReadBits(CommonWord0, 10, 4);
MrtEnable = ReadBits(CommonWord0, 14, 1) != 0;
KillsPixels = ReadBits(CommonWord0, 15, 1) != 0;
DoesGlobalStore = ReadBits(CommonWord0, 16, 1) != 0;
SassVersion = ReadBits(CommonWord0, 17, 4);
DoesLoadOrStore = ReadBits(CommonWord0, 26, 1) != 0;
DoesFp64 = ReadBits(CommonWord0, 27, 1) != 0;
StreamOutMask = ReadBits(CommonWord0, 28, 4);
ShaderLocalMemoryLowSize = ReadBits(CommonWord1, 0, 24);
PerPatchAttributeCount = ReadBits(CommonWord1, 24, 8);
ShaderLocalMemoryHighSize = ReadBits(CommonWord2, 0, 24);
ThreadsPerInputPrimitive = ReadBits(CommonWord2, 24, 8);
ShaderLocalMemoryCrsSize = ReadBits(CommonWord3, 0, 24);
OutputTopology = ReadBits(CommonWord3, 24, 4);
MaxOutputVertexCount = ReadBits(CommonWord4, 0, 12);
StoreReqStart = ReadBits(CommonWord4, 12, 8);
StoreReqEnd = ReadBits(CommonWord4, 24, 8);
}
private static int ReadBits(uint Word, int Offset, int BitWidth)
{
uint Mask = (1u << BitWidth) - 1u;
return (int)((Word >> Offset) & Mask);
}
}
}

View file

@ -82,6 +82,9 @@ namespace Ryujinx.Graphics.Gal.Shader
Bra, Bra,
Exit, Exit,
Kil Kil,
Emit,
Cut
} }
} }

View file

@ -2,13 +2,14 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
class ShaderIrOperAbuf : ShaderIrNode class ShaderIrOperAbuf : ShaderIrNode
{ {
public int Offs { get; private set; } public int Offs { get; private set; }
public int GprIndex { get; private set; }
public ShaderIrOperAbuf(int Offs, int GprIndex) public ShaderIrNode Vertex { get; private set; }
public ShaderIrOperAbuf(int Offs, ShaderIrNode Vertex)
{ {
this.Offs = Offs; this.Offs = Offs;
this.GprIndex = GprIndex; this.Vertex = Vertex;
} }
} }
} }

View file

@ -64,29 +64,48 @@ namespace Ryujinx.Graphics.Gal.Shader
Set("0100110011100x", ShaderDecode.I2i_C); Set("0100110011100x", ShaderDecode.I2i_C);
Set("0011100x11100x", ShaderDecode.I2i_I); Set("0011100x11100x", ShaderDecode.I2i_I);
Set("0101110011100x", ShaderDecode.I2i_R); Set("0101110011100x", ShaderDecode.I2i_R);
Set("0100110000010x", ShaderDecode.Iadd_C);
Set("0011100000010x", ShaderDecode.Iadd_I);
Set("0001110x0xxxxx", ShaderDecode.Iadd_I32);
Set("0101110000010x", ShaderDecode.Iadd_R);
Set("010011001100xx", ShaderDecode.Iadd3_C);
Set("001110001100xx", ShaderDecode.Iadd3_I);
Set("010111001100xx", ShaderDecode.Iadd3_R);
Set("0100110000100x", ShaderDecode.Imnmx_C); Set("0100110000100x", ShaderDecode.Imnmx_C);
Set("0011100x00100x", ShaderDecode.Imnmx_I); Set("0011100x00100x", ShaderDecode.Imnmx_I);
Set("0101110000100x", ShaderDecode.Imnmx_R); Set("0101110000100x", ShaderDecode.Imnmx_R);
Set("1110111111010x", ShaderDecode.Isberd);
Set("11100000xxxxxx", ShaderDecode.Ipa); Set("11100000xxxxxx", ShaderDecode.Ipa);
Set("0100110000011x", ShaderDecode.Iscadd_C); Set("0100110000011x", ShaderDecode.Iscadd_C);
Set("0011100x00011x", ShaderDecode.Iscadd_I); Set("0011100x00011x", ShaderDecode.Iscadd_I);
Set("0101110000011x", ShaderDecode.Iscadd_R); Set("0101110000011x", ShaderDecode.Iscadd_R);
Set("010010110101xx", ShaderDecode.Iset_C);
Set("001101100101xx", ShaderDecode.Iset_I);
Set("010110110101xx", ShaderDecode.Iset_R);
Set("010010110110xx", ShaderDecode.Isetp_C); Set("010010110110xx", ShaderDecode.Isetp_C);
Set("0011011x0110xx", ShaderDecode.Isetp_I); Set("0011011x0110xx", ShaderDecode.Isetp_I);
Set("010110110110xx", ShaderDecode.Isetp_R); Set("010110110110xx", ShaderDecode.Isetp_R);
Set("111000110011xx", ShaderDecode.Kil); Set("111000110011xx", ShaderDecode.Kil);
Set("1110111111011x", ShaderDecode.Ld_A); Set("1110111111011x", ShaderDecode.Ld_A);
Set("1110111110010x", ShaderDecode.Ld_C); Set("1110111110010x", ShaderDecode.Ld_C);
Set("0100110001000x", ShaderDecode.Lop_C);
Set("0011100001000x", ShaderDecode.Lop_I);
Set("000001xxxxxxxx", ShaderDecode.Lop_I32); Set("000001xxxxxxxx", ShaderDecode.Lop_I32);
Set("0101110001000x", ShaderDecode.Lop_R);
Set("0100110010011x", ShaderDecode.Mov_C); Set("0100110010011x", ShaderDecode.Mov_C);
Set("0011100x10011x", ShaderDecode.Mov_I); Set("0011100x10011x", ShaderDecode.Mov_I);
Set("000000010000xx", ShaderDecode.Mov_I32); Set("000000010000xx", ShaderDecode.Mov_I32);
Set("0101110010011x", ShaderDecode.Mov_R); Set("0101110010011x", ShaderDecode.Mov_R);
Set("1111000011001x", ShaderDecode.Mov_S);
Set("0101000010000x", ShaderDecode.Mufu); Set("0101000010000x", ShaderDecode.Mufu);
Set("1111101111100x", ShaderDecode.Out_R);
Set("0101000010010x", ShaderDecode.Psetp); Set("0101000010010x", ShaderDecode.Psetp);
Set("0100110010010x", ShaderDecode.Rro_C); Set("0100110010010x", ShaderDecode.Rro_C);
Set("0011100x10010x", ShaderDecode.Rro_I); Set("0011100x10010x", ShaderDecode.Rro_I);
Set("0101110010010x", ShaderDecode.Rro_R); Set("0101110010010x", ShaderDecode.Rro_R);
Set("0100110010100x", ShaderDecode.Sel_C);
Set("0011100010100x", ShaderDecode.Sel_I);
Set("0101110010100x", ShaderDecode.Sel_R);
Set("0100110001001x", ShaderDecode.Shl_C); Set("0100110001001x", ShaderDecode.Shl_C);
Set("0011100x01001x", ShaderDecode.Shl_I); Set("0011100x01001x", ShaderDecode.Shl_I);
Set("0101110001001x", ShaderDecode.Shl_R); Set("0101110001001x", ShaderDecode.Shl_R);
@ -98,6 +117,7 @@ namespace Ryujinx.Graphics.Gal.Shader
Set("1101111101001x", ShaderDecode.Texq); Set("1101111101001x", ShaderDecode.Texq);
Set("1101100xxxxxxx", ShaderDecode.Texs); Set("1101100xxxxxxx", ShaderDecode.Texs);
Set("1101101xxxxxxx", ShaderDecode.Tlds); Set("1101101xxxxxxx", ShaderDecode.Tlds);
Set("01011111xxxxxx", ShaderDecode.Vmad);
Set("0100111xxxxxxx", ShaderDecode.Xmad_CR); Set("0100111xxxxxxx", ShaderDecode.Xmad_CR);
Set("0011011x00xxxx", ShaderDecode.Xmad_I); Set("0011011x00xxxx", ShaderDecode.Xmad_I);
Set("010100010xxxxx", ShaderDecode.Xmad_RC); Set("010100010xxxxx", ShaderDecode.Xmad_RC);

View file

@ -0,0 +1,126 @@
using System;
using System.IO;
namespace Ryujinx.Graphics.Gal
{
static class ShaderDumper
{
private static string RuntimeDir;
private static int DumpIndex = 1;
public static void Dump(IGalMemory Memory, long Position, GalShaderType Type, string ExtSuffix = "")
{
if (string.IsNullOrWhiteSpace(GraphicsConfig.ShadersDumpPath))
{
return;
}
string FileName = "Shader" + DumpIndex.ToString("d4") + "." + ShaderExtension(Type) + ExtSuffix + ".bin";
string FullPath = Path.Combine(FullDir(), FileName);
string CodePath = Path.Combine(CodeDir(), FileName);
DumpIndex++;
using (FileStream FullFile = File.Create(FullPath))
using (FileStream CodeFile = File.Create(CodePath))
using (BinaryWriter FullWriter = new BinaryWriter(FullFile))
using (BinaryWriter CodeWriter = new BinaryWriter(CodeFile))
{
for (long i = 0; i < 0x50; i += 4)
{
FullWriter.Write(Memory.ReadInt32(Position + i));
}
long Offset = 0;
ulong Instruction = 0;
//Dump until a NOP instruction is found
while ((Instruction >> 52 & 0xfff8) != 0x50b0)
{
uint Word0 = (uint)Memory.ReadInt32(Position + 0x50 + Offset + 0);
uint Word1 = (uint)Memory.ReadInt32(Position + 0x50 + Offset + 4);
Instruction = Word0 | (ulong)Word1 << 32;
//Zero instructions (other kind of NOP) stop immediatly,
//this is to avoid two rows of zeroes
if (Instruction == 0)
{
break;
}
FullWriter.Write(Instruction);
CodeWriter.Write(Instruction);
Offset += 8;
}
//Align to meet nvdisasm requeriments
while (Offset % 0x20 != 0)
{
FullWriter.Write(0);
CodeWriter.Write(0);
Offset += 4;
}
}
}
private static string FullDir()
{
return CreateAndReturn(Path.Combine(DumpDir(), "Full"));
}
private static string CodeDir()
{
return CreateAndReturn(Path.Combine(DumpDir(), "Code"));
}
private static string DumpDir()
{
if (string.IsNullOrEmpty(RuntimeDir))
{
int Index = 1;
do
{
RuntimeDir = Path.Combine(GraphicsConfig.ShadersDumpPath, "Dumps" + Index.ToString("d2"));
Index++;
}
while (Directory.Exists(RuntimeDir));
Directory.CreateDirectory(RuntimeDir);
}
return RuntimeDir;
}
private static string CreateAndReturn(string Dir)
{
if (!Directory.Exists(Dir))
{
Directory.CreateDirectory(Dir);
}
return Dir;
}
private static string ShaderExtension(GalShaderType Type)
{
switch (Type)
{
case GalShaderType.Vertex: return "vert";
case GalShaderType.TessControl: return "tesc";
case GalShaderType.TessEvaluation: return "tese";
case GalShaderType.Geometry: return "geom";
case GalShaderType.Fragment: return "frag";
default: throw new ArgumentException(nameof(Type));
}
}
}
}

View file

@ -0,0 +1,4 @@
public static class GraphicsConfig
{
public static string ShadersDumpPath;
}

View file

@ -21,13 +21,4 @@
<ProjectReference Include="..\ChocolArm64\ChocolArm64.csproj" /> <ProjectReference Include="..\ChocolArm64\ChocolArm64.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Gal\OpenGL\FbVtxShader.glsl">
<LogicalName>GlFbVtxShader</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Gal\OpenGL\FbFragShader.glsl">
<LogicalName>GlFbFragShader</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Project> </Project>

View file

@ -1,6 +1,7 @@
using Ryujinx.Graphics.Gal; using Ryujinx.Graphics.Gal;
using Ryujinx.HLE.Gpu.Memory; using Ryujinx.HLE.Gpu.Memory;
using Ryujinx.HLE.Gpu.Texture; using Ryujinx.HLE.Gpu.Texture;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Ryujinx.HLE.Gpu.Engines namespace Ryujinx.HLE.Gpu.Engines
@ -64,6 +65,8 @@ namespace Ryujinx.HLE.Gpu.Engines
bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0;
int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth);
int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight);
int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch);
int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions);
bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0;
int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth);
@ -71,75 +74,114 @@ namespace Ryujinx.HLE.Gpu.Engines
int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch);
int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions);
TextureSwizzle SrcSwizzle = SrcLinear
? TextureSwizzle.Pitch
: TextureSwizzle.BlockLinear;
TextureSwizzle DstSwizzle = DstLinear TextureSwizzle DstSwizzle = DstLinear
? TextureSwizzle.Pitch ? TextureSwizzle.Pitch
: TextureSwizzle.BlockLinear; : TextureSwizzle.BlockLinear;
int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf);
int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
long Key = Vmm.GetPhysicalAddress(MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress));
long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress);
long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress);
bool IsFbTexture = Gpu.Engine3d.IsFrameBufferPosition(Key); long SrcKey = Vmm.GetPhysicalAddress(SrcAddress);
long DstKey = Vmm.GetPhysicalAddress(DstAddress);
if (IsFbTexture && DstLinear) bool IsSrcFb = Gpu.Engine3d.IsFrameBufferPosition(SrcKey);
bool IsDstFb = Gpu.Engine3d.IsFrameBufferPosition(DstKey);
TextureInfo SrcTexture()
{ {
DstSwizzle = TextureSwizzle.BlockLinear; return new TextureInfo(
SrcAddress,
SrcWidth,
SrcHeight,
SrcPitch,
SrcBlockHeight, 1,
SrcSwizzle,
GalTextureFormat.A8B8G8R8);
} }
TextureInfo DstTexture = new TextureInfo( TextureInfo DstTexture()
DstAddress,
DstWidth,
DstHeight,
DstBlockHeight,
DstBlockHeight,
DstSwizzle,
GalTextureFormat.A8B8G8R8);
if (IsFbTexture)
{ {
//TODO: Change this when the correct frame buffer resolution is used. return new TextureInfo(
//Currently, the frame buffer size is hardcoded to 1280x720. DstAddress,
SrcWidth = 1280; DstWidth,
SrcHeight = 720; DstHeight,
DstPitch,
DstBlockHeight, 1,
DstSwizzle,
GalTextureFormat.A8B8G8R8);
}
Gpu.Renderer.FrameBuffer.GetBufferData(Key, (byte[] Buffer) => //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.FrameBuffer.Copy(
SrcKey,
DstKey,
0,
0,
SrcWidth,
SrcHeight,
0,
0,
DstWidth,
DstHeight);
}
if (IsSrcFb)
{
//Frame Buffer -> Texture copy.
Gpu.Renderer.FrameBuffer.GetBufferData(SrcKey, (byte[] Buffer) =>
{ {
CopyTexture( TextureInfo Src = SrcTexture();
Vmm, TextureInfo Dst = DstTexture();
DstTexture,
Buffer, if (Src.Width != Dst.Width ||
SrcWidth, Src.Height != Dst.Height)
SrcHeight); {
throw new NotImplementedException("Texture resizing is not supported");
}
TextureWriter.Write(Vmm, Dst, Buffer);
}); });
} }
else if (IsDstFb)
{
//Texture -> Frame Buffer copy.
const GalTextureFormat Format = GalTextureFormat.A8B8G8R8;
byte[] Buffer = TextureReader.Read(Vmm, SrcTexture());
Gpu.Renderer.FrameBuffer.SetBufferData(
DstKey,
DstWidth,
DstHeight,
Format,
Buffer);
}
else else
{ {
long Size = SrcWidth * SrcHeight * 4; //Texture -> Texture copy.
TextureInfo Src = SrcTexture();
TextureInfo Dst = DstTexture();
byte[] Buffer = Vmm.ReadBytes(SrcAddress, Size); if (Src.Width != Dst.Width ||
Src.Height != Dst.Height)
{
throw new NotImplementedException("Texture resizing is not supported");
}
CopyTexture( TextureWriter.Write(Vmm, Dst, TextureReader.Read(Vmm, Src));
Vmm,
DstTexture,
Buffer,
SrcWidth,
SrcHeight);
} }
} }
private void CopyTexture(
NvGpuVmm Vmm,
TextureInfo Texture,
byte[] Buffer,
int Width,
int Height)
{
TextureWriter.Write(Vmm, Texture, Buffer, Width, Height);
}
private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg)
{ {
return return

View file

@ -132,10 +132,22 @@ namespace Ryujinx.HLE.Gpu.Engines
int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
//Note: Using the Width/Height results seems to give incorrect results. float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 4);
//Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely. float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 4);
Gpu.Renderer.FrameBuffer.Create(Key, 1280, 720);
float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 4);
float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 4);
int VpX = (int)MathF.Max(0, TX - MathF.Abs(SX));
int VpY = (int)MathF.Max(0, TY - MathF.Abs(SY));
int VpW = (int)(TX + MathF.Abs(SX)) - VpX;
int VpH = (int)(TY + MathF.Abs(SY)) - VpY;
Gpu.Renderer.FrameBuffer.Create(Key, Width, Height);
Gpu.Renderer.FrameBuffer.Bind(Key); Gpu.Renderer.FrameBuffer.Bind(Key);
Gpu.Renderer.FrameBuffer.SetViewport(VpX, VpY, VpW, VpH);
} }
private long[] UploadShaders(NvGpuVmm Vmm) private long[] UploadShaders(NvGpuVmm Vmm)
@ -195,8 +207,8 @@ namespace Ryujinx.HLE.Gpu.Engines
Gpu.Renderer.Shader.Bind(Key); Gpu.Renderer.Shader.Bind(Key);
} }
float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportScaleX); float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportScaleY); float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
Gpu.Renderer.Shader.SetFlip(SignX, SignY); Gpu.Renderer.Shader.SetFlip(SignX, SignY);
@ -220,8 +232,8 @@ namespace Ryujinx.HLE.Gpu.Engines
private void SetFrontFace() private void SetFrontFace()
{ {
float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportScaleX); float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportScaleY); float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace); GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace);
@ -548,9 +560,9 @@ namespace Ryujinx.HLE.Gpu.Engines
if (Cb.Enabled) if (Cb.Enabled)
{ {
byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size); IntPtr DataAddress = Vmm.GetHostAddress(Cb.Position, Cb.Size);
Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data); Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Cb.Size, DataAddress);
} }
} }
} }
@ -583,9 +595,9 @@ namespace Ryujinx.HLE.Gpu.Engines
if (!IboCached || Vmm.IsRegionModified(IboKey, (uint)IbSize, NvGpuBufferType.Index)) if (!IboCached || Vmm.IsRegionModified(IboKey, (uint)IbSize, NvGpuBufferType.Index))
{ {
byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize); IntPtr DataAddress = Vmm.GetHostAddress(IndexPosition, IbSize);
Gpu.Renderer.Rasterizer.CreateIbo(IboKey, Data); Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, DataAddress);
} }
Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat); Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat);
@ -647,9 +659,9 @@ namespace Ryujinx.HLE.Gpu.Engines
if (!VboCached || Vmm.IsRegionModified(VboKey, VbSize, NvGpuBufferType.Vertex)) if (!VboCached || Vmm.IsRegionModified(VboKey, VbSize, NvGpuBufferType.Vertex))
{ {
byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize); IntPtr DataAddress = Vmm.GetHostAddress(VertexPosition, VbSize);
Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Data); Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, DataAddress);
} }
Gpu.Renderer.Rasterizer.SetVertexArray(Stride, VboKey, Attribs[Index].ToArray()); Gpu.Renderer.Rasterizer.SetVertexArray(Stride, VboKey, Attribs[Index].ToArray());

View file

@ -6,12 +6,14 @@ namespace Ryujinx.HLE.Gpu.Engines
FrameBufferNWidth = 0x202, FrameBufferNWidth = 0x202,
FrameBufferNHeight = 0x203, FrameBufferNHeight = 0x203,
FrameBufferNFormat = 0x204, FrameBufferNFormat = 0x204,
ViewportScaleX = 0x280, ViewportNScaleX = 0x280,
ViewportScaleY = 0x281, ViewportNScaleY = 0x281,
ViewportScaleZ = 0x282, ViewportNScaleZ = 0x282,
ViewportTranslateX = 0x283, ViewportNTranslateX = 0x283,
ViewportTranslateY = 0x284, ViewportNTranslateY = 0x284,
ViewportTranslateZ = 0x285, ViewportNTranslateZ = 0x285,
ViewportNHoriz = 0x300,
ViewportNVert = 0x301,
VertexArrayFirst = 0x35d, VertexArrayFirst = 0x35d,
VertexArrayCount = 0x35e, VertexArrayCount = 0x35e,
ClearDepth = 0x364, ClearDepth = 0x364,

View file

@ -1,5 +1,6 @@
using ChocolArm64.Memory; using ChocolArm64.Memory;
using Ryujinx.Graphics.Gal; using Ryujinx.Graphics.Gal;
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
namespace Ryujinx.HLE.Gpu.Memory namespace Ryujinx.HLE.Gpu.Memory
@ -279,6 +280,11 @@ namespace Ryujinx.HLE.Gpu.Memory
return Cache.IsRegionModified(Memory, BufferType, PA, Size); return Cache.IsRegionModified(Memory, BufferType, PA, Size);
} }
public IntPtr GetHostAddress(long Position, long Size)
{
return Memory.GetHostAddress(GetPhysicalAddress(Position), Size);
}
public byte ReadByte(long Position) public byte ReadByte(long Position)
{ {
Position = GetPhysicalAddress(Position); Position = GetPhysicalAddress(Position);

View file

@ -55,9 +55,11 @@ namespace Ryujinx.HLE.Gpu.Texture
int Pitch = (Tic[3] & 0xffff) << 5; int Pitch = (Tic[3] & 0xffff) << 5;
int BlockHeightLog2 = (Tic[3] >> 3) & 7; int BlockHeightLog2 = (Tic[3] >> 3) & 7;
int TileWidthLog2 = (Tic[3] >> 10) & 7;
int BlockHeight = 1 << BlockHeightLog2; int BlockHeight = 1 << BlockHeightLog2;
int TileWidth = 1 << TileWidthLog2;
int Width = (Tic[4] & 0xffff) + 1; int Width = (Tic[4] & 0xffff) + 1;
int Height = (Tic[5] & 0xffff) + 1; int Height = (Tic[5] & 0xffff) + 1;
@ -68,6 +70,7 @@ namespace Ryujinx.HLE.Gpu.Texture
Height, Height,
Pitch, Pitch,
BlockHeight, BlockHeight,
TileWidth,
Swizzle, Swizzle,
Format); Format);

View file

@ -7,8 +7,14 @@ namespace Ryujinx.HLE.Gpu.Texture
{ {
static class TextureHelper static class TextureHelper
{ {
public static ISwizzle GetSwizzle(TextureInfo Texture, int Width, int Bpp) public static ISwizzle GetSwizzle(TextureInfo Texture, int BlockWidth, int Bpp)
{ {
int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth;
int AlignMask = Texture.TileWidth * (64 / Bpp) - 1;
Width = (Width + AlignMask) & ~AlignMask;
switch (Texture.Swizzle) switch (Texture.Swizzle)
{ {
case TextureSwizzle._1dBuffer: case TextureSwizzle._1dBuffer:
@ -37,6 +43,8 @@ namespace Ryujinx.HLE.Gpu.Texture
case GalTextureFormat.A8B8G8R8: case GalTextureFormat.A8B8G8R8:
case GalTextureFormat.R32: case GalTextureFormat.R32:
case GalTextureFormat.ZF32: case GalTextureFormat.ZF32:
case GalTextureFormat.BF10GF11RF11:
case GalTextureFormat.Z24S8:
return Texture.Width * Texture.Height * 4; return Texture.Width * Texture.Height * 4;
case GalTextureFormat.A1B5G5R5: case GalTextureFormat.A1B5G5R5:
@ -54,6 +62,8 @@ namespace Ryujinx.HLE.Gpu.Texture
return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 8); return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 8);
} }
case GalTextureFormat.BC6H_SF16:
case GalTextureFormat.BC6H_UF16:
case GalTextureFormat.BC7U: case GalTextureFormat.BC7U:
case GalTextureFormat.BC2: case GalTextureFormat.BC2:
case GalTextureFormat.BC3: case GalTextureFormat.BC3:

View file

@ -11,6 +11,7 @@ namespace Ryujinx.HLE.Gpu.Texture
public int Pitch { get; private set; } public int Pitch { get; private set; }
public int BlockHeight { get; private set; } public int BlockHeight { get; private set; }
public int TileWidth { get; private set; }
public TextureSwizzle Swizzle { get; private set; } public TextureSwizzle Swizzle { get; private set; }
@ -29,6 +30,8 @@ namespace Ryujinx.HLE.Gpu.Texture
BlockHeight = 16; BlockHeight = 16;
TileWidth = 1;
Swizzle = TextureSwizzle.BlockLinear; Swizzle = TextureSwizzle.BlockLinear;
Format = GalTextureFormat.A8B8G8R8; Format = GalTextureFormat.A8B8G8R8;
@ -40,16 +43,18 @@ namespace Ryujinx.HLE.Gpu.Texture
int Height, int Height,
int Pitch, int Pitch,
int BlockHeight, int BlockHeight,
int TileWidth,
TextureSwizzle Swizzle, TextureSwizzle Swizzle,
GalTextureFormat Format) GalTextureFormat Format)
{ {
this.Position = Position; this.Position = Position;
this.Width = Width; this.Width = Width;
this.Height = Height; this.Height = Height;
this.Pitch = Pitch; this.Pitch = Pitch;
this.BlockHeight = BlockHeight; this.BlockHeight = BlockHeight;
this.Swizzle = Swizzle; this.TileWidth = TileWidth;
this.Format = Format; this.Swizzle = Swizzle;
this.Format = Format;
} }
} }
} }

View file

@ -14,11 +14,15 @@ namespace Ryujinx.HLE.Gpu.Texture
case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture); case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture);
case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture); case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture);
case GalTextureFormat.R32: return Read4Bpp (Memory, Texture); case GalTextureFormat.R32: return Read4Bpp (Memory, Texture);
case GalTextureFormat.BF10GF11RF11: return Read4Bpp (Memory, Texture);
case GalTextureFormat.Z24S8: return Read4Bpp (Memory, Texture);
case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture); case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture);
case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture); case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture);
case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture); case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture);
case GalTextureFormat.R16: return Read2Bpp (Memory, Texture); case GalTextureFormat.R16: return Read2Bpp (Memory, Texture);
case GalTextureFormat.R8: return Read1Bpp (Memory, Texture); case GalTextureFormat.R8: return Read1Bpp (Memory, Texture);
case GalTextureFormat.BC6H_SF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
case GalTextureFormat.BC6H_UF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
case GalTextureFormat.BC7U: return Read16BptCompressedTexture(Memory, Texture, 4, 4); case GalTextureFormat.BC7U: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture); case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture);
case GalTextureFormat.BC2: return Read16BptCompressedTexture(Memory, Texture, 4, 4); case GalTextureFormat.BC2: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
@ -40,7 +44,7 @@ namespace Ryujinx.HLE.Gpu.Texture
case GalTextureFormat.Astc2D8x5: return Read16BptCompressedTexture(Memory, Texture, 8, 5); case GalTextureFormat.Astc2D8x5: return Read16BptCompressedTexture(Memory, Texture, 8, 5);
case GalTextureFormat.Astc2D10x5: return Read16BptCompressedTexture(Memory, Texture, 10, 5); case GalTextureFormat.Astc2D10x5: return Read16BptCompressedTexture(Memory, Texture, 10, 5);
case GalTextureFormat.Astc2D10x6: return Read16BptCompressedTexture(Memory, Texture, 10, 6); case GalTextureFormat.Astc2D10x6: return Read16BptCompressedTexture(Memory, Texture, 10, 6);
} }
throw new NotImplementedException(Texture.Format.ToString()); throw new NotImplementedException(Texture.Format.ToString());
} }
@ -52,7 +56,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height]; byte[] Output = new byte[Width * Height];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 1); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 1);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -85,7 +89,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 2]; byte[] Output = new byte[Width * Height * 2];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -123,7 +127,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 2]; byte[] Output = new byte[Width * Height * 2];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -160,7 +164,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 2]; byte[] Output = new byte[Width * Height * 2];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -193,7 +197,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 4]; byte[] Output = new byte[Width * Height * 4];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -226,7 +230,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 8]; byte[] Output = new byte[Width * Height * 8];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 8);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -259,7 +263,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 16]; byte[] Output = new byte[Width * Height * 16];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 16);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -294,7 +298,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 8]; byte[] Output = new byte[Width * Height * 8];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 4, 8);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -327,7 +331,7 @@ namespace Ryujinx.HLE.Gpu.Texture
byte[] Output = new byte[Width * Height * 16]; byte[] Output = new byte[Width * Height * 16];
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16); ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, BlockWidth, 16);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,

View file

@ -6,29 +6,9 @@ namespace Ryujinx.HLE.Gpu.Texture
{ {
static class TextureWriter static class TextureWriter
{ {
public static void Write( public unsafe static void Write(IAMemory Memory, TextureInfo Texture, byte[] Data)
IAMemory Memory,
TextureInfo Texture,
byte[] Data,
int Width,
int Height)
{ {
switch (Texture.Format) ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4);
{
case GalTextureFormat.A8B8G8R8: Write4Bpp(Memory, Texture, Data, Width, Height); break;
default: throw new NotImplementedException(Texture.Format.ToString());
}
}
private unsafe static void Write4Bpp(
IAMemory Memory,
TextureInfo Texture,
byte[] Data,
int Width,
int Height)
{
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4);
(AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
Memory, Memory,
@ -38,8 +18,8 @@ namespace Ryujinx.HLE.Gpu.Texture
{ {
long InOffs = 0; long InOffs = 0;
for (int Y = 0; Y < Height; Y++) for (int Y = 0; Y < Texture.Height; Y++)
for (int X = 0; X < Width; X++) for (int X = 0; X < Texture.Width; X++)
{ {
long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);

View file

@ -2,6 +2,7 @@ using ChocolArm64.Memory;
using Ryujinx.HLE.Loaders.Executables; using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.HLE.OsHle; using Ryujinx.HLE.OsHle;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
namespace Ryujinx.HLE.Loaders namespace Ryujinx.HLE.Loaders
{ {
@ -15,6 +16,8 @@ namespace Ryujinx.HLE.Loaders
public string Name { get; private set; } public string Name { get; private set; }
public string FilePath { get; private set; }
private AMemory Memory; private AMemory Memory;
public long ImageBase { get; private set; } public long ImageBase { get; private set; }
@ -26,7 +29,12 @@ namespace Ryujinx.HLE.Loaders
m_SymbolTable = new Dictionary<long, string>(); m_SymbolTable = new Dictionary<long, string>();
Name = Exe.Name; FilePath = Exe.FilePath;
if (FilePath != null)
{
Name = Path.GetFileNameWithoutExtension(FilePath.Replace(Homebrew.TemporaryNroSuffix, ""));
}
this.Memory = Memory; this.Memory = Memory;
this.ImageBase = ImageBase; this.ImageBase = ImageBase;

View file

@ -2,7 +2,7 @@ namespace Ryujinx.HLE.Loaders.Executables
{ {
public interface IExecutable public interface IExecutable
{ {
string Name { get; } string FilePath { get; }
byte[] Text { get; } byte[] Text { get; }
byte[] RO { get; } byte[] RO { get; }

View file

@ -4,7 +4,7 @@ namespace Ryujinx.HLE.Loaders.Executables
{ {
class Nro : IExecutable class Nro : IExecutable
{ {
public string Name { get; private set; } public string FilePath { get; private set; }
public byte[] Text { get; private set; } public byte[] Text { get; private set; }
public byte[] RO { get; private set; } public byte[] RO { get; private set; }
@ -16,9 +16,9 @@ namespace Ryujinx.HLE.Loaders.Executables
public int DataOffset { get; private set; } public int DataOffset { get; private set; }
public int BssSize { get; private set; } public int BssSize { get; private set; }
public Nro(Stream Input, string Name) public Nro(Stream Input, string FilePath)
{ {
this.Name = Name; this.FilePath = FilePath;
BinaryReader Reader = new BinaryReader(Input); BinaryReader Reader = new BinaryReader(Input);

View file

@ -6,7 +6,7 @@ namespace Ryujinx.HLE.Loaders.Executables
{ {
class Nso : IExecutable class Nso : IExecutable
{ {
public string Name { get; private set; } public string FilePath { get; private set; }
public byte[] Text { get; private set; } public byte[] Text { get; private set; }
public byte[] RO { get; private set; } public byte[] RO { get; private set; }
@ -29,9 +29,9 @@ namespace Ryujinx.HLE.Loaders.Executables
HasDataHash = 1 << 5 HasDataHash = 1 << 5
} }
public Nso(Stream Input, string Name) public Nso(Stream Input, string FilePath)
{ {
this.Name = Name; this.FilePath = FilePath;
BinaryReader Reader = new BinaryReader(Input); BinaryReader Reader = new BinaryReader(Input);

View file

@ -9,10 +9,12 @@ namespace Ryujinx.HLE.OsHle.Handles
public int CoreMask { get; set; } public int CoreMask { get; set; }
public long MutexAddress { get; set; } public long MutexAddress { get; set; }
public long CondVarAddress { get; set; } public long CondVarAddress { get; set; }
public long ArbiterWaitAddress { get; set; }
public bool CondVarSignaled { get; set; } public bool CondVarSignaled { get; set; }
public bool ArbiterSignaled { get; set; }
private Process Process; private Process Process;

View file

@ -1,11 +1,14 @@
using ChocolArm64.Memory; using ChocolArm64.Memory;
using System.Text;
namespace Ryujinx.HLE.OsHle namespace Ryujinx.HLE.OsHle
{ {
static class Homebrew static class Homebrew
{ {
public const string TemporaryNroSuffix = ".ryu_tmp.nro";
//http://switchbrew.org/index.php?title=Homebrew_ABI //http://switchbrew.org/index.php?title=Homebrew_ABI
public static void WriteHbAbiData(AMemory Memory, long Position, int MainThreadHandle) public static void WriteHbAbiData(AMemory Memory, long Position, int MainThreadHandle, string SwitchPath)
{ {
Memory.Manager.Map(Position, AMemoryMgr.PageSize, (int)MemoryType.Normal, AMemoryPerm.RW); Memory.Manager.Map(Position, AMemoryMgr.PageSize, (int)MemoryType.Normal, AMemoryPerm.RW);
@ -15,6 +18,11 @@ namespace Ryujinx.HLE.OsHle
//NextLoadPath //NextLoadPath
WriteConfigEntry(Memory, ref Position, 2, 0, Position + 0x200, Position + 0x400); WriteConfigEntry(Memory, ref Position, 2, 0, Position + 0x200, Position + 0x400);
// Argv
long ArgvPosition = Position + 0xC00;
WriteConfigEntry(Memory, ref Position, 5, 0, 0, ArgvPosition);
Memory.WriteBytes(ArgvPosition, Encoding.ASCII.GetBytes(SwitchPath + "\0"));
//AppletType //AppletType
WriteConfigEntry(Memory, ref Position, 7); WriteConfigEntry(Memory, ref Position, 7);

View file

@ -87,19 +87,35 @@ namespace Ryujinx.HLE.OsHle
MainProcess.Run(); MainProcess.Run();
} }
public void LoadProgram(string FileName) public void LoadProgram(string FilePath)
{ {
bool IsNro = Path.GetExtension(FileName).ToLower() == ".nro"; bool IsNro = Path.GetExtension(FilePath).ToLower() == ".nro";
string Name = Path.GetFileNameWithoutExtension(FileName); string Name = Path.GetFileNameWithoutExtension(FilePath);
string SwitchFilePath = Ns.VFs.SystemPathToSwitchPath(FilePath);
if (IsNro && (SwitchFilePath == null || !SwitchFilePath.StartsWith("sdmc:/")))
{
string SwitchPath = $"sdmc:/switch/{Name}{Homebrew.TemporaryNroSuffix}";
string TempPath = Ns.VFs.SwitchPathToSystemPath(SwitchPath);
string SwitchDir = Path.GetDirectoryName(TempPath);
if (!Directory.Exists(SwitchDir))
{
Directory.CreateDirectory(SwitchDir);
}
File.Copy(FilePath, TempPath, true);
FilePath = TempPath;
}
Process MainProcess = MakeProcess(); Process MainProcess = MakeProcess();
using (FileStream Input = new FileStream(FileName, FileMode.Open)) using (FileStream Input = new FileStream(FilePath, FileMode.Open))
{ {
MainProcess.LoadProgram(IsNro MainProcess.LoadProgram(IsNro
? (IExecutable)new Nro(Input, Name) ? (IExecutable)new Nro(Input, FilePath)
: (IExecutable)new Nso(Input, Name)); : (IExecutable)new Nso(Input, FilePath));
} }
MainProcess.SetEmptyArgs(); MainProcess.SetEmptyArgs();

View file

@ -0,0 +1,112 @@
using ChocolArm64.Memory;
using ChocolArm64.State;
using Ryujinx.HLE.OsHle.Handles;
using static Ryujinx.HLE.OsHle.ErrorCode;
namespace Ryujinx.HLE.OsHle.Kernel
{
static class AddressArbiter
{
static ulong WaitForAddress(Process Process, AThreadState ThreadState, long Address, ulong Timeout)
{
KThread CurrentThread = Process.GetThread(ThreadState.Tpidr);
Process.Scheduler.SetReschedule(CurrentThread.ProcessorId);
CurrentThread.ArbiterWaitAddress = Address;
CurrentThread.ArbiterSignaled = false;
Process.Scheduler.EnterWait(CurrentThread, NsTimeConverter.GetTimeMs(Timeout));
if (!CurrentThread.ArbiterSignaled)
{
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
}
return 0;
}
public static ulong WaitForAddressIfLessThan(Process Process,
AThreadState ThreadState,
AMemory Memory,
long Address,
int Value,
ulong Timeout,
bool ShouldDecrement)
{
Memory.SetExclusive(ThreadState, Address);
int CurrentValue = Memory.ReadInt32(Address);
while (true)
{
if (Memory.TestExclusive(ThreadState, Address))
{
if (CurrentValue < Value)
{
if (ShouldDecrement)
{
Memory.WriteInt32(Address, CurrentValue - 1);
}
Memory.ClearExclusiveForStore(ThreadState);
}
else
{
Memory.ClearExclusiveForStore(ThreadState);
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
}
break;
}
Memory.SetExclusive(ThreadState, Address);
CurrentValue = Memory.ReadInt32(Address);
}
if (Timeout == 0)
{
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
}
return WaitForAddress(Process, ThreadState, Address, Timeout);
}
public static ulong WaitForAddressIfEqual(Process Process,
AThreadState ThreadState,
AMemory Memory,
long Address,
int Value,
ulong Timeout)
{
if (Memory.ReadInt32(Address) != Value)
{
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
}
if (Timeout == 0)
{
return MakeError(ErrorModule.Kernel, KernelErr.Timeout);
}
return WaitForAddress(Process, ThreadState, Address, Timeout);
}
}
enum ArbitrationType : int
{
WaitIfLessThan,
DecrementAndWaitIfLessThan,
WaitIfEqual
}
enum SignalType : int
{
Signal,
IncrementAndSignalIfEqual,
ModifyByWaitingCountAndSignalIfEqual
}
}

View file

@ -12,7 +12,7 @@ namespace Ryujinx.HLE.OsHle.Kernel
public const int Timeout = 117; public const int Timeout = 117;
public const int Canceled = 118; public const int Canceled = 118;
public const int CountOutOfRange = 119; public const int CountOutOfRange = 119;
public const int InvalidInfo = 120; public const int InvalidEnumValue = 120;
public const int InvalidThread = 122; public const int InvalidThread = 122;
public const int InvalidState = 125; public const int InvalidState = 125;
} }

View file

@ -73,7 +73,8 @@ namespace Ryujinx.HLE.OsHle.Kernel
{ 0x2c, SvcMapPhysicalMemory }, { 0x2c, SvcMapPhysicalMemory },
{ 0x2d, SvcUnmapPhysicalMemory }, { 0x2d, SvcUnmapPhysicalMemory },
{ 0x32, SvcSetThreadActivity }, { 0x32, SvcSetThreadActivity },
{ 0x33, SvcGetThreadContext3 } { 0x33, SvcGetThreadContext3 },
{ 0x34, SvcWaitForAddress }
}; };
this.Ns = Ns; this.Ns = Ns;

View file

@ -294,7 +294,7 @@ namespace Ryujinx.HLE.OsHle.Kernel
InfoType == 19 || InfoType == 19 ||
InfoType == 20) InfoType == 20)
{ {
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidInfo); ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
return; return;
} }

View file

@ -197,6 +197,57 @@ namespace Ryujinx.HLE.OsHle.Kernel
Process.Scheduler.EnterWait(CurrThread); Process.Scheduler.EnterWait(CurrThread);
} }
private void SvcWaitForAddress(AThreadState ThreadState)
{
long Address = (long)ThreadState.X0;
ArbitrationType Type = (ArbitrationType)ThreadState.X1;
int Value = (int)ThreadState.X2;
ulong Timeout = ThreadState.X3;
Ns.Log.PrintDebug(LogClass.KernelSvc,
"Address = " + Address.ToString("x16") + ", " +
"ArbitrationType = " + Type .ToString() + ", " +
"Value = " + Value .ToString("x8") + ", " +
"Timeout = " + Timeout.ToString("x16"));
if (IsPointingInsideKernel(Address))
{
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address 0x{Address:x16}!");
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
return;
}
if (IsWordAddressUnaligned(Address))
{
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned address 0x{Address:x16}!");
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
return;
}
switch (Type)
{
case ArbitrationType.WaitIfLessThan:
ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, false);
break;
case ArbitrationType.DecrementAndWaitIfLessThan:
ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, true);
break;
case ArbitrationType.WaitIfEqual:
ThreadState.X0 = AddressArbiter.WaitForAddressIfEqual(Process, ThreadState, Memory, Address, Value, Timeout);
break;
default:
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
break;
}
}
private void MutexUnlock(KThread CurrThread, long MutexAddress) private void MutexUnlock(KThread CurrThread, long MutexAddress)
{ {
lock (Process.ThreadSyncLock) lock (Process.ThreadSyncLock)

View file

@ -13,6 +13,7 @@ using Ryujinx.HLE.OsHle.Services.Nv;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
namespace Ryujinx.HLE.OsHle namespace Ryujinx.HLE.OsHle
@ -155,7 +156,9 @@ namespace Ryujinx.HLE.OsHle
{ {
HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd); HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd);
Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle); string SwitchPath = Ns.VFs.SystemPathToSwitchPath(Executables[0].FilePath);
Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle, SwitchPath);
MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition; MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
MainThread.Thread.ThreadState.X1 = ulong.MaxValue; MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
@ -423,6 +426,11 @@ namespace Ryujinx.HLE.OsHle
} }
} }
if (NeedsHbAbi && Executables.Count > 0 && Executables[0].FilePath.EndsWith(Homebrew.TemporaryNroSuffix))
{
File.Delete(Executables[0].FilePath);
}
INvDrvServices.UnloadProcess(this); INvDrvServices.UnloadProcess(this);
AppletState.Dispose(); AppletState.Dispose();

View file

@ -17,7 +17,9 @@ namespace Ryujinx.HLE.OsHle.Services.Am
{ {
m_Commands = new Dictionary<int, ServiceProcessRequest>() m_Commands = new Dictionary<int, ServiceProcessRequest>()
{ {
{ 0, Exit },
{ 1, LockExit }, { 1, LockExit },
{ 2, UnlockExit },
{ 9, GetLibraryAppletLaunchableEvent }, { 9, GetLibraryAppletLaunchableEvent },
{ 10, SetScreenShotPermission }, { 10, SetScreenShotPermission },
{ 11, SetOperationModeChangedNotification }, { 11, SetOperationModeChangedNotification },
@ -31,8 +33,24 @@ namespace Ryujinx.HLE.OsHle.Services.Am
LaunchableEvent = new KEvent(); LaunchableEvent = new KEvent();
} }
public long Exit(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAm, "Stubbed.");
return 0;
}
public long LockExit(ServiceCtx Context) public long LockExit(ServiceCtx Context)
{ {
Context.Ns.Log.PrintStub(LogClass.ServiceAm, "Stubbed.");
return 0;
}
public long UnlockExit(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAm, "Stubbed.");
return 0; return 0;
} }

View file

@ -3,6 +3,7 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
static class AudErr static class AudErr
{ {
public const int DeviceNotFound = 1; public const int DeviceNotFound = 1;
public const int UnsupportedRevision = 2;
public const int UnsupportedSampleRate = 3; public const int UnsupportedSampleRate = 3;
} }
} }

View file

@ -1,6 +1,6 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud namespace Ryujinx.HLE.OsHle.Services.Aud.AudioOut
{ {
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
struct AudioOutData struct AudioOutData

View file

@ -1,12 +1,11 @@
using ChocolArm64.Memory; using ChocolArm64.Memory;
using Ryujinx.Audio; using Ryujinx.Audio;
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Handles; using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc; using Ryujinx.HLE.OsHle.Ipc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Aud namespace Ryujinx.HLE.OsHle.Services.Aud.AudioOut
{ {
class IAudioOut : IpcService, IDisposable class IAudioOut : IpcService, IDisposable
{ {

View file

@ -0,0 +1,8 @@
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
static class AudioConsts
{
public const int HostSampleRate = 48000;
public const int HostChannelsCount = 2;
}
}

View file

@ -0,0 +1,11 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x10, Pack = 4)]
struct BehaviorIn
{
public long Unknown0;
public long Unknown8;
}
}

View file

@ -0,0 +1,16 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0xc, Pack = 1)]
struct BiquadFilter
{
public byte Enable;
public byte Padding;
public short B0;
public short B1;
public short B2;
public short A1;
public short A2;
}
}

View file

@ -0,0 +1,316 @@
using ChocolArm64.Memory;
using Ryujinx.Audio;
using Ryujinx.Audio.Adpcm;
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.Utilities;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
class IAudioRenderer : IpcService, IDisposable
{
//This is the amount of samples that are going to be appended
//each time that RequestUpdateAudioRenderer is called. Ideally,
//this value shouldn't be neither too small (to avoid the player
//starving due to running out of samples) or too large (to avoid
//high latency).
private const int MixBufferSamplesCount = 960;
private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
private KEvent UpdateEvent;
private AMemory Memory;
private IAalOutput AudioOut;
private AudioRendererParameter Params;
private MemoryPoolContext[] MemoryPools;
private VoiceContext[] Voices;
private int Track;
public IAudioRenderer(AMemory Memory, IAalOutput AudioOut, AudioRendererParameter Params)
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 4, RequestUpdateAudioRenderer },
{ 5, StartAudioRenderer },
{ 6, StopAudioRenderer },
{ 7, QuerySystemEvent }
};
UpdateEvent = new KEvent();
this.Memory = Memory;
this.AudioOut = AudioOut;
this.Params = Params;
Track = AudioOut.OpenTrack(
AudioConsts.HostSampleRate,
AudioConsts.HostChannelsCount,
AudioCallback);
MemoryPools = CreateArray<MemoryPoolContext>(Params.EffectCount + Params.VoiceCount * 4);
Voices = CreateArray<VoiceContext>(Params.VoiceCount);
InitializeAudioOut();
}
private void AudioCallback()
{
UpdateEvent.WaitEvent.Set();
}
private static T[] CreateArray<T>(int Size) where T : new()
{
T[] Output = new T[Size];
for (int Index = 0; Index < Size; Index++)
{
Output[Index] = new T();
}
return Output;
}
private void InitializeAudioOut()
{
AppendMixedBuffer(0);
AppendMixedBuffer(1);
AppendMixedBuffer(2);
AudioOut.Start(Track);
}
public long RequestUpdateAudioRenderer(ServiceCtx Context)
{
long OutputPosition = Context.Request.ReceiveBuff[0].Position;
long OutputSize = Context.Request.ReceiveBuff[0].Size;
AMemoryHelper.FillWithZeros(Context.Memory, OutputPosition, (int)OutputSize);
long InputPosition = Context.Request.SendBuff[0].Position;
StructReader Reader = new StructReader(Context.Memory, InputPosition);
StructWriter Writer = new StructWriter(Context.Memory, OutputPosition);
UpdateDataHeader InputHeader = Reader.Read<UpdateDataHeader>();
Reader.Read<BehaviorIn>(InputHeader.BehaviorSize);
MemoryPoolIn[] MemoryPoolsIn = Reader.Read<MemoryPoolIn>(InputHeader.MemoryPoolSize);
for (int Index = 0; Index < MemoryPoolsIn.Length; Index++)
{
MemoryPoolIn MemoryPool = MemoryPoolsIn[Index];
if (MemoryPool.State == MemoryPoolState.RequestAttach)
{
MemoryPools[Index].OutStatus.State = MemoryPoolState.Attached;
}
else if (MemoryPool.State == MemoryPoolState.RequestDetach)
{
MemoryPools[Index].OutStatus.State = MemoryPoolState.Detached;
}
}
Reader.Read<VoiceChannelResourceIn>(InputHeader.VoiceResourceSize);
VoiceIn[] VoicesIn = Reader.Read<VoiceIn>(InputHeader.VoiceSize);
for (int Index = 0; Index < VoicesIn.Length; Index++)
{
VoiceIn Voice = VoicesIn[Index];
VoiceContext VoiceCtx = Voices[Index];
VoiceCtx.SetAcquireState(Voice.Acquired != 0);
if (Voice.Acquired == 0)
{
continue;
}
if (Voice.FirstUpdate != 0)
{
VoiceCtx.AdpcmCtx = GetAdpcmDecoderContext(
Voice.AdpcmCoeffsPosition,
Voice.AdpcmCoeffsSize);
VoiceCtx.SampleFormat = Voice.SampleFormat;
VoiceCtx.SampleRate = Voice.SampleRate;
VoiceCtx.ChannelsCount = Voice.ChannelsCount;
VoiceCtx.SetBufferIndex(Voice.BaseWaveBufferIndex);
}
VoiceCtx.WaveBuffers[0] = Voice.WaveBuffer0;
VoiceCtx.WaveBuffers[1] = Voice.WaveBuffer1;
VoiceCtx.WaveBuffers[2] = Voice.WaveBuffer2;
VoiceCtx.WaveBuffers[3] = Voice.WaveBuffer3;
VoiceCtx.Volume = Voice.Volume;
VoiceCtx.PlayState = Voice.PlayState;
}
UpdateAudio();
UpdateDataHeader OutputHeader = new UpdateDataHeader();
int UpdateHeaderSize = Marshal.SizeOf<UpdateDataHeader>();
OutputHeader.Revision = IAudioRendererManager.RevMagic;
OutputHeader.BehaviorSize = 0xb0;
OutputHeader.MemoryPoolSize = (Params.EffectCount + Params.VoiceCount * 4) * 0x10;
OutputHeader.VoiceSize = Params.VoiceCount * 0x10;
OutputHeader.EffectSize = Params.EffectCount * 0x10;
OutputHeader.SinkSize = Params.SinkCount * 0x20;
OutputHeader.PerformanceManagerSize = 0x10;
OutputHeader.TotalSize = UpdateHeaderSize +
OutputHeader.BehaviorSize +
OutputHeader.MemoryPoolSize +
OutputHeader.VoiceSize +
OutputHeader.EffectSize +
OutputHeader.SinkSize +
OutputHeader.PerformanceManagerSize;
Writer.Write(OutputHeader);
foreach (MemoryPoolContext MemoryPool in MemoryPools)
{
Writer.Write(MemoryPool.OutStatus);
}
foreach (VoiceContext Voice in Voices)
{
Writer.Write(Voice.OutStatus);
}
return 0;
}
public long StartAudioRenderer(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
return 0;
}
public long StopAudioRenderer(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
return 0;
}
public long QuerySystemEvent(ServiceCtx Context)
{
int Handle = Context.Process.HandleTable.OpenHandle(UpdateEvent);
Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
return 0;
}
private AdpcmDecoderContext GetAdpcmDecoderContext(long Position, long Size)
{
if (Size == 0)
{
return null;
}
AdpcmDecoderContext Context = new AdpcmDecoderContext();
Context.Coefficients = new short[Size >> 1];
for (int Offset = 0; Offset < Size; Offset += 2)
{
Context.Coefficients[Offset >> 1] = Memory.ReadInt16(Position + Offset);
}
return Context;
}
private void UpdateAudio()
{
long[] Released = AudioOut.GetReleasedBuffers(Track, 2);
for (int Index = 0; Index < Released.Length; Index++)
{
AppendMixedBuffer(Released[Index]);
}
}
private void AppendMixedBuffer(long Tag)
{
int[] MixBuffer = new int[MixBufferSamplesCount * AudioConsts.HostChannelsCount];
foreach (VoiceContext Voice in Voices)
{
if (!Voice.Playing)
{
continue;
}
int OutOffset = 0;
int PendingSamples = MixBufferSamplesCount;
while (PendingSamples > 0)
{
int[] Samples = Voice.GetBufferData(Memory, PendingSamples, out int ReturnedSamples);
if (ReturnedSamples == 0)
{
break;
}
PendingSamples -= ReturnedSamples;
for (int Offset = 0; Offset < Samples.Length; Offset++)
{
int Sample = (int)(Samples[Offset] * Voice.Volume);
MixBuffer[OutOffset++] += Sample;
}
}
}
AudioOut.AppendBuffer(Track, Tag, GetFinalBuffer(MixBuffer));
}
private static short[] GetFinalBuffer(int[] Buffer)
{
short[] Output = new short[Buffer.Length];
for (int Offset = 0; Offset < Buffer.Length; Offset++)
{
Output[Offset] = DspUtils.Saturate(Buffer[Offset]);
}
return Output;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing)
{
UpdateEvent.Dispose();
}
}
}
}

View file

@ -0,0 +1,12 @@
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
class MemoryPoolContext
{
public MemoryPoolOut OutStatus;
public MemoryPoolContext()
{
OutStatus.State = MemoryPoolState.Detached;
}
}
}

View file

@ -0,0 +1,14 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = 4)]
struct MemoryPoolIn
{
public long Address;
public long Size;
public MemoryPoolState State;
public int Unknown14;
public long Unknown18;
}
}

View file

@ -0,0 +1,12 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x10, Pack = 4)]
struct MemoryPoolOut
{
public MemoryPoolState State;
public int Unknown14;
public long Unknown18;
}
}

View file

@ -1,4 +1,4 @@
namespace Ryujinx.HLE.OsHle.Services.Aud namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{ {
enum MemoryPoolState : int enum MemoryPoolState : int
{ {

View file

@ -0,0 +1,9 @@
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
enum PlayState : byte
{
Playing = 0,
Stopped = 1,
Paused = 2
}
}

View file

@ -0,0 +1,191 @@
using System;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
static class Resampler
{
#region "LookUp Tables"
private static short[] CurveLut0 = new short[]
{
6600, 19426, 6722, 3, 6479, 19424, 6845, 9, 6359, 19419, 6968, 15, 6239, 19412, 7093, 22,
6121, 19403, 7219, 28, 6004, 19391, 7345, 34, 5888, 19377, 7472, 41, 5773, 19361, 7600, 48,
5659, 19342, 7728, 55, 5546, 19321, 7857, 62, 5434, 19298, 7987, 69, 5323, 19273, 8118, 77,
5213, 19245, 8249, 84, 5104, 19215, 8381, 92, 4997, 19183, 8513, 101, 4890, 19148, 8646, 109,
4785, 19112, 8780, 118, 4681, 19073, 8914, 127, 4579, 19031, 9048, 137, 4477, 18988, 9183, 147,
4377, 18942, 9318, 157, 4277, 18895, 9454, 168, 4179, 18845, 9590, 179, 4083, 18793, 9726, 190,
3987, 18738, 9863, 202, 3893, 18682, 10000, 215, 3800, 18624, 10137, 228, 3709, 18563, 10274, 241,
3618, 18500, 10411, 255, 3529, 18436, 10549, 270, 3441, 18369, 10687, 285, 3355, 18300, 10824, 300,
3269, 18230, 10962, 317, 3186, 18157, 11100, 334, 3103, 18082, 11238, 351, 3022, 18006, 11375, 369,
2942, 17927, 11513, 388, 2863, 17847, 11650, 408, 2785, 17765, 11788, 428, 2709, 17681, 11925, 449,
2635, 17595, 12062, 471, 2561, 17507, 12198, 494, 2489, 17418, 12334, 517, 2418, 17327, 12470, 541,
2348, 17234, 12606, 566, 2280, 17140, 12741, 592, 2213, 17044, 12876, 619, 2147, 16946, 13010, 647,
2083, 16846, 13144, 675, 2020, 16745, 13277, 704, 1958, 16643, 13409, 735, 1897, 16539, 13541, 766,
1838, 16434, 13673, 798, 1780, 16327, 13803, 832, 1723, 16218, 13933, 866, 1667, 16109, 14062, 901,
1613, 15998, 14191, 937, 1560, 15885, 14318, 975, 1508, 15772, 14445, 1013, 1457, 15657, 14571, 1052,
1407, 15540, 14695, 1093, 1359, 15423, 14819, 1134, 1312, 15304, 14942, 1177, 1266, 15185, 15064, 1221,
1221, 15064, 15185, 1266, 1177, 14942, 15304, 1312, 1134, 14819, 15423, 1359, 1093, 14695, 15540, 1407,
1052, 14571, 15657, 1457, 1013, 14445, 15772, 1508, 975, 14318, 15885, 1560, 937, 14191, 15998, 1613,
901, 14062, 16109, 1667, 866, 13933, 16218, 1723, 832, 13803, 16327, 1780, 798, 13673, 16434, 1838,
766, 13541, 16539, 1897, 735, 13409, 16643, 1958, 704, 13277, 16745, 2020, 675, 13144, 16846, 2083,
647, 13010, 16946, 2147, 619, 12876, 17044, 2213, 592, 12741, 17140, 2280, 566, 12606, 17234, 2348,
541, 12470, 17327, 2418, 517, 12334, 17418, 2489, 494, 12198, 17507, 2561, 471, 12062, 17595, 2635,
449, 11925, 17681, 2709, 428, 11788, 17765, 2785, 408, 11650, 17847, 2863, 388, 11513, 17927, 2942,
369, 11375, 18006, 3022, 351, 11238, 18082, 3103, 334, 11100, 18157, 3186, 317, 10962, 18230, 3269,
300, 10824, 18300, 3355, 285, 10687, 18369, 3441, 270, 10549, 18436, 3529, 255, 10411, 18500, 3618,
241, 10274, 18563, 3709, 228, 10137, 18624, 3800, 215, 10000, 18682, 3893, 202, 9863, 18738, 3987,
190, 9726, 18793, 4083, 179, 9590, 18845, 4179, 168, 9454, 18895, 4277, 157, 9318, 18942, 4377,
147, 9183, 18988, 4477, 137, 9048, 19031, 4579, 127, 8914, 19073, 4681, 118, 8780, 19112, 4785,
109, 8646, 19148, 4890, 101, 8513, 19183, 4997, 92, 8381, 19215, 5104, 84, 8249, 19245, 5213,
77, 8118, 19273, 5323, 69, 7987, 19298, 5434, 62, 7857, 19321, 5546, 55, 7728, 19342, 5659,
48, 7600, 19361, 5773, 41, 7472, 19377, 5888, 34, 7345, 19391, 6004, 28, 7219, 19403, 6121,
22, 7093, 19412, 6239, 15, 6968, 19419, 6359, 9, 6845, 19424, 6479, 3, 6722, 19426, 6600
};
private static short[] CurveLut1 = new short[]
{
-68, 32639, 69, -5, -200, 32630, 212, -15, -328, 32613, 359, -26, -450, 32586, 512, -36,
-568, 32551, 669, -47, -680, 32507, 832, -58, -788, 32454, 1000, -69, -891, 32393, 1174, -80,
-990, 32323, 1352, -92, -1084, 32244, 1536, -103, -1173, 32157, 1724, -115, -1258, 32061, 1919, -128,
-1338, 31956, 2118, -140, -1414, 31844, 2322, -153, -1486, 31723, 2532, -167, -1554, 31593, 2747, -180,
-1617, 31456, 2967, -194, -1676, 31310, 3192, -209, -1732, 31157, 3422, -224, -1783, 30995, 3657, -240,
-1830, 30826, 3897, -256, -1874, 30649, 4143, -272, -1914, 30464, 4393, -289, -1951, 30272, 4648, -307,
-1984, 30072, 4908, -325, -2014, 29866, 5172, -343, -2040, 29652, 5442, -362, -2063, 29431, 5716, -382,
-2083, 29203, 5994, -403, -2100, 28968, 6277, -424, -2114, 28727, 6565, -445, -2125, 28480, 6857, -468,
-2133, 28226, 7153, -490, -2139, 27966, 7453, -514, -2142, 27700, 7758, -538, -2142, 27428, 8066, -563,
-2141, 27151, 8378, -588, -2136, 26867, 8694, -614, -2130, 26579, 9013, -641, -2121, 26285, 9336, -668,
-2111, 25987, 9663, -696, -2098, 25683, 9993, -724, -2084, 25375, 10326, -753, -2067, 25063, 10662, -783,
-2049, 24746, 11000, -813, -2030, 24425, 11342, -844, -2009, 24100, 11686, -875, -1986, 23771, 12033, -907,
-1962, 23438, 12382, -939, -1937, 23103, 12733, -972, -1911, 22764, 13086, -1005, -1883, 22422, 13441, -1039,
-1855, 22077, 13798, -1072, -1825, 21729, 14156, -1107, -1795, 21380, 14516, -1141, -1764, 21027, 14877, -1176,
-1732, 20673, 15239, -1211, -1700, 20317, 15602, -1246, -1667, 19959, 15965, -1282, -1633, 19600, 16329, -1317,
-1599, 19239, 16694, -1353, -1564, 18878, 17058, -1388, -1530, 18515, 17423, -1424, -1495, 18151, 17787, -1459,
-1459, 17787, 18151, -1495, -1424, 17423, 18515, -1530, -1388, 17058, 18878, -1564, -1353, 16694, 19239, -1599,
-1317, 16329, 19600, -1633, -1282, 15965, 19959, -1667, -1246, 15602, 20317, -1700, -1211, 15239, 20673, -1732,
-1176, 14877, 21027, -1764, -1141, 14516, 21380, -1795, -1107, 14156, 21729, -1825, -1072, 13798, 22077, -1855,
-1039, 13441, 22422, -1883, -1005, 13086, 22764, -1911, -972, 12733, 23103, -1937, -939, 12382, 23438, -1962,
-907, 12033, 23771, -1986, -875, 11686, 24100, -2009, -844, 11342, 24425, -2030, -813, 11000, 24746, -2049,
-783, 10662, 25063, -2067, -753, 10326, 25375, -2084, -724, 9993, 25683, -2098, -696, 9663, 25987, -2111,
-668, 9336, 26285, -2121, -641, 9013, 26579, -2130, -614, 8694, 26867, -2136, -588, 8378, 27151, -2141,
-563, 8066, 27428, -2142, -538, 7758, 27700, -2142, -514, 7453, 27966, -2139, -490, 7153, 28226, -2133,
-468, 6857, 28480, -2125, -445, 6565, 28727, -2114, -424, 6277, 28968, -2100, -403, 5994, 29203, -2083,
-382, 5716, 29431, -2063, -362, 5442, 29652, -2040, -343, 5172, 29866, -2014, -325, 4908, 30072, -1984,
-307, 4648, 30272, -1951, -289, 4393, 30464, -1914, -272, 4143, 30649, -1874, -256, 3897, 30826, -1830,
-240, 3657, 30995, -1783, -224, 3422, 31157, -1732, -209, 3192, 31310, -1676, -194, 2967, 31456, -1617,
-180, 2747, 31593, -1554, -167, 2532, 31723, -1486, -153, 2322, 31844, -1414, -140, 2118, 31956, -1338,
-128, 1919, 32061, -1258, -115, 1724, 32157, -1173, -103, 1536, 32244, -1084, -92, 1352, 32323, -990,
-80, 1174, 32393, -891, -69, 1000, 32454, -788, -58, 832, 32507, -680, -47, 669, 32551, -568,
-36, 512, 32586, -450, -26, 359, 32613, -328, -15, 212, 32630, -200, -5, 69, 32639, -68
};
private static short[] CurveLut2 = new short[]
{
3195, 26287, 3329, -32, 3064, 26281, 3467, -34, 2936, 26270, 3608, -38, 2811, 26253, 3751, -42,
2688, 26230, 3897, -46, 2568, 26202, 4046, -50, 2451, 26169, 4199, -54, 2338, 26130, 4354, -58,
2227, 26085, 4512, -63, 2120, 26035, 4673, -67, 2015, 25980, 4837, -72, 1912, 25919, 5004, -76,
1813, 25852, 5174, -81, 1716, 25780, 5347, -87, 1622, 25704, 5522, -92, 1531, 25621, 5701, -98,
1442, 25533, 5882, -103, 1357, 25440, 6066, -109, 1274, 25342, 6253, -115, 1193, 25239, 6442, -121,
1115, 25131, 6635, -127, 1040, 25018, 6830, -133, 967, 24899, 7027, -140, 897, 24776, 7227, -146,
829, 24648, 7430, -153, 764, 24516, 7635, -159, 701, 24379, 7842, -166, 641, 24237, 8052, -174,
583, 24091, 8264, -181, 526, 23940, 8478, -187, 472, 23785, 8695, -194, 420, 23626, 8914, -202,
371, 23462, 9135, -209, 324, 23295, 9358, -215, 279, 23123, 9583, -222, 236, 22948, 9809, -230,
194, 22769, 10038, -237, 154, 22586, 10269, -243, 117, 22399, 10501, -250, 81, 22208, 10735, -258,
47, 22015, 10970, -265, 15, 21818, 11206, -271, -16, 21618, 11444, -277, -44, 21415, 11684, -283,
-71, 21208, 11924, -290, -97, 20999, 12166, -296, -121, 20786, 12409, -302, -143, 20571, 12653, -306,
-163, 20354, 12898, -311, -183, 20134, 13143, -316, -201, 19911, 13389, -321, -218, 19686, 13635, -325,
-234, 19459, 13882, -328, -248, 19230, 14130, -332, -261, 18998, 14377, -335, -273, 18765, 14625, -337,
-284, 18531, 14873, -339, -294, 18295, 15121, -341, -302, 18057, 15369, -341, -310, 17817, 15617, -341,
-317, 17577, 15864, -340, -323, 17335, 16111, -340, -328, 17092, 16357, -338, -332, 16848, 16603, -336,
-336, 16603, 16848, -332, -338, 16357, 17092, -328, -340, 16111, 17335, -323, -340, 15864, 17577, -317,
-341, 15617, 17817, -310, -341, 15369, 18057, -302, -341, 15121, 18295, -294, -339, 14873, 18531, -284,
-337, 14625, 18765, -273, -335, 14377, 18998, -261, -332, 14130, 19230, -248, -328, 13882, 19459, -234,
-325, 13635, 19686, -218, -321, 13389, 19911, -201, -316, 13143, 20134, -183, -311, 12898, 20354, -163,
-306, 12653, 20571, -143, -302, 12409, 20786, -121, -296, 12166, 20999, -97, -290, 11924, 21208, -71,
-283, 11684, 21415, -44, -277, 11444, 21618, -16, -271, 11206, 21818, 15, -265, 10970, 22015, 47,
-258, 10735, 22208, 81, -250, 10501, 22399, 117, -243, 10269, 22586, 154, -237, 10038, 22769, 194,
-230, 9809, 22948, 236, -222, 9583, 23123, 279, -215, 9358, 23295, 324, -209, 9135, 23462, 371,
-202, 8914, 23626, 420, -194, 8695, 23785, 472, -187, 8478, 23940, 526, -181, 8264, 24091, 583,
-174, 8052, 24237, 641, -166, 7842, 24379, 701, -159, 7635, 24516, 764, -153, 7430, 24648, 829,
-146, 7227, 24776, 897, -140, 7027, 24899, 967, -133, 6830, 25018, 1040, -127, 6635, 25131, 1115,
-121, 6442, 25239, 1193, -115, 6253, 25342, 1274, -109, 6066, 25440, 1357, -103, 5882, 25533, 1442,
-98, 5701, 25621, 1531, -92, 5522, 25704, 1622, -87, 5347, 25780, 1716, -81, 5174, 25852, 1813,
-76, 5004, 25919, 1912, -72, 4837, 25980, 2015, -67, 4673, 26035, 2120, -63, 4512, 26085, 2227,
-58, 4354, 26130, 2338, -54, 4199, 26169, 2451, -50, 4046, 26202, 2568, -46, 3897, 26230, 2688,
-42, 3751, 26253, 2811, -38, 3608, 26270, 2936, -34, 3467, 26281, 3064, -32, 3329, 26287, 3195
};
#endregion
public static int[] Resample2Ch(
int[] Buffer,
int SrcSampleRate,
int DstSampleRate,
int SamplesCount,
ref int FracPart)
{
if (Buffer == null)
{
throw new ArgumentNullException(nameof(Buffer));
}
if (SrcSampleRate <= 0)
{
throw new ArgumentOutOfRangeException(nameof(SrcSampleRate));
}
if (DstSampleRate <= 0)
{
throw new ArgumentOutOfRangeException(nameof(DstSampleRate));
}
double Ratio = (double)SrcSampleRate / DstSampleRate;
int NewSamplesCount = (int)(SamplesCount / Ratio);
int Step = (int)(Ratio * 0x8000);
int[] Output = new int[NewSamplesCount * 2];
short[] Lut;
if (Step > 0xaaaa)
{
Lut = CurveLut0;
}
else if (Step <= 0x8000)
{
Lut = CurveLut1;
}
else
{
Lut = CurveLut2;
}
int InOffs = 0;
for (int OutOffs = 0; OutOffs < Output.Length; OutOffs += 2)
{
int LutIndex = (FracPart >> 8) * 4;
int Sample0 = Buffer[(InOffs + 0) * 2 + 0] * Lut[LutIndex + 0] +
Buffer[(InOffs + 1) * 2 + 0] * Lut[LutIndex + 1] +
Buffer[(InOffs + 2) * 2 + 0] * Lut[LutIndex + 2] +
Buffer[(InOffs + 3) * 2 + 0] * Lut[LutIndex + 3];
int Sample1 = Buffer[(InOffs + 0) * 2 + 1] * Lut[LutIndex + 0] +
Buffer[(InOffs + 1) * 2 + 1] * Lut[LutIndex + 1] +
Buffer[(InOffs + 2) * 2 + 1] * Lut[LutIndex + 2] +
Buffer[(InOffs + 3) * 2 + 1] * Lut[LutIndex + 3];
int NewOffset = FracPart + Step;
InOffs += NewOffset >> 15;
FracPart = NewOffset & 0x7fff;
Output[OutOffs + 0] = Sample0 >> 15;
Output[OutOffs + 1] = Sample1 >> 15;
}
return Output;
}
}
}

View file

@ -1,15 +1,15 @@
namespace Ryujinx.HLE.OsHle.Services.Aud namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{ {
struct UpdateDataHeader struct UpdateDataHeader
{ {
public int Revision; public int Revision;
public int BehaviorSize; public int BehaviorSize;
public int MemoryPoolsSize; public int MemoryPoolSize;
public int VoicesSize; public int VoiceSize;
public int VoiceResourceSize; public int VoiceResourceSize;
public int EffectsSize; public int EffectSize;
public int MixesSize; public int MixeSize;
public int SinksSize; public int SinkSize;
public int PerformanceManagerSize; public int PerformanceManagerSize;
public int Unknown24; public int Unknown24;
public int Unknown28; public int Unknown28;

View file

@ -0,0 +1,10 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x70, Pack = 1)]
struct VoiceChannelResourceIn
{
//???
}
}

View file

@ -0,0 +1,188 @@
using ChocolArm64.Memory;
using Ryujinx.Audio.Adpcm;
using System;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
class VoiceContext
{
private bool Acquired;
private bool BufferReload;
private int ResamplerFracPart;
private int BufferIndex;
private int Offset;
public int SampleRate;
public int ChannelsCount;
public float Volume;
public PlayState PlayState;
public SampleFormat SampleFormat;
public AdpcmDecoderContext AdpcmCtx;
public WaveBuffer[] WaveBuffers;
public VoiceOut OutStatus;
private int[] Samples;
public bool Playing => Acquired && PlayState == PlayState.Playing;
public VoiceContext()
{
WaveBuffers = new WaveBuffer[4];
}
public void SetAcquireState(bool NewState)
{
if (Acquired && !NewState)
{
//Release.
Reset();
}
Acquired = NewState;
}
private void Reset()
{
BufferReload = true;
BufferIndex = 0;
Offset = 0;
OutStatus.PlayedSamplesCount = 0;
OutStatus.PlayedWaveBuffersCount = 0;
OutStatus.VoiceDropsCount = 0;
}
public int[] GetBufferData(AMemory Memory, int MaxSamples, out int SamplesCount)
{
if (!Playing)
{
SamplesCount = 0;
return null;
}
if (BufferReload)
{
BufferReload = false;
UpdateBuffer(Memory);
}
WaveBuffer Wb = WaveBuffers[BufferIndex];
int MaxSize = Samples.Length - Offset;
int Size = MaxSamples * AudioConsts.HostChannelsCount;
if (Size > MaxSize)
{
Size = MaxSize;
}
int[] Output = new int[Size];
Array.Copy(Samples, Offset, Output, 0, Size);
SamplesCount = Size / AudioConsts.HostChannelsCount;
OutStatus.PlayedSamplesCount += SamplesCount;
Offset += Size;
if (Offset == Samples.Length)
{
Offset = 0;
if (Wb.Looping == 0)
{
SetBufferIndex((BufferIndex + 1) & 3);
}
OutStatus.PlayedWaveBuffersCount++;
if (Wb.LastBuffer != 0)
{
PlayState = PlayState.Paused;
}
}
return Output;
}
private void UpdateBuffer(AMemory Memory)
{
//TODO: Implement conversion for formats other
//than interleaved stereo (2 channels).
//As of now, it assumes that HostChannelsCount == 2.
WaveBuffer Wb = WaveBuffers[BufferIndex];
if (SampleFormat == SampleFormat.PcmInt16)
{
int SamplesCount = (int)(Wb.Size / (sizeof(short) * ChannelsCount));
Samples = new int[SamplesCount * AudioConsts.HostChannelsCount];
if (ChannelsCount == 1)
{
for (int Index = 0; Index < SamplesCount; Index++)
{
short Sample = Memory.ReadInt16(Wb.Position + Index * 2);
Samples[Index * 2 + 0] = Sample;
Samples[Index * 2 + 1] = Sample;
}
}
else
{
for (int Index = 0; Index < SamplesCount * 2; Index++)
{
Samples[Index] = Memory.ReadInt16(Wb.Position + Index * 2);
}
}
}
else if (SampleFormat == SampleFormat.Adpcm)
{
byte[] Buffer = Memory.ReadBytes(Wb.Position, Wb.Size);
Samples = AdpcmDecoder.Decode(Buffer, AdpcmCtx);
}
else
{
throw new InvalidOperationException();
}
if (SampleRate != AudioConsts.HostSampleRate)
{
//TODO: We should keep the frames being discarded (see the 4 below)
//on a buffer and include it on the next samples buffer, to allow
//the resampler to do seamless interpolation between wave buffers.
int SamplesCount = Samples.Length / AudioConsts.HostChannelsCount;
SamplesCount = Math.Max(SamplesCount - 4, 0);
Samples = Resampler.Resample2Ch(
Samples,
SampleRate,
AudioConsts.HostSampleRate,
SamplesCount,
ref ResamplerFracPart);
}
}
public void SetBufferIndex(int Index)
{
BufferIndex = Index & 3;
BufferReload = true;
}
}
}

View file

@ -0,0 +1,49 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x170, Pack = 1)]
struct VoiceIn
{
public int VoiceSlot;
public int NodeId;
public byte FirstUpdate;
public byte Acquired;
public PlayState PlayState;
public SampleFormat SampleFormat;
public int SampleRate;
public int Priority;
public int Unknown14;
public int ChannelsCount;
public float Pitch;
public float Volume;
public BiquadFilter BiquadFilter0;
public BiquadFilter BiquadFilter1;
public int AppendedWaveBuffersCount;
public int BaseWaveBufferIndex;
public int Unknown44;
public long AdpcmCoeffsPosition;
public long AdpcmCoeffsSize;
public int VoiceDestination;
public int Padding;
public WaveBuffer WaveBuffer0;
public WaveBuffer WaveBuffer1;
public WaveBuffer WaveBuffer2;
public WaveBuffer WaveBuffer3;
}
}

View file

@ -0,0 +1,12 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x10, Pack = 4)]
struct VoiceOut
{
public long PlayedSamplesCount;
public int PlayedWaveBuffersCount;
public int VoiceDropsCount; //?
}
}

View file

@ -0,0 +1,20 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer
{
[StructLayout(LayoutKind.Sequential, Size = 0x38, Pack = 1)]
struct WaveBuffer
{
public long Position;
public long Size;
public int FirstSampleOffset;
public int LastSampleOffset;
public byte Looping;
public byte LastBuffer;
public short Unknown1A;
public int Unknown1C;
public long AdpcmLoopContextPosition;
public long AdpcmLoopContextSize;
public long Unknown30;
}
}

View file

@ -8,14 +8,14 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
public int SampleRate; public int SampleRate;
public int SampleCount; public int SampleCount;
public int Unknown8; public int Unknown8;
public int UnknownC; public int MixCount;
public int VoiceCount; public int VoiceCount;
public int SinkCount; public int SinkCount;
public int EffectCount; public int EffectCount;
public int Unknown1C; public int PerformanceManagerCount;
public int Unknown20; public int VoiceDropEnable;
public int SplitterCount; public int SplitterCount;
public int Unknown28; public int SplitterDestinationDataCount;
public int Unknown2C; public int Unknown2C;
public int Revision; public int Revision;
} }

View file

@ -3,6 +3,7 @@ using Ryujinx.Audio;
using Ryujinx.HLE.Logging; using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Handles; using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc; using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.Services.Aud.AudioOut;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -154,13 +155,13 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
IAalOutput AudioOut = Context.Ns.AudioOut; IAalOutput AudioOut = Context.Ns.AudioOut;
int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback, out AudioFormat Format); int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback);
MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track)); MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track));
Context.ResponseData.Write(SampleRate); Context.ResponseData.Write(SampleRate);
Context.ResponseData.Write(Channels); Context.ResponseData.Write(Channels);
Context.ResponseData.Write((int)Format); Context.ResponseData.Write((int)SampleFormat.PcmInt16);
Context.ResponseData.Write((int)PlaybackState.Stopped); Context.ResponseData.Write((int)PlaybackState.Stopped);
return 0; return 0;

View file

@ -1,136 +0,0 @@
using ChocolArm64.Memory;
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.OsHle.Services.Aud
{
class IAudioRenderer : IpcService, IDisposable
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
private KEvent UpdateEvent;
private AudioRendererParameter Params;
public IAudioRenderer(AudioRendererParameter Params)
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 4, RequestUpdateAudioRenderer },
{ 5, StartAudioRenderer },
{ 6, StopAudioRenderer },
{ 7, QuerySystemEvent }
};
UpdateEvent = new KEvent();
this.Params = Params;
}
public long RequestUpdateAudioRenderer(ServiceCtx Context)
{
long OutputPosition = Context.Request.ReceiveBuff[0].Position;
long OutputSize = Context.Request.ReceiveBuff[0].Size;
AMemoryHelper.FillWithZeros(Context.Memory, OutputPosition, (int)OutputSize);
long InputPosition = Context.Request.SendBuff[0].Position;
UpdateDataHeader InputDataHeader = AMemoryHelper.Read<UpdateDataHeader>(Context.Memory, InputPosition);
UpdateDataHeader OutputDataHeader = new UpdateDataHeader();
int UpdateHeaderSize = Marshal.SizeOf<UpdateDataHeader>();
OutputDataHeader.Revision = Params.Revision;
OutputDataHeader.BehaviorSize = 0xb0;
OutputDataHeader.MemoryPoolsSize = (Params.EffectCount + Params.VoiceCount * 4) * 0x10;
OutputDataHeader.VoicesSize = Params.VoiceCount * 0x10;
OutputDataHeader.EffectsSize = Params.EffectCount * 0x10;
OutputDataHeader.SinksSize = Params.SinkCount * 0x20;
OutputDataHeader.PerformanceManagerSize = 0x10;
OutputDataHeader.TotalSize = UpdateHeaderSize +
OutputDataHeader.BehaviorSize +
OutputDataHeader.MemoryPoolsSize +
OutputDataHeader.VoicesSize +
OutputDataHeader.EffectsSize +
OutputDataHeader.SinksSize +
OutputDataHeader.PerformanceManagerSize;
AMemoryHelper.Write(Context.Memory, OutputPosition, OutputDataHeader);
int InMemoryPoolOffset = UpdateHeaderSize + InputDataHeader.BehaviorSize;
int OutMemoryPoolOffset = UpdateHeaderSize;
for (int Offset = 0; Offset < OutputDataHeader.MemoryPoolsSize; Offset += 0x10, InMemoryPoolOffset += 0x20)
{
MemoryPoolState PoolState = (MemoryPoolState)Context.Memory.ReadInt32(InputPosition + InMemoryPoolOffset + 0x10);
//TODO: Figure out what the other values does.
if (PoolState == MemoryPoolState.RequestAttach)
{
Context.Memory.WriteInt32(OutputPosition + OutMemoryPoolOffset + Offset, (int)MemoryPoolState.Attached);
}
else if (PoolState == MemoryPoolState.RequestDetach)
{
Context.Memory.WriteInt32(OutputPosition + OutMemoryPoolOffset + Offset, (int)MemoryPoolState.Detached);
}
}
int OutVoicesOffset = OutMemoryPoolOffset + OutputDataHeader.MemoryPoolsSize;
for (int Offset = 0; Offset < OutputDataHeader.VoicesSize; Offset += 0x10)
{
Context.Memory.WriteInt32(OutputPosition + OutVoicesOffset + Offset + 8, (int)VoicePlaybackState.Finished);
}
//TODO: We shouldn't be signaling this here.
UpdateEvent.WaitEvent.Set();
return 0;
}
public long StartAudioRenderer(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
return 0;
}
public long StopAudioRenderer(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
return 0;
}
public long QuerySystemEvent(ServiceCtx Context)
{
int Handle = Context.Process.HandleTable.OpenHandle(UpdateEvent);
Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
return 0;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing)
{
UpdateEvent.Dispose();
}
}
}
}

View file

@ -1,7 +1,11 @@
using Ryujinx.Audio;
using Ryujinx.HLE.Logging; using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Ipc; using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer;
using Ryujinx.HLE.OsHle.Utilities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using static Ryujinx.HLE.OsHle.ErrorCode;
namespace Ryujinx.HLE.OsHle.Services.Aud namespace Ryujinx.HLE.OsHle.Services.Aud
{ {
@ -12,6 +16,10 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
('V' << 16) | ('V' << 16) |
('0' << 24); ('0' << 24);
private const int Rev = 4;
public const int RevMagic = Rev0Magic + (Rev << 24);
private Dictionary<int, ServiceProcessRequest> m_Commands; private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands; public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
@ -28,76 +36,69 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
public long OpenAudioRenderer(ServiceCtx Context) public long OpenAudioRenderer(ServiceCtx Context)
{ {
//Same buffer as GetAudioRendererWorkBufferSize is receive here. IAalOutput AudioOut = Context.Ns.AudioOut;
AudioRendererParameter Params = new AudioRendererParameter(); AudioRendererParameter Params = GetAudioRendererParameter(Context);
Params.SampleRate = Context.RequestData.ReadInt32(); MakeObject(Context, new IAudioRenderer(Context.Memory, AudioOut, Params));
Params.SampleCount = Context.RequestData.ReadInt32();
Params.Unknown8 = Context.RequestData.ReadInt32();
Params.UnknownC = Context.RequestData.ReadInt32();
Params.VoiceCount = Context.RequestData.ReadInt32();
Params.SinkCount = Context.RequestData.ReadInt32();
Params.EffectCount = Context.RequestData.ReadInt32();
Params.Unknown1C = Context.RequestData.ReadInt32();
Params.Unknown20 = Context.RequestData.ReadInt32();
Params.SplitterCount = Context.RequestData.ReadInt32();
Params.Unknown28 = Context.RequestData.ReadInt32();
Params.Unknown2C = Context.RequestData.ReadInt32();
Params.Revision = Context.RequestData.ReadInt32();
MakeObject(Context, new IAudioRenderer(Params));
return 0; return 0;
} }
public long GetAudioRendererWorkBufferSize(ServiceCtx Context) public long GetAudioRendererWorkBufferSize(ServiceCtx Context)
{ {
long SampleRate = Context.RequestData.ReadUInt32(); AudioRendererParameter Params = GetAudioRendererParameter(Context);
long Unknown4 = Context.RequestData.ReadUInt32();
long Unknown8 = Context.RequestData.ReadUInt32();
long UnknownC = Context.RequestData.ReadUInt32();
long Unknown10 = Context.RequestData.ReadUInt32(); //VoiceCount
long Unknown14 = Context.RequestData.ReadUInt32(); //SinkCount
long Unknown18 = Context.RequestData.ReadUInt32(); //EffectCount
long Unknown1c = Context.RequestData.ReadUInt32(); //Boolean
long Unknown20 = Context.RequestData.ReadUInt32(); //Not used here in FW3.0.1 - Boolean
long Unknown24 = Context.RequestData.ReadUInt32();
long Unknown28 = Context.RequestData.ReadUInt32(); //SplitterCount
long Unknown2c = Context.RequestData.ReadUInt32(); //Not used here in FW3.0.1
int RevMagic = Context.RequestData.ReadInt32();
int Version = (RevMagic - Rev0Magic) >> 24; int Revision = (Params.Revision - Rev0Magic) >> 24;
if (Version <= 3) //REV3 Max is supported if (Revision <= Rev)
{ {
long Size = RoundUp(Unknown8 * 4, 64); bool IsSplitterSupported = Revision >= 3;
Size += (UnknownC << 10);
Size += (UnknownC + 1) * 0x940;
Size += Unknown10 * 0x3F0;
Size += RoundUp((UnknownC + 1) * 8, 16);
Size += RoundUp(Unknown10 * 8, 16);
Size += RoundUp((0x3C0 * (Unknown14 + UnknownC) + 4 * Unknown4) * (Unknown8 + 6), 64);
Size += 0x2C0 * (Unknown14 + UnknownC) + 0x30 * (Unknown18 + (4 * Unknown10)) + 0x50;
if (Version >= 3) //IsSplitterSupported long Size;
Size = IntUtils.AlignUp(Params.Unknown8 * 4, 64);
Size += Params.MixCount * 0x400;
Size += (Params.MixCount + 1) * 0x940;
Size += Params.VoiceCount * 0x3F0;
Size += IntUtils.AlignUp((Params.MixCount + 1) * 8, 16);
Size += IntUtils.AlignUp(Params.VoiceCount * 8, 16);
Size += IntUtils.AlignUp(
((Params.SinkCount + Params.MixCount) * 0x3C0 + Params.SampleCount * 4) *
(Params.Unknown8 + 6), 64);
Size += (Params.SinkCount + Params.MixCount) * 0x2C0;
Size += (Params.EffectCount + Params.VoiceCount * 4) * 0x30 + 0x50;
if (IsSplitterSupported)
{ {
Size += RoundUp((NodeStatesGetWorkBufferSize((int)UnknownC + 1) + EdgeMatrixGetWorkBufferSize((int)UnknownC + 1)), 16); Size += IntUtils.AlignUp((
Size += 0xE0 * Unknown28 + 0x20 * Unknown24 + RoundUp(Unknown28 * 4, 16); NodeStatesGetWorkBufferSize(Params.MixCount + 1) +
EdgeMatrixGetWorkBufferSize(Params.MixCount + 1)), 16);
Size += Params.SplitterDestinationDataCount * 0xE0;
Size += Params.SplitterCount * 0x20;
Size += IntUtils.AlignUp(Params.SplitterDestinationDataCount * 4, 16);
} }
Size = 0x4C0 * Unknown18 + RoundUp(Size, 64) + 0x170 * Unknown14 + ((Unknown10 << 8) | 0x40); Size = Params.EffectCount * 0x4C0 +
Params.SinkCount * 0x170 +
Params.VoiceCount * 0x100 +
IntUtils.AlignUp(Size, 64) + 0x40;
if (Unknown1c >= 1) if (Params.PerformanceManagerCount >= 1)
{ {
Size += ((((Unknown18 + Unknown14 + Unknown10 + UnknownC + 1) * 16) + 0x658) * (Unknown1c + 1) + 0x13F) & ~0x3FL; Size += (((Params.EffectCount +
Params.SinkCount +
Params.VoiceCount +
Params.MixCount + 1) * 16 + 0x658) *
(Params.PerformanceManagerCount + 1) + 0x13F) & ~0x3FL;
} }
long WorkBufferSize = (Size + 0x1907D) & ~0xFFFL; Size = (Size + 0x1907D) & ~0xFFFL;
Context.ResponseData.Write(WorkBufferSize); Context.ResponseData.Write(Size);
Context.Ns.Log.PrintDebug(LogClass.ServiceAudio, $"WorkBufferSize is 0x{WorkBufferSize:x16}."); Context.Ns.Log.PrintDebug(LogClass.ServiceAudio, $"WorkBufferSize is 0x{Size:x16}.");
return 0; return 0;
} }
@ -105,20 +106,36 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
{ {
Context.ResponseData.Write(0L); Context.ResponseData.Write(0L);
Context.Ns.Log.PrintError(LogClass.ServiceAudio, $"Library Revision 0x{RevMagic:x8} is not supported!"); Context.Ns.Log.PrintWarning(LogClass.ServiceAudio, $"Library Revision 0x{Params.Revision:x8} is not supported!");
return 0x499; return MakeError(ErrorModule.Audio, AudErr.UnsupportedRevision);
} }
} }
private static long RoundUp(long Value, int Size) private AudioRendererParameter GetAudioRendererParameter(ServiceCtx Context)
{ {
return (Value + (Size - 1)) & ~((long)Size - 1); AudioRendererParameter Params = new AudioRendererParameter();
Params.SampleRate = Context.RequestData.ReadInt32();
Params.SampleCount = Context.RequestData.ReadInt32();
Params.Unknown8 = Context.RequestData.ReadInt32();
Params.MixCount = Context.RequestData.ReadInt32();
Params.VoiceCount = Context.RequestData.ReadInt32();
Params.SinkCount = Context.RequestData.ReadInt32();
Params.EffectCount = Context.RequestData.ReadInt32();
Params.PerformanceManagerCount = Context.RequestData.ReadInt32();
Params.VoiceDropEnable = Context.RequestData.ReadInt32();
Params.SplitterCount = Context.RequestData.ReadInt32();
Params.SplitterDestinationDataCount = Context.RequestData.ReadInt32();
Params.Unknown2C = Context.RequestData.ReadInt32();
Params.Revision = Context.RequestData.ReadInt32();
return Params;
} }
private static int NodeStatesGetWorkBufferSize(int Value) private static int NodeStatesGetWorkBufferSize(int Value)
{ {
int Result = (int)RoundUp(Value, 64); int Result = IntUtils.AlignUp(Value, 64);
if (Result < 0) if (Result < 0)
{ {
@ -130,7 +147,7 @@ namespace Ryujinx.HLE.OsHle.Services.Aud
private static int EdgeMatrixGetWorkBufferSize(int Value) private static int EdgeMatrixGetWorkBufferSize(int Value)
{ {
int Result = (int)RoundUp(Value * Value, 64); int Result = IntUtils.AlignUp(Value * Value, 64);
if (Result < 0) if (Result < 0)
{ {

View file

@ -1,12 +1,12 @@
namespace Ryujinx.Audio namespace Ryujinx.HLE.OsHle.Services.Aud
{ {
public enum AudioFormat enum SampleFormat : byte
{ {
Invalid = 0, Invalid = 0,
PcmInt8 = 1, PcmInt8 = 1,
PcmInt16 = 2, PcmInt16 = 2,
PcmImt24 = 3, PcmInt24 = 3,
PcmImt32 = 4, PcmInt32 = 4,
PcmFloat = 5, PcmFloat = 5,
Adpcm = 6 Adpcm = 6
} }

View file

@ -1,9 +0,0 @@
namespace Ryujinx.HLE.OsHle.Services.Aud
{
enum VoicePlaybackState : int
{
Playing = 0,
Finished = 1,
Paused = 2
}
}

View file

@ -0,0 +1,21 @@
using Ryujinx.HLE.OsHle.Ipc;
using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Bcat
{
class IBcatService : IpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IBcatService()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
//...
};
}
}
}

View file

@ -0,0 +1,21 @@
using Ryujinx.HLE.OsHle.Ipc;
using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Bcat
{
class IDeliveryCacheStorageService : IpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IDeliveryCacheStorageService()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
//...
};
}
}
}

View file

@ -0,0 +1,39 @@
using Ryujinx.HLE.OsHle.Ipc;
using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Bcat
{
class IServiceCreator : IpcService
{
private Dictionary<int, ServiceProcessRequest> m_Commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IServiceCreator()
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 0, CreateBcatService },
{ 1, CreateDeliveryCacheStorageService }
};
}
public long CreateBcatService(ServiceCtx Context)
{
long Id = Context.RequestData.ReadInt64();
MakeObject(Context, new IBcatService());
return 0;
}
public long CreateDeliveryCacheStorageService(ServiceCtx Context)
{
long Id = Context.RequestData.ReadInt64();
MakeObject(Context, new IDeliveryCacheStorageService());
return 0;
}
}
}

View file

@ -35,7 +35,7 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
{ 10, Commit }, { 10, Commit },
{ 11, GetFreeSpaceSize }, { 11, GetFreeSpaceSize },
{ 12, GetTotalSpaceSize }, { 12, GetTotalSpaceSize },
//{ 13, CleanDirectoryRecursively }, { 13, CleanDirectoryRecursively },
//{ 14, GetFileTimeStampRaw } //{ 14, GetFileTimeStampRaw }
}; };
@ -46,8 +46,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long CreateFile(ServiceCtx Context) public long CreateFile(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
long Mode = Context.RequestData.ReadInt64(); long Mode = Context.RequestData.ReadInt64();
@ -80,8 +78,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long DeleteFile(ServiceCtx Context) public long DeleteFile(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
string FileName = Context.Ns.VFs.GetFullPath(Path, Name); string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
@ -103,8 +99,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long CreateDirectory(ServiceCtx Context) public long CreateDirectory(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
string DirName = Context.Ns.VFs.GetFullPath(Path, Name); string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
@ -141,8 +135,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
private long DeleteDirectory(ServiceCtx Context, bool Recursive) private long DeleteDirectory(ServiceCtx Context, bool Recursive)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
string DirName = Context.Ns.VFs.GetFullPath(Path, Name); string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
@ -220,8 +212,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long GetEntryType(ServiceCtx Context) public long GetEntryType(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
string FileName = Context.Ns.VFs.GetFullPath(Path, Name); string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
@ -246,8 +236,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long OpenFile(ServiceCtx Context) public long OpenFile(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
int FilterFlags = Context.RequestData.ReadInt32(); int FilterFlags = Context.RequestData.ReadInt32();
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
@ -282,8 +270,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long OpenDirectory(ServiceCtx Context) public long OpenDirectory(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
int FilterFlags = Context.RequestData.ReadInt32(); int FilterFlags = Context.RequestData.ReadInt32();
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
@ -321,8 +307,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long GetFreeSpaceSize(ServiceCtx Context) public long GetFreeSpaceSize(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace); Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
@ -332,8 +316,6 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
public long GetTotalSpaceSize(ServiceCtx Context) public long GetTotalSpaceSize(ServiceCtx Context)
{ {
long Position = Context.Request.PtrBuff[0].Position;
string Name = ReadUtf8String(Context); string Name = ReadUtf8String(Context);
Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize); Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);
@ -341,6 +323,37 @@ namespace Ryujinx.HLE.OsHle.Services.FspSrv
return 0; return 0;
} }
public long CleanDirectoryRecursively(ServiceCtx Context)
{
string Name = ReadUtf8String(Context);
string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
if (!Directory.Exists(DirName))
{
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
}
if (IsPathAlreadyInUse(DirName))
{
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
}
foreach (string Entry in Directory.EnumerateFileSystemEntries(DirName))
{
if (Directory.Exists(Entry))
{
Directory.Delete(Entry, true);
}
else if (File.Exists(Entry))
{
File.Delete(Entry);
}
}
return 0;
}
private bool IsPathAlreadyInUse(string Path) private bool IsPathAlreadyInUse(string Path)
{ {
lock (OpenPaths) lock (OpenPaths)

View file

@ -1,6 +1,11 @@
using Ryujinx.HLE.Logging; using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Ipc; using Ryujinx.HLE.OsHle.Ipc;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace Ryujinx.HLE.OsHle.Services.Nifm namespace Ryujinx.HLE.OsHle.Services.Nifm
{ {
@ -14,10 +19,13 @@ namespace Ryujinx.HLE.OsHle.Services.Nifm
{ {
m_Commands = new Dictionary<int, ServiceProcessRequest>() m_Commands = new Dictionary<int, ServiceProcessRequest>()
{ {
{ 4, CreateRequest } { 4, CreateRequest },
{ 12, GetCurrentIpAddress }
}; };
} }
public const int NoInternetConnection = 0x2586e;
//CreateRequest(i32) //CreateRequest(i32)
public long CreateRequest(ServiceCtx Context) public long CreateRequest(ServiceCtx Context)
{ {
@ -29,5 +37,22 @@ namespace Ryujinx.HLE.OsHle.Services.Nifm
return 0; return 0;
} }
public long GetCurrentIpAddress(ServiceCtx Context)
{
if (!NetworkInterface.GetIsNetworkAvailable())
{
return NoInternetConnection;
}
IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
IPAddress Address = Host.AddressList.FirstOrDefault(A => A.AddressFamily == AddressFamily.InterNetwork);
Context.ResponseData.Write(BitConverter.ToUInt32(Address.GetAddressBytes()));
Context.Ns.Log.PrintInfo(LogClass.ServiceNifm, $"Console's local IP is {Address.ToString()}");
return 0;
}
} }
} }

View file

@ -23,11 +23,11 @@ namespace Ryujinx.HLE.OsHle.Services.Nv
private static Dictionary<string, IoctlProcessor> IoctlProcessors = private static Dictionary<string, IoctlProcessor> IoctlProcessors =
new Dictionary<string, IoctlProcessor>() new Dictionary<string, IoctlProcessor>()
{ {
{ "/dev/nvhost-as-gpu", ProcessIoctlNvGpuAS }, { "/dev/nvhost-as-gpu", ProcessIoctlNvGpuAS },
{ "/dev/nvhost-ctrl", ProcessIoctlNvHostCtrl }, { "/dev/nvhost-ctrl", ProcessIoctlNvHostCtrl },
{ "/dev/nvhost-ctrl-gpu", ProcessIoctlNvGpuGpu }, { "/dev/nvhost-ctrl-gpu", ProcessIoctlNvGpuGpu },
{ "/dev/nvhost-gpu", ProcessIoctlNvHostChannel }, { "/dev/nvhost-gpu", ProcessIoctlNvHostGpu },
{ "/dev/nvmap", ProcessIoctlNvMap } { "/dev/nvmap", ProcessIoctlNvMap }
}; };
public static GlobalStateTable Fds { get; private set; } public static GlobalStateTable Fds { get; private set; }
@ -44,6 +44,7 @@ namespace Ryujinx.HLE.OsHle.Services.Nv
{ 3, Initialize }, { 3, Initialize },
{ 4, QueryEvent }, { 4, QueryEvent },
{ 8, SetClientPid }, { 8, SetClientPid },
{ 11, Ioctl },
{ 13, FinishInitialize } { 13, FinishInitialize }
}; };
@ -162,9 +163,9 @@ namespace Ryujinx.HLE.OsHle.Services.Nv
return ProcessIoctl(Context, Cmd, NvGpuGpuIoctl.ProcessIoctl); return ProcessIoctl(Context, Cmd, NvGpuGpuIoctl.ProcessIoctl);
} }
private static int ProcessIoctlNvHostChannel(ServiceCtx Context, int Cmd) private static int ProcessIoctlNvHostGpu(ServiceCtx Context, int Cmd)
{ {
return ProcessIoctl(Context, Cmd, NvHostChannelIoctl.ProcessIoctl); return ProcessIoctl(Context, Cmd, NvHostChannelIoctl.ProcessIoctlGpu);
} }
private static int ProcessIoctlNvMap(ServiceCtx Context, int Cmd) private static int ProcessIoctlNvMap(ServiceCtx Context, int Cmd)
@ -207,6 +208,8 @@ namespace Ryujinx.HLE.OsHle.Services.Nv
NvGpuASIoctl.UnloadProcess(Process); NvGpuASIoctl.UnloadProcess(Process);
NvHostChannelIoctl.UnloadProcess(Process);
NvHostCtrlIoctl.UnloadProcess(Process); NvHostCtrlIoctl.UnloadProcess(Process);
NvMapIoctl.UnloadProcess(Process); NvMapIoctl.UnloadProcess(Process);

View file

@ -0,0 +1,7 @@
namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
{
class NvChannel
{
public int Timeout;
}
}

View file

@ -0,0 +1,7 @@
namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
{
enum NvChannelName
{
Gpu
}
}

View file

@ -3,23 +3,50 @@ using Ryujinx.HLE.Gpu.Memory;
using Ryujinx.HLE.Logging; using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Services.Nv.NvGpuAS; using Ryujinx.HLE.OsHle.Services.Nv.NvGpuAS;
using System; using System;
using System.Collections.Concurrent;
namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
{ {
class NvHostChannelIoctl class NvHostChannelIoctl
{ {
public static int ProcessIoctl(ServiceCtx Context, int Cmd) private class ChannelsPerProcess
{
public ConcurrentDictionary<NvChannelName, NvChannel> Channels { get; private set; }
public ChannelsPerProcess()
{
Channels = new ConcurrentDictionary<NvChannelName, NvChannel>();
Channels.TryAdd(NvChannelName.Gpu, new NvChannel());
}
}
private static ConcurrentDictionary<Process, ChannelsPerProcess> Channels;
static NvHostChannelIoctl()
{
Channels = new ConcurrentDictionary<Process, ChannelsPerProcess>();
}
public static int ProcessIoctlGpu(ServiceCtx Context, int Cmd)
{
return ProcessIoctl(Context, NvChannelName.Gpu, Cmd);
}
public static int ProcessIoctl(ServiceCtx Context, NvChannelName Channel, int Cmd)
{ {
switch (Cmd & 0xffff) switch (Cmd & 0xffff)
{ {
case 0x4714: return SetUserData (Context); case 0x4714: return SetUserData (Context);
case 0x4801: return SetNvMap (Context); case 0x4801: return SetNvMap (Context);
case 0x4808: return SubmitGpfifo (Context); case 0x4803: return SetTimeout (Context, Channel);
case 0x4809: return AllocObjCtx (Context); case 0x4808: return SubmitGpfifo (Context);
case 0x480b: return ZcullBind (Context); case 0x4809: return AllocObjCtx (Context);
case 0x480c: return SetErrorNotifier(Context); case 0x480b: return ZcullBind (Context);
case 0x480d: return SetPriority (Context); case 0x480c: return SetErrorNotifier (Context);
case 0x481a: return AllocGpfifoEx2 (Context); case 0x480d: return SetPriority (Context);
case 0x481a: return AllocGpfifoEx2 (Context);
case 0x481b: return KickoffPbWithAttr(Context);
} }
throw new NotImplementedException(Cmd.ToString("x8")); throw new NotImplementedException(Cmd.ToString("x8"));
@ -45,6 +72,15 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
return NvResult.Success; return NvResult.Success;
} }
private static int SetTimeout(ServiceCtx Context, NvChannelName Channel)
{
long InputPosition = Context.Request.GetBufferType0x21().Position;
GetChannel(Context, Channel).Timeout = Context.Memory.ReadInt32(InputPosition);
return NvResult.Success;
}
private static int SubmitGpfifo(ServiceCtx Context) private static int SubmitGpfifo(ServiceCtx Context)
{ {
long InputPosition = Context.Request.GetBufferType0x21().Position; long InputPosition = Context.Request.GetBufferType0x21().Position;
@ -58,15 +94,7 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
{ {
long Gpfifo = Context.Memory.ReadInt64(InputPosition + 0x18 + Index * 8); long Gpfifo = Context.Memory.ReadInt64(InputPosition + 0x18 + Index * 8);
long VA = Gpfifo & 0xff_ffff_ffff; PushGpfifo(Context, Vmm, Gpfifo);
int Size = (int)(Gpfifo >> 40) & 0x7ffffc;
byte[] Data = Vmm.ReadBytes(VA, Size);
NvGpuPBEntry[] PushBuffer = NvGpuPushBuffer.Decode(Data);
Context.Ns.Gpu.Fifo.PushBuffer(Vmm, PushBuffer);
} }
Args.SyncptId = 0; Args.SyncptId = 0;
@ -126,5 +154,57 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
return NvResult.Success; return NvResult.Success;
} }
private static int KickoffPbWithAttr(ServiceCtx Context)
{
long InputPosition = Context.Request.GetBufferType0x21().Position;
long OutputPosition = Context.Request.GetBufferType0x22().Position;
NvHostChannelSubmitGpfifo Args = AMemoryHelper.Read<NvHostChannelSubmitGpfifo>(Context.Memory, InputPosition);
NvGpuVmm Vmm = NvGpuASIoctl.GetVmm(Context);
for (int Index = 0; Index < Args.NumEntries; Index++)
{
long Gpfifo = Context.Memory.ReadInt64(Args.Address + Index * 8);
PushGpfifo(Context, Vmm, Gpfifo);
}
Args.SyncptId = 0;
Args.SyncptValue = 0;
AMemoryHelper.Write(Context.Memory, OutputPosition, Args);
return NvResult.Success;
}
private static void PushGpfifo(ServiceCtx Context, NvGpuVmm Vmm, long Gpfifo)
{
long VA = Gpfifo & 0xff_ffff_ffff;
int Size = (int)(Gpfifo >> 40) & 0x7ffffc;
byte[] Data = Vmm.ReadBytes(VA, Size);
NvGpuPBEntry[] PushBuffer = NvGpuPushBuffer.Decode(Data);
Context.Ns.Gpu.Fifo.PushBuffer(Vmm, PushBuffer);
}
public static NvChannel GetChannel(ServiceCtx Context, NvChannelName Channel)
{
ChannelsPerProcess Cpp = Channels.GetOrAdd(Context.Process, (Key) =>
{
return new ChannelsPerProcess();
});
return Cpp.Channels[Channel];
}
public static void UnloadProcess(Process Process)
{
Channels.TryRemove(Process, out _);
}
} }
} }

View file

@ -2,7 +2,7 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvHostChannel
{ {
struct NvHostChannelSubmitGpfifo struct NvHostChannelSubmitGpfifo
{ {
public long Gpfifo; public long Address;
public int NumEntries; public int NumEntries;
public int Flags; public int Flags;
public int SyncptId; public int SyncptId;

View file

@ -48,7 +48,7 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvMap
return NvResult.InvalidInput; return NvResult.InvalidInput;
} }
int Size = IntUtils.RoundUp(Args.Size, NvGpuVmm.PageSize); int Size = IntUtils.AlignUp(Args.Size, NvGpuVmm.PageSize);
Args.Handle = AddNvMap(Context, new NvMapHandle(Size)); Args.Handle = AddNvMap(Context, new NvMapHandle(Size));
@ -121,7 +121,7 @@ namespace Ryujinx.HLE.OsHle.Services.Nv.NvMap
Map.Align = Args.Align; Map.Align = Args.Align;
Map.Kind = (byte)Args.Kind; Map.Kind = (byte)Args.Kind;
int Size = IntUtils.RoundUp(Map.Size, NvGpuVmm.PageSize); int Size = IntUtils.AlignUp(Map.Size, NvGpuVmm.PageSize);
long Address = Args.Address; long Address = Args.Address;

Some files were not shown because too many files have changed in this diff Show more