Add a new JIT compiler for CPU code (#693)
* Start of the ARMeilleure project
* Refactoring around the old IRAdapter, now renamed to PreAllocator
* Optimize the LowestBitSet method
* Add CLZ support and fix CLS implementation
* Add missing Equals and GetHashCode overrides on some structs, misc small tweaks
* Implement the ByteSwap IR instruction, and some refactoring on the assembler
* Implement the DivideUI IR instruction and fix 64-bits IDIV
* Correct constant operand type on CSINC
* Move division instructions implementation to InstEmitDiv
* Fix destination type for the ConditionalSelect IR instruction
* Implement UMULH and SMULH, with new IR instructions
* Fix some issues with shift instructions
* Fix constant types for BFM instructions
* Fix up new tests using the new V128 struct
* Update tests
* Move DIV tests to a separate file
* Add support for calls, and some instructions that depends on them
* Start adding support for SIMD & FP types, along with some of the related ARM instructions
* Fix some typos and the divide instruction with FP operands
* Fix wrong method call on Clz_V
* Implement ARM FP & SIMD move instructions, Saddlv_V, and misc. fixes
* Implement SIMD logical instructions and more misc. fixes
* Fix PSRAD x86 instruction encoding, TRN, UABD and UABDL implementations
* Implement float conversion instruction, merge in LDj3SNuD fixes, and some other misc. fixes
* Implement SIMD shift instruction and fix Dup_V
* Add SCVTF and UCVTF (vector, fixed-point) variants to the opcode table
* Fix check with tolerance on tester
* Implement FP & SIMD comparison instructions, and some fixes
* Update FCVT (Scalar) encoding on the table to support the Half-float variants
* Support passing V128 structs, some cleanup on the register allocator, merge LDj3SNuD fixes
* Use old memory access methods, made a start on SIMD memory insts support, some fixes
* Fix float constant passed to functions, save and restore non-volatile XMM registers, other fixes
* Fix arguments count with struct return values, other fixes
* More instructions
* Misc. fixes and integrate LDj3SNuD fixes
* Update tests
* Add a faster linear scan allocator, unwinding support on windows, and other changes
* Update Ryujinx.HLE
* Update Ryujinx.Graphics
* Fix V128 return pointer passing, RCX is clobbered
* Update Ryujinx.Tests
* Update ITimeZoneService
* Stop using GetFunctionPointer as that can't be called from native code, misc. fixes and tweaks
* Use generic GetFunctionPointerForDelegate method and other tweaks
* Some refactoring on the code generator, assert on invalid operations and use a separate enum for intrinsics
* Remove some unused code on the assembler
* Fix REX.W prefix regression on float conversion instructions, add some sort of profiler
* Add hardware capability detection
* Fix regression on Sha1h and revert Fcm** changes
* Add SSE2-only paths on vector extract and insert, some refactoring on the pre-allocator
* Fix silly mistake introduced on last commit on CpuId
* Generate inline stack probes when the stack allocation is too large
* Initial support for the System-V ABI
* Support multiple destination operands
* Fix SSE2 VectorInsert8 path, and other fixes
* Change placement of XMM callee save and restore code to match other compilers
* Rename Dest to Destination and Inst to Instruction
* Fix a regression related to calls and the V128 type
* Add an extra space on comments to match code style
* Some refactoring
* Fix vector insert FP32 SSE2 path
* Port over the ARM32 instructions
* Avoid memory protection races on JIT Cache
* Another fix on VectorInsert FP32 (thanks to LDj3SNuD
* Float operands don't need to use the same register when VEX is supported
* Add a new register allocator, higher quality code for hot code (tier up), and other tweaks
* Some nits, small improvements on the pre allocator
* CpuThreadState is gone
* Allow changing CPU emulators with a config entry
* Add runtime identifiers on the ARMeilleure project
* Allow switching between CPUs through a config entry (pt. 2)
* Change win10-x64 to win-x64 on projects
* Update the Ryujinx project to use ARMeilleure
* Ensure that the selected register is valid on the hybrid allocator
* Allow exiting on returns to 0 (should fix test regression)
* Remove register assignments for most used variables on the hybrid allocator
* Do not use fixed registers as spill temp
* Add missing namespace and remove unneeded using
* Address PR feedback
* Fix types, etc
* Enable AssumeStrictAbiCompliance by default
* Ensure that Spill and Fill don't load or store any more than necessary
2019-08-08 18:56:22 +00:00
|
|
|
|
using ARMeilleure.Memory;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
using Ryujinx.Graphics.Gal;
|
|
|
|
|
using Ryujinx.Graphics.Memory;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Texture
|
|
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
|
public static class ImageUtils
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
|
[Flags]
|
|
|
|
|
private enum TargetBuffer
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2018-09-18 04:30:35 +00:00
|
|
|
|
Color = 1 << 0,
|
|
|
|
|
Depth = 1 << 1,
|
|
|
|
|
Stencil = 1 << 2,
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
|
DepthStencil = Depth | Stencil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct ImageDescriptor
|
|
|
|
|
{
|
|
|
|
|
public int BytesPerPixel { get; private set; }
|
|
|
|
|
public int BlockWidth { get; private set; }
|
|
|
|
|
public int BlockHeight { get; private set; }
|
2019-02-28 01:12:24 +00:00
|
|
|
|
public int BlockDepth { get; private set; }
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2018-09-18 04:30:35 +00:00
|
|
|
|
public TargetBuffer Target { get; private set; }
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public ImageDescriptor(int bytesPerPixel, int blockWidth, int blockHeight, int blockDepth, TargetBuffer target)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BytesPerPixel = bytesPerPixel;
|
|
|
|
|
BlockWidth = blockWidth;
|
|
|
|
|
BlockHeight = blockHeight;
|
|
|
|
|
BlockDepth = blockDepth;
|
|
|
|
|
Target = target;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 21:02:23 +00:00
|
|
|
|
private const GalImageFormat Snorm = GalImageFormat.Snorm;
|
|
|
|
|
private const GalImageFormat Unorm = GalImageFormat.Unorm;
|
|
|
|
|
private const GalImageFormat Sint = GalImageFormat.Sint;
|
|
|
|
|
private const GalImageFormat Uint = GalImageFormat.Uint;
|
|
|
|
|
private const GalImageFormat Float = GalImageFormat.Float;
|
|
|
|
|
private const GalImageFormat Srgb = GalImageFormat.Srgb;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
private static readonly Dictionary<GalTextureFormat, GalImageFormat> TextureTable =
|
2018-09-08 17:51:50 +00:00
|
|
|
|
new Dictionary<GalTextureFormat, GalImageFormat>()
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalTextureFormat.Rgba32, GalImageFormat.Rgba32 | Sint | Uint | Float },
|
|
|
|
|
{ GalTextureFormat.Rgba16, GalImageFormat.Rgba16 | Snorm | Unorm | Sint | Uint | Float },
|
|
|
|
|
{ GalTextureFormat.Rg32, GalImageFormat.Rg32 | Sint | Uint | Float },
|
|
|
|
|
{ GalTextureFormat.Rgba8, GalImageFormat.Rgba8 | Snorm | Unorm | Sint | Uint | Srgb },
|
|
|
|
|
{ GalTextureFormat.Rgb10A2, GalImageFormat.Rgb10A2 | Snorm | Unorm | Sint | Uint },
|
|
|
|
|
{ GalTextureFormat.Rg8, GalImageFormat.Rg8 | Snorm | Unorm | Sint | Uint },
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{ GalTextureFormat.R16, GalImageFormat.R16 | Snorm | Unorm | Sint | Uint | Float },
|
|
|
|
|
{ GalTextureFormat.R8, GalImageFormat.R8 | Snorm | Unorm | Sint | Uint },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalTextureFormat.Rg16, GalImageFormat.Rg16 | Snorm | Unorm | Sint | Float },
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{ GalTextureFormat.R32, GalImageFormat.R32 | Sint | Uint | Float },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalTextureFormat.Rgba4, GalImageFormat.Rgba4 | Unorm },
|
|
|
|
|
{ GalTextureFormat.Rgb5A1, GalImageFormat.Rgb5A1 | Unorm },
|
|
|
|
|
{ GalTextureFormat.Rgb565, GalImageFormat.Rgb565 | Unorm },
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{ GalTextureFormat.R11G11B10F, GalImageFormat.R11G11B10 | Float },
|
|
|
|
|
{ GalTextureFormat.D24S8, GalImageFormat.D24S8 | Unorm | Uint },
|
|
|
|
|
{ GalTextureFormat.D32F, GalImageFormat.D32 | Float },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalTextureFormat.D32Fx24S8, GalImageFormat.D32S8 | Float },
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{ GalTextureFormat.D16, GalImageFormat.D16 | Unorm },
|
|
|
|
|
|
2019-07-02 02:39:22 +00:00
|
|
|
|
// Compressed formats
|
2018-10-17 21:02:23 +00:00
|
|
|
|
{ GalTextureFormat.BptcSfloat, GalImageFormat.BptcSfloat | Float },
|
|
|
|
|
{ GalTextureFormat.BptcUfloat, GalImageFormat.BptcUfloat | Float },
|
|
|
|
|
{ GalTextureFormat.BptcUnorm, GalImageFormat.BptcUnorm | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.BC1, GalImageFormat.BC1 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.BC2, GalImageFormat.BC2 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.BC3, GalImageFormat.BC3 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.BC4, GalImageFormat.BC4 | Unorm | Snorm },
|
|
|
|
|
{ GalTextureFormat.BC5, GalImageFormat.BC5 | Unorm | Snorm },
|
|
|
|
|
{ GalTextureFormat.Astc2D4x4, GalImageFormat.Astc2D4x4 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D5x5, GalImageFormat.Astc2D5x5 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D6x6, GalImageFormat.Astc2D6x6 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D8x8, GalImageFormat.Astc2D8x8 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D10x10, GalImageFormat.Astc2D10x10 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D12x12, GalImageFormat.Astc2D12x12 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D5x4, GalImageFormat.Astc2D5x4 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D6x5, GalImageFormat.Astc2D6x5 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D8x6, GalImageFormat.Astc2D8x6 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D10x8, GalImageFormat.Astc2D10x8 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D12x10, GalImageFormat.Astc2D12x10 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D8x5, GalImageFormat.Astc2D8x5 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D10x5, GalImageFormat.Astc2D10x5 | Unorm | Srgb },
|
|
|
|
|
{ GalTextureFormat.Astc2D10x6, GalImageFormat.Astc2D10x6 | Unorm | Srgb }
|
|
|
|
|
};
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
private static readonly Dictionary<GalImageFormat, ImageDescriptor> ImageTable =
|
2018-09-08 17:51:50 +00:00
|
|
|
|
new Dictionary<GalImageFormat, ImageDescriptor>()
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalImageFormat.Rgba32, new ImageDescriptor(16, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgba16, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rg32, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgbx8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgba8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Bgra8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgb10A2, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{ GalImageFormat.R32, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalImageFormat.Rgba4, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{ GalImageFormat.BptcSfloat, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BptcUfloat, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalImageFormat.Bgr5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgb5A1, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rgb565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Bgr565, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{ GalImageFormat.BptcUnorm, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
2019-03-04 01:45:25 +00:00
|
|
|
|
{ GalImageFormat.Rg16, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Rg8, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{ GalImageFormat.R16, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.R8, new ImageDescriptor(1, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.R11G11B10, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BC1, new ImageDescriptor(8, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BC2, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BC3, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BC4, new ImageDescriptor(8, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.BC5, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D4x4, new ImageDescriptor(16, 4, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D5x5, new ImageDescriptor(16, 5, 5, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D6x6, new ImageDescriptor(16, 6, 6, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D8x8, new ImageDescriptor(16, 8, 8, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D10x10, new ImageDescriptor(16, 10, 10, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D12x12, new ImageDescriptor(16, 12, 12, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D5x4, new ImageDescriptor(16, 5, 4, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D6x5, new ImageDescriptor(16, 6, 5, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D8x6, new ImageDescriptor(16, 8, 6, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D10x8, new ImageDescriptor(16, 10, 8, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D12x10, new ImageDescriptor(16, 12, 10, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D8x5, new ImageDescriptor(16, 8, 5, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D10x5, new ImageDescriptor(16, 10, 5, 1, TargetBuffer.Color) },
|
|
|
|
|
{ GalImageFormat.Astc2D10x6, new ImageDescriptor(16, 10, 6, 1, TargetBuffer.Color) },
|
|
|
|
|
|
|
|
|
|
{ GalImageFormat.D16, new ImageDescriptor(2, 1, 1, 1, TargetBuffer.Depth) },
|
|
|
|
|
{ GalImageFormat.D24, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Depth) },
|
|
|
|
|
{ GalImageFormat.D24S8, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.DepthStencil) },
|
|
|
|
|
{ GalImageFormat.D32, new ImageDescriptor(4, 1, 1, 1, TargetBuffer.Depth) },
|
|
|
|
|
{ GalImageFormat.D32S8, new ImageDescriptor(8, 1, 1, 1, TargetBuffer.DepthStencil) }
|
2018-09-18 04:30:35 +00:00
|
|
|
|
};
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
public static GalImageFormat ConvertTexture(
|
2019-03-04 01:45:25 +00:00
|
|
|
|
GalTextureFormat format,
|
|
|
|
|
GalTextureType rType,
|
|
|
|
|
GalTextureType gType,
|
|
|
|
|
GalTextureType bType,
|
|
|
|
|
GalTextureType aType,
|
|
|
|
|
bool convSrgb)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (!TextureTable.TryGetValue(format, out GalImageFormat imageFormat))
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException($"Format 0x{((int)format):x} not implemented!");
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (!HasDepth(imageFormat) && (rType != gType || rType != bType || rType != aType))
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException("Per component types are not implemented!");
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
GalImageFormat formatType = convSrgb ? Srgb : GetFormatType(rType);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
GalImageFormat combinedFormat = (imageFormat & GalImageFormat.FormatMask) | formatType;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (!imageFormat.HasFlag(formatType))
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException($"Format \"{combinedFormat}\" not implemented!");
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
2018-10-17 21:02:23 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return combinedFormat;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static GalImageFormat ConvertSurface(GalSurfaceFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
case GalSurfaceFormat.Rgba32Float: return GalImageFormat.Rgba32 | Float;
|
|
|
|
|
case GalSurfaceFormat.Rgba32Uint: return GalImageFormat.Rgba32 | Uint;
|
|
|
|
|
case GalSurfaceFormat.Rgba16Float: return GalImageFormat.Rgba16 | Float;
|
|
|
|
|
case GalSurfaceFormat.Rgba16Unorm: return GalImageFormat.Rgba16 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rg32Float: return GalImageFormat.Rg32 | Float;
|
|
|
|
|
case GalSurfaceFormat.Rg32Sint: return GalImageFormat.Rg32 | Sint;
|
|
|
|
|
case GalSurfaceFormat.Rg32Uint: return GalImageFormat.Rg32 | Uint;
|
|
|
|
|
case GalSurfaceFormat.Bgra8Unorm: return GalImageFormat.Bgra8 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Bgra8Srgb: return GalImageFormat.Bgra8 | Srgb;
|
|
|
|
|
case GalSurfaceFormat.Rgb10A2Unorm: return GalImageFormat.Rgb10A2 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rgba8Unorm: return GalImageFormat.Rgba8 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rgba8Srgb: return GalImageFormat.Rgba8 | Srgb;
|
|
|
|
|
case GalSurfaceFormat.Rgba8Snorm: return GalImageFormat.Rgba8 | Snorm;
|
|
|
|
|
case GalSurfaceFormat.Rg16Snorm: return GalImageFormat.Rg16 | Snorm;
|
|
|
|
|
case GalSurfaceFormat.Rg16Unorm: return GalImageFormat.Rg16 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rg16Sint: return GalImageFormat.Rg16 | Sint;
|
|
|
|
|
case GalSurfaceFormat.Rg16Float: return GalImageFormat.Rg16 | Float;
|
2018-10-17 21:02:23 +00:00
|
|
|
|
case GalSurfaceFormat.R11G11B10Float: return GalImageFormat.R11G11B10 | Float;
|
|
|
|
|
case GalSurfaceFormat.R32Float: return GalImageFormat.R32 | Float;
|
|
|
|
|
case GalSurfaceFormat.R32Uint: return GalImageFormat.R32 | Uint;
|
2019-03-04 01:45:25 +00:00
|
|
|
|
case GalSurfaceFormat.Rg8Unorm: return GalImageFormat.Rg8 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rg8Snorm: return GalImageFormat.Rg8 | Snorm;
|
2018-10-17 21:02:23 +00:00
|
|
|
|
case GalSurfaceFormat.R16Float: return GalImageFormat.R16 | Float;
|
|
|
|
|
case GalSurfaceFormat.R16Unorm: return GalImageFormat.R16 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.R16Uint: return GalImageFormat.R16 | Uint;
|
|
|
|
|
case GalSurfaceFormat.R8Unorm: return GalImageFormat.R8 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.R8Uint: return GalImageFormat.R8 | Uint;
|
2019-03-04 01:45:25 +00:00
|
|
|
|
case GalSurfaceFormat.B5G6R5Unorm: return GalImageFormat.Rgb565 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Bgr5A1Unorm: return GalImageFormat.Bgr5A1 | Unorm;
|
|
|
|
|
case GalSurfaceFormat.Rgbx8Unorm: return GalImageFormat.Rgbx8 | Unorm;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException(format.ToString());
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static GalImageFormat ConvertZeta(GalZetaFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2018-10-17 21:02:23 +00:00
|
|
|
|
case GalZetaFormat.D32Float: return GalImageFormat.D32 | Float;
|
|
|
|
|
case GalZetaFormat.S8D24Unorm: return GalImageFormat.D24S8 | Unorm;
|
|
|
|
|
case GalZetaFormat.D16Unorm: return GalImageFormat.D16 | Unorm;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
case GalZetaFormat.D24X8Unorm: return GalImageFormat.D24 | Unorm;
|
2018-10-17 21:02:23 +00:00
|
|
|
|
case GalZetaFormat.D24S8Unorm: return GalImageFormat.D24S8 | Unorm;
|
|
|
|
|
case GalZetaFormat.D32S8X24Float: return GalImageFormat.D32S8 | Float;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException(format.ToString());
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static byte[] ReadTexture(IMemory memory, GalImage image, long position)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
Add a new JIT compiler for CPU code (#693)
* Start of the ARMeilleure project
* Refactoring around the old IRAdapter, now renamed to PreAllocator
* Optimize the LowestBitSet method
* Add CLZ support and fix CLS implementation
* Add missing Equals and GetHashCode overrides on some structs, misc small tweaks
* Implement the ByteSwap IR instruction, and some refactoring on the assembler
* Implement the DivideUI IR instruction and fix 64-bits IDIV
* Correct constant operand type on CSINC
* Move division instructions implementation to InstEmitDiv
* Fix destination type for the ConditionalSelect IR instruction
* Implement UMULH and SMULH, with new IR instructions
* Fix some issues with shift instructions
* Fix constant types for BFM instructions
* Fix up new tests using the new V128 struct
* Update tests
* Move DIV tests to a separate file
* Add support for calls, and some instructions that depends on them
* Start adding support for SIMD & FP types, along with some of the related ARM instructions
* Fix some typos and the divide instruction with FP operands
* Fix wrong method call on Clz_V
* Implement ARM FP & SIMD move instructions, Saddlv_V, and misc. fixes
* Implement SIMD logical instructions and more misc. fixes
* Fix PSRAD x86 instruction encoding, TRN, UABD and UABDL implementations
* Implement float conversion instruction, merge in LDj3SNuD fixes, and some other misc. fixes
* Implement SIMD shift instruction and fix Dup_V
* Add SCVTF and UCVTF (vector, fixed-point) variants to the opcode table
* Fix check with tolerance on tester
* Implement FP & SIMD comparison instructions, and some fixes
* Update FCVT (Scalar) encoding on the table to support the Half-float variants
* Support passing V128 structs, some cleanup on the register allocator, merge LDj3SNuD fixes
* Use old memory access methods, made a start on SIMD memory insts support, some fixes
* Fix float constant passed to functions, save and restore non-volatile XMM registers, other fixes
* Fix arguments count with struct return values, other fixes
* More instructions
* Misc. fixes and integrate LDj3SNuD fixes
* Update tests
* Add a faster linear scan allocator, unwinding support on windows, and other changes
* Update Ryujinx.HLE
* Update Ryujinx.Graphics
* Fix V128 return pointer passing, RCX is clobbered
* Update Ryujinx.Tests
* Update ITimeZoneService
* Stop using GetFunctionPointer as that can't be called from native code, misc. fixes and tweaks
* Use generic GetFunctionPointerForDelegate method and other tweaks
* Some refactoring on the code generator, assert on invalid operations and use a separate enum for intrinsics
* Remove some unused code on the assembler
* Fix REX.W prefix regression on float conversion instructions, add some sort of profiler
* Add hardware capability detection
* Fix regression on Sha1h and revert Fcm** changes
* Add SSE2-only paths on vector extract and insert, some refactoring on the pre-allocator
* Fix silly mistake introduced on last commit on CpuId
* Generate inline stack probes when the stack allocation is too large
* Initial support for the System-V ABI
* Support multiple destination operands
* Fix SSE2 VectorInsert8 path, and other fixes
* Change placement of XMM callee save and restore code to match other compilers
* Rename Dest to Destination and Inst to Instruction
* Fix a regression related to calls and the V128 type
* Add an extra space on comments to match code style
* Some refactoring
* Fix vector insert FP32 SSE2 path
* Port over the ARM32 instructions
* Avoid memory protection races on JIT Cache
* Another fix on VectorInsert FP32 (thanks to LDj3SNuD
* Float operands don't need to use the same register when VEX is supported
* Add a new register allocator, higher quality code for hot code (tier up), and other tweaks
* Some nits, small improvements on the pre allocator
* CpuThreadState is gone
* Allow changing CPU emulators with a config entry
* Add runtime identifiers on the ARMeilleure project
* Allow switching between CPUs through a config entry (pt. 2)
* Change win10-x64 to win-x64 on projects
* Update the Ryujinx project to use ARMeilleure
* Ensure that the selected register is valid on the hybrid allocator
* Allow exiting on returns to 0 (should fix test regression)
* Remove register assignments for most used variables on the hybrid allocator
* Do not use fixed registers as spill temp
* Add missing namespace and remove unneeded using
* Address PR feedback
* Fix types, etc
* Enable AssumeStrictAbiCompliance by default
* Ensure that Spill and Fill don't load or store any more than necessary
2019-08-08 18:56:22 +00:00
|
|
|
|
IMemoryManager cpuMemory;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (memory is NvGpuVmm vmm)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
cpuMemory = vmm.Memory;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
Add a new JIT compiler for CPU code (#693)
* Start of the ARMeilleure project
* Refactoring around the old IRAdapter, now renamed to PreAllocator
* Optimize the LowestBitSet method
* Add CLZ support and fix CLS implementation
* Add missing Equals and GetHashCode overrides on some structs, misc small tweaks
* Implement the ByteSwap IR instruction, and some refactoring on the assembler
* Implement the DivideUI IR instruction and fix 64-bits IDIV
* Correct constant operand type on CSINC
* Move division instructions implementation to InstEmitDiv
* Fix destination type for the ConditionalSelect IR instruction
* Implement UMULH and SMULH, with new IR instructions
* Fix some issues with shift instructions
* Fix constant types for BFM instructions
* Fix up new tests using the new V128 struct
* Update tests
* Move DIV tests to a separate file
* Add support for calls, and some instructions that depends on them
* Start adding support for SIMD & FP types, along with some of the related ARM instructions
* Fix some typos and the divide instruction with FP operands
* Fix wrong method call on Clz_V
* Implement ARM FP & SIMD move instructions, Saddlv_V, and misc. fixes
* Implement SIMD logical instructions and more misc. fixes
* Fix PSRAD x86 instruction encoding, TRN, UABD and UABDL implementations
* Implement float conversion instruction, merge in LDj3SNuD fixes, and some other misc. fixes
* Implement SIMD shift instruction and fix Dup_V
* Add SCVTF and UCVTF (vector, fixed-point) variants to the opcode table
* Fix check with tolerance on tester
* Implement FP & SIMD comparison instructions, and some fixes
* Update FCVT (Scalar) encoding on the table to support the Half-float variants
* Support passing V128 structs, some cleanup on the register allocator, merge LDj3SNuD fixes
* Use old memory access methods, made a start on SIMD memory insts support, some fixes
* Fix float constant passed to functions, save and restore non-volatile XMM registers, other fixes
* Fix arguments count with struct return values, other fixes
* More instructions
* Misc. fixes and integrate LDj3SNuD fixes
* Update tests
* Add a faster linear scan allocator, unwinding support on windows, and other changes
* Update Ryujinx.HLE
* Update Ryujinx.Graphics
* Fix V128 return pointer passing, RCX is clobbered
* Update Ryujinx.Tests
* Update ITimeZoneService
* Stop using GetFunctionPointer as that can't be called from native code, misc. fixes and tweaks
* Use generic GetFunctionPointerForDelegate method and other tweaks
* Some refactoring on the code generator, assert on invalid operations and use a separate enum for intrinsics
* Remove some unused code on the assembler
* Fix REX.W prefix regression on float conversion instructions, add some sort of profiler
* Add hardware capability detection
* Fix regression on Sha1h and revert Fcm** changes
* Add SSE2-only paths on vector extract and insert, some refactoring on the pre-allocator
* Fix silly mistake introduced on last commit on CpuId
* Generate inline stack probes when the stack allocation is too large
* Initial support for the System-V ABI
* Support multiple destination operands
* Fix SSE2 VectorInsert8 path, and other fixes
* Change placement of XMM callee save and restore code to match other compilers
* Rename Dest to Destination and Inst to Instruction
* Fix a regression related to calls and the V128 type
* Add an extra space on comments to match code style
* Some refactoring
* Fix vector insert FP32 SSE2 path
* Port over the ARM32 instructions
* Avoid memory protection races on JIT Cache
* Another fix on VectorInsert FP32 (thanks to LDj3SNuD
* Float operands don't need to use the same register when VEX is supported
* Add a new register allocator, higher quality code for hot code (tier up), and other tweaks
* Some nits, small improvements on the pre allocator
* CpuThreadState is gone
* Allow changing CPU emulators with a config entry
* Add runtime identifiers on the ARMeilleure project
* Allow switching between CPUs through a config entry (pt. 2)
* Change win10-x64 to win-x64 on projects
* Update the Ryujinx project to use ARMeilleure
* Ensure that the selected register is valid on the hybrid allocator
* Allow exiting on returns to 0 (should fix test regression)
* Remove register assignments for most used variables on the hybrid allocator
* Do not use fixed registers as spill temp
* Add missing namespace and remove unneeded using
* Address PR feedback
* Fix types, etc
* Enable AssumeStrictAbiCompliance by default
* Ensure that Spill and Fill don't load or store any more than necessary
2019-08-08 18:56:22 +00:00
|
|
|
|
cpuMemory = (IMemoryManager)memory;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ISwizzle swizzle = TextureHelper.GetSwizzle(image);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(image.Format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
(int width, int height, int depth) = GetImageSizeInBlocks(image);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int bytesPerPixel = desc.BytesPerPixel;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-07-02 02:39:22 +00:00
|
|
|
|
// Note: Each row of the texture needs to be aligned to 4 bytes.
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int pitch = (width * bytesPerPixel + 3) & ~3;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int dataLayerSize = height * pitch * depth;
|
|
|
|
|
byte[] data = new byte[dataLayerSize * image.LayerCount];
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int targetMipLevel = image.MaxMipmapLevel <= 1 ? 1 : image.MaxMipmapLevel - 1;
|
|
|
|
|
int layerOffset = GetLayerOffset(image, targetMipLevel);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int layer = 0; layer < image.LayerCount; layer++)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int z = 0; z < depth; z++)
|
2018-10-14 02:54:14 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int y = 0; y < height; y++)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int outOffs = (dataLayerSize * layer) + y * pitch + (z * width * height * bytesPerPixel);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int x = 0; x < width; x++)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
long offset = (uint)swizzle.GetSwizzleOffset(x, y, z);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
cpuMemory.ReadBytes(position + (layerOffset * layer) + offset, data, outOffs, bytesPerPixel);
|
2018-10-14 02:54:14 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
outOffs += bytesPerPixel;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-14 02:54:14 +00:00
|
|
|
|
}
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return data;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static void WriteTexture(NvGpuVmm vmm, GalImage image, long position, byte[] data)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ISwizzle swizzle = TextureHelper.GetSwizzle(image);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(image.Format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
(int width, int height, int depth) = GetImageSizeInBlocks(image);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int bytesPerPixel = desc.BytesPerPixel;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int inOffs = 0;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int z = 0; z < depth; z++)
|
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
|
for (int x = 0; x < width; x++)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
long offset = (uint)swizzle.GetSwizzleOffset(x, y, z);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
vmm.Memory.WriteBytes(position + offset, data, inOffs, bytesPerPixel);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
inOffs += bytesPerPixel;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
|
// TODO: Support non 2D
|
2018-11-17 04:01:31 +00:00
|
|
|
|
public static bool CopyTexture(
|
2019-03-04 01:45:25 +00:00
|
|
|
|
NvGpuVmm vmm,
|
|
|
|
|
GalImage srcImage,
|
|
|
|
|
GalImage dstImage,
|
|
|
|
|
long srcAddress,
|
|
|
|
|
long dstAddress,
|
|
|
|
|
int srcX,
|
|
|
|
|
int srcY,
|
|
|
|
|
int dstX,
|
|
|
|
|
int dstY,
|
|
|
|
|
int width,
|
|
|
|
|
int height)
|
2018-11-17 04:01:31 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ISwizzle srcSwizzle = TextureHelper.GetSwizzle(srcImage);
|
|
|
|
|
ISwizzle dstSwizzle = TextureHelper.GetSwizzle(dstImage);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(srcImage.Format);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (GetImageDescriptor(dstImage.Format).BytesPerPixel != desc.BytesPerPixel)
|
2018-11-17 04:01:31 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int bytesPerPixel = desc.BytesPerPixel;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
|
for (int x = 0; x < width; x++)
|
2018-11-17 04:01:31 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
long srcOffset = (uint)srcSwizzle.GetSwizzleOffset(srcX + x, srcY + y, 0);
|
|
|
|
|
long dstOffset = (uint)dstSwizzle.GetSwizzleOffset(dstX + x, dstY + y, 0);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
byte[] texel = vmm.ReadBytes(srcAddress + srcOffset, bytesPerPixel);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
vmm.WriteBytes(dstAddress + dstOffset, texel);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetSize(GalImage image)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(image.Format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int componentCount = GetCoordsCountTextureTarget(image.TextureTarget);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (IsArray(image.TextureTarget))
|
|
|
|
|
componentCount--;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int width = DivRoundUp(image.Width, desc.BlockWidth);
|
|
|
|
|
int height = DivRoundUp(image.Height, desc.BlockHeight);
|
|
|
|
|
int depth = DivRoundUp(image.Depth, desc.BlockDepth);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (componentCount)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
|
|
|
|
case 1:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return desc.BytesPerPixel * width * image.LayerCount;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
case 2:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return desc.BytesPerPixel * width * height * image.LayerCount;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
case 3:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return desc.BytesPerPixel * width * height * depth * image.LayerCount;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
default:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new InvalidOperationException($"Invalid component count: {componentCount}");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetGpuSize(GalImage image, bool forcePitch = false)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return TextureHelper.GetSwizzle(image).GetImageSize(image.MaxMipmapLevel) * image.LayerCount;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetLayerOffset(GalImage image, int mipLevel)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (mipLevel <= 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
mipLevel = 1;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return TextureHelper.GetSwizzle(image).GetMipOffset(mipLevel);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetPitch(GalImageFormat format, int width)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int pitch = desc.BytesPerPixel * DivRoundUp(width, desc.BlockWidth);
|
2018-10-13 01:37:01 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
pitch = (pitch + 0x1f) & ~0x1f;
|
2018-10-13 01:37:01 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return pitch;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetBlockWidth(GalImageFormat format)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return GetImageDescriptor(format).BlockWidth;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetBlockHeight(GalImageFormat format)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return GetImageDescriptor(format).BlockHeight;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetBlockDepth(GalImageFormat format)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return GetImageDescriptor(format).BlockDepth;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetAlignedWidth(GalImage image)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(image.Format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int alignMask;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (image.Layout == GalMemoryLayout.BlockLinear)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
alignMask = image.TileWidth * (64 / desc.BytesPerPixel) - 1;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
alignMask = (32 / desc.BytesPerPixel) - 1;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (image.Width + alignMask) & ~alignMask;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static (int Width, int Height, int Depth) GetImageSizeInBlocks(GalImage image)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(image.Format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (DivRoundUp(image.Width, desc.BlockWidth),
|
|
|
|
|
DivRoundUp(image.Height, desc.BlockHeight),
|
|
|
|
|
DivRoundUp(image.Depth, desc.BlockDepth));
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetBytesPerPixel(GalImageFormat format)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return GetImageDescriptor(format).BytesPerPixel;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
private static int DivRoundUp(int lhs, int rhs)
|
2018-09-18 04:30:35 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (lhs + (rhs - 1)) / rhs;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static bool HasColor(GalImageFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (GetImageDescriptor(format).Target & TargetBuffer.Color) != 0;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static bool HasDepth(GalImageFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (GetImageDescriptor(format).Target & TargetBuffer.Depth) != 0;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static bool HasStencil(GalImageFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (GetImageDescriptor(format).Target & TargetBuffer.Stencil) != 0;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static bool IsCompressed(GalImageFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
ImageDescriptor desc = GetImageDescriptor(format);
|
2018-09-18 04:30:35 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return (desc.BlockWidth | desc.BlockHeight) != 1;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
private static ImageDescriptor GetImageDescriptor(GalImageFormat format)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
GalImageFormat pixelFormat = format & GalImageFormat.FormatMask;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if (ImageTable.TryGetValue(pixelFormat, out ImageDescriptor descriptor))
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return descriptor;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException($"Format \"{pixelFormat}\" not implemented!");
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
private static GalImageFormat GetFormatType(GalTextureType type)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (type)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
case GalTextureType.Snorm: return Snorm;
|
|
|
|
|
case GalTextureType.Unorm: return Unorm;
|
|
|
|
|
case GalTextureType.Sint: return Sint;
|
|
|
|
|
case GalTextureType.Uint: return Uint;
|
2018-10-17 21:02:23 +00:00
|
|
|
|
case GalTextureType.Float: return Float;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
default: throw new NotImplementedException(((int)type).ToString());
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static TextureTarget GetTextureTarget(GalTextureTarget galTextureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (galTextureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
|
|
|
|
case GalTextureTarget.OneD:
|
|
|
|
|
return TextureTarget.Texture1D;
|
|
|
|
|
case GalTextureTarget.TwoD:
|
|
|
|
|
case GalTextureTarget.TwoDNoMipMap:
|
|
|
|
|
return TextureTarget.Texture2D;
|
|
|
|
|
case GalTextureTarget.ThreeD:
|
|
|
|
|
return TextureTarget.Texture3D;
|
|
|
|
|
case GalTextureTarget.OneDArray:
|
|
|
|
|
return TextureTarget.Texture1DArray;
|
|
|
|
|
case GalTextureTarget.OneDBuffer:
|
|
|
|
|
return TextureTarget.TextureBuffer;
|
|
|
|
|
case GalTextureTarget.TwoDArray:
|
|
|
|
|
return TextureTarget.Texture2DArray;
|
|
|
|
|
case GalTextureTarget.CubeMap:
|
|
|
|
|
return TextureTarget.TextureCubeMap;
|
|
|
|
|
case GalTextureTarget.CubeArray:
|
|
|
|
|
return TextureTarget.TextureCubeMapArray;
|
|
|
|
|
default:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotSupportedException($"Texture target {galTextureTarget} currently not supported!");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static bool IsArray(GalTextureTarget textureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (textureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
|
|
|
|
case GalTextureTarget.OneDArray:
|
|
|
|
|
case GalTextureTarget.TwoDArray:
|
|
|
|
|
case GalTextureTarget.CubeArray:
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static int GetCoordsCountTextureTarget(GalTextureTarget textureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (textureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
|
{
|
|
|
|
|
case GalTextureTarget.OneD:
|
|
|
|
|
return 1;
|
|
|
|
|
case GalTextureTarget.OneDArray:
|
|
|
|
|
case GalTextureTarget.OneDBuffer:
|
|
|
|
|
case GalTextureTarget.TwoD:
|
|
|
|
|
case GalTextureTarget.TwoDNoMipMap:
|
|
|
|
|
return 2;
|
|
|
|
|
case GalTextureTarget.ThreeD:
|
|
|
|
|
case GalTextureTarget.TwoDArray:
|
|
|
|
|
case GalTextureTarget.CubeMap:
|
|
|
|
|
return 3;
|
|
|
|
|
case GalTextureTarget.CubeArray:
|
|
|
|
|
return 4;
|
|
|
|
|
default:
|
2019-03-04 01:45:25 +00:00
|
|
|
|
throw new NotImplementedException($"TextureTarget.{textureTarget} not implemented yet.");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
2018-11-09 18:41:40 +00:00
|
|
|
|
}
|