private static LinkedListNode <INode> ReplaceLdgWithLdc(LinkedListNode <INode> node, ShaderConfig config, int storageIndex) { Operation operation = (Operation)node.Value; Operand GetCbufOffset() { Operand addrLow = operation.GetSource(0); Operand baseAddrLow = config.CreateCbuf(0, UbeBaseOffset + storageIndex * StorageDescSize); Operand baseAddrTrunc = Local(); Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment()); Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask); node.List.AddBefore(node, andOp); Operand byteOffset = Local(); Operand wordOffset = Local(); Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc); Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2)); node.List.AddBefore(node, subOp); node.List.AddBefore(node, shrOp); return(wordOffset); } Operand[] sources = new Operand[operation.SourcesCount]; int cbSlot = UbeFirstCbuf + storageIndex; sources[0] = Const(cbSlot); sources[1] = GetCbufOffset(); config.SetUsedConstantBuffer(cbSlot); for (int index = 2; index < operation.SourcesCount; index++) { sources[index] = operation.GetSource(index); } Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources); for (int index = 0; index < operation.SourcesCount; index++) { operation.SetSource(index, null); } LinkedListNode <INode> oldNode = node; node = node.List.AddBefore(node, ldcOp); node.List.Remove(oldNode); return(node); }
private static LinkedListNode <INode> ReplaceGlobalWithStorage(LinkedListNode <INode> node, ShaderConfig config, int storageIndex) { Operation operation = (Operation)node.Value; bool isAtomic = operation.Inst.IsAtomic(); bool isStg16Or8 = operation.Inst == Instruction.StoreGlobal16 || operation.Inst == Instruction.StoreGlobal8; bool isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal || isStg16Or8; config.SetUsedStorageBuffer(storageIndex, isWrite); Operand GetStorageOffset() { Operand addrLow = operation.GetSource(0); Operand baseAddrLow = config.CreateCbuf(0, GetStorageCbOffset(config.Stage, storageIndex)); Operand baseAddrTrunc = Local(); Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment()); Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask); node.List.AddBefore(node, andOp); Operand byteOffset = Local(); Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc); node.List.AddBefore(node, subOp); if (isStg16Or8) { return(byteOffset); } Operand wordOffset = Local(); Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2)); node.List.AddBefore(node, shrOp); return(wordOffset); } Operand[] sources = new Operand[operation.SourcesCount]; sources[0] = Const(storageIndex); sources[1] = GetStorageOffset(); for (int index = 2; index < operation.SourcesCount; index++) { sources[index] = operation.GetSource(index); } Operation storageOp; if (isAtomic) { Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage; storageOp = new Operation(inst, operation.Dest, sources); } else if (operation.Inst == Instruction.LoadGlobal) { storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources); } else { Instruction storeInst = operation.Inst switch { Instruction.StoreGlobal16 => Instruction.StoreStorage16, Instruction.StoreGlobal8 => Instruction.StoreStorage8, _ => Instruction.StoreStorage }; storageOp = new Operation(storeInst, null, sources); } for (int index = 0; index < operation.SourcesCount; index++) { operation.SetSource(index, null); } LinkedListNode <INode> oldNode = node; node = node.List.AddBefore(node, storageOp); node.List.Remove(oldNode); return(node); }