Skip to content

Commit f5b4f75

Browse files
authored
Merge pull request #1 from DevsDaddy/develop
First Release
2 parents 33d4428 + 2aebb2b commit f5b4f75

File tree

889 files changed

+126740
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

889 files changed

+126740
-2
lines changed

Packages/r1_xscaling.unitypackage

24.8 MB
Binary file not shown.

README.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1-
# UnityXScaling
2-
Unity XScaling - a runtime framework for GPU-based textures and screen upscaling (super-resolution). Supports every platform.
1+
# Unity XScaling Framework
2+
![Unity XScaling Framework](https://github.com/DevsDaddy/UnityXScaling/assets/147835900/ac00e72d-923f-443e-9228-5ea5c1f52ea7)
3+
**Unity XScaling Framework** - is a set of tools for texture upscaling and real-time camera rendering, allowing you to improve picture quality without increasing the size of your build.
4+
5+
Simply load 256x256 textures into your build and upscale them to any size in real time on demand. Or increase performance and quality with Super-Resolution Rendering and the Camera Upscale component
6+
7+
**:question: What does the XScaling Library include?**
8+
* Real-time Texture Upscaling;
9+
* UI Image and RawImage Upscaler;
10+
* Distance-based textures upscaling;
11+
* Rendering Upscaling (Super-Sampling) (similar FRS or DLSS but fully cross-platform);
12+
* GPU-Based and cross-platform;
13+
14+
## Get Started
15+
**GameShield** installs into your project as easily as possible - basic installation and configuration takes **only 4 steps**. Below we will consider both **automatic initialization** and **manual configuration**.
16+
17+
**Basic Installation:**
18+
* Download <a href="https://github.com/DevsDaddy/UnityXScaling/releases">Latest Release from GitHUB</a>;
19+
* Import **.unitypackage** into your project;
20+
* See example scenes or add ready-to-use components on your models / ui elements;
21+
* Done!
22+
23+
**Dependencies:**
24+
- Unity 2019+ and Built-in Render Pipeline Only (URP and HDRP support is coming soon);
25+
- TextMesh Pro and Texture2D packages for UI support;
26+
27+
## Note
28+
> This library is still under development. The system performs best on cartoon textures, but I aim to solve this soon by adding new algorithms such as Real-ESRGAN.
29+
> Do not use preset HQ Quality at Production. Use HQ Performance instead.
30+
31+
## Join Community
32+
- <a href="https://discord.gg/xuNTKRDebx">Discord Community</a>
33+
34+
## Support Me
35+
**You can support the development and updating of libraries and assemblies by dropping a coin:**
36+
<table>
37+
<tr><td>Bitcoin (BTC)</td><td>bc1qef2d34r4xkrm48zknjdjt7c0ea92ay9m2a7q55</td></tr>
38+
<tr><td>Etherium (ETH)</td><td>0x1112a2Ef850711DF4dE9c432376F255f416ef5d0</td></tr>
39+
<tr><td>Boosty</td><td>https://boosty.to/devsdaddy</td></tr>
40+
</table>

.gitignore Source/.gitignore

File renamed without changes.

Source/Assets/DevsDaddy.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Assets/DevsDaddy/XScaling.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using DevsDaddy.XScaling.Core.Render;
5+
using DevsDaddy.XScaling.Core.Upscale;
6+
using DevsDaddy.XScaling.Enum;
7+
using DevsDaddy.XScaling.Utils;
8+
using UnityEngine;
9+
using UnityEngine.Experimental.Rendering;
10+
using UnityEngine.Rendering;
11+
12+
namespace DevsDaddy.XScaling
13+
{
14+
/// <summary>
15+
/// XScale Camera Render Upscaling Component
16+
/// </summary>
17+
[AddComponentMenu("XScaling/Camera/Upscale Camera")]
18+
[RequireComponent(typeof(Camera))]
19+
public class CameraUpscaler : MonoBehaviour
20+
{
21+
// Events
22+
public delegate void ApplyMipMapBiasDelegate(float biasOffset);
23+
public static ApplyMipMapBiasDelegate OnApplyMipMapBias;
24+
25+
public delegate void UndoMipMapBiasDelegate();
26+
public static UndoMipMapBiasDelegate OnUndoMipMapBias;
27+
28+
[Header("Basic Setup")]
29+
[Tooltip("Standard Upscale Presets")]
30+
public UpscaleMode qualityMode = UpscaleMode.HQQuality;
31+
32+
private Vector2Int _maxRenderSize;
33+
private Vector2Int _displaySize;
34+
private bool _resetHistory;
35+
36+
private Camera _renderCamera;
37+
private RenderTexture _originalRenderTarget;
38+
private DepthTextureMode _originalDepthTextureMode;
39+
private Rect _originalRect;
40+
41+
private UpscaleMode _prevQualityMode;
42+
private Vector2Int _prevDisplaySize;
43+
44+
private Material _copyWithDepthMaterial;
45+
46+
private bool _isActiveRender = false;
47+
48+
private void OnEnable()
49+
{
50+
_renderCamera = GetComponent<Camera>();
51+
_originalRenderTarget = _renderCamera.targetTexture;
52+
_originalDepthTextureMode = _renderCamera.depthTextureMode;
53+
_renderCamera.targetTexture = null;
54+
_renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
55+
56+
// Determine the desired rendering and display resolutions
57+
_displaySize = GetDisplaySize();
58+
UpscaleUtils.GetRenderResolutionFromQualityMode(out var maxRenderWidth, out var maxRenderHeight, _displaySize.x, _displaySize.y, qualityMode);
59+
_maxRenderSize = new Vector2Int(maxRenderWidth, maxRenderHeight);
60+
61+
if (_maxRenderSize.x == 0 || _maxRenderSize.y == 0)
62+
{
63+
Debug.LogError($"XScaling Upscaler render size is invalid: {_maxRenderSize.x}x{_maxRenderSize.y}. Please check your screen resolution and camera viewport parameters.");
64+
enabled = false;
65+
return;
66+
}
67+
68+
_copyWithDepthMaterial = new Material(Shader.Find("Hidden/BlitCopyWithDepth"));
69+
CreateContext();
70+
71+
XRender.OnStateChanged += OnRenderStateChanged;
72+
OnRenderStateChanged(XRender.IsActive);
73+
}
74+
75+
private void OnDisable()
76+
{
77+
DestroyContext();
78+
79+
if (_copyWithDepthMaterial != null)
80+
{
81+
Destroy(_copyWithDepthMaterial);
82+
_copyWithDepthMaterial = null;
83+
}
84+
85+
// Restore the camera's original state
86+
_renderCamera.depthTextureMode = _originalDepthTextureMode;
87+
_renderCamera.targetTexture = _originalRenderTarget;
88+
XRender.OnStateChanged -= OnRenderStateChanged;
89+
}
90+
91+
private void OnRenderStateChanged(bool isRenderActive) {
92+
_isActiveRender = isRenderActive;
93+
}
94+
95+
private void CreateContext()
96+
{
97+
_prevDisplaySize = _displaySize;
98+
_prevQualityMode = qualityMode;
99+
ApplyMipmapBias();
100+
}
101+
102+
private void DestroyContext()
103+
{
104+
UndoMipmapBias();
105+
}
106+
107+
private void ApplyMipmapBias()
108+
{
109+
// Apply a mipmap bias so that textures retain their sharpness
110+
float biasOffset = UpscaleUtils.GetMipmapBiasOffset(_maxRenderSize.x, _displaySize.x);
111+
if (!float.IsNaN(biasOffset) && !float.IsInfinity(biasOffset))
112+
{
113+
OnApplyMipMapBias?.Invoke(biasOffset);
114+
}
115+
}
116+
117+
private void UndoMipmapBias()
118+
{
119+
OnUndoMipMapBias?.Invoke();
120+
}
121+
122+
private void Update()
123+
{
124+
// Monitor for any changes in parameters that require a reset of the FSR3 Upscaler context
125+
var displaySize = GetDisplaySize();
126+
if (displaySize.x != _prevDisplaySize.x || displaySize.y != _prevDisplaySize.y || qualityMode != _prevQualityMode)
127+
{
128+
OnDisable();
129+
OnEnable();
130+
}
131+
}
132+
private void LateUpdate()
133+
{
134+
// Remember the original camera viewport before we modify it in OnPreCull
135+
_originalRect = _renderCamera.rect;
136+
}
137+
138+
private void OnPreCull()
139+
{
140+
_renderCamera.aspect = (float)_displaySize.x / _displaySize.y;
141+
_renderCamera.rect = new Rect(0, 0, _originalRect.width * _maxRenderSize.x / _renderCamera.pixelWidth, _originalRect.height * _maxRenderSize.y / _renderCamera.pixelHeight);
142+
}
143+
144+
private void OnRenderImage(RenderTexture src, RenderTexture dest)
145+
{
146+
// Restore the camera's viewport rect so we can output at full resolution
147+
_renderCamera.rect = _originalRect;
148+
_renderCamera.ResetProjectionMatrix();
149+
150+
if (_isActiveRender) {
151+
// The backbuffer is not set up to allow random-write access, so we need a temporary render texture for FSR3 to output to
152+
RenderTexture renderTexture = UpscaleProcessor.UpscaleToRenderTexture(src, _prevQualityMode,
153+
UpscaleTechnique.TechniqueB, new UpscaleSize(Screen.width, Screen.height));
154+
155+
// Output the upscaled image
156+
if (_originalRenderTarget != null)
157+
Graphics.Blit(renderTexture, _originalRenderTarget, _copyWithDepthMaterial);
158+
else
159+
Graphics.Blit(renderTexture, dest);
160+
161+
RenderTexture.ReleaseTemporary(renderTexture);
162+
renderTexture.Release();
163+
RenderTexture.active = dest;
164+
}
165+
else {
166+
// Output the upscaled image
167+
if (_originalRenderTarget != null)
168+
Graphics.Blit(src, _originalRenderTarget, _copyWithDepthMaterial);
169+
else
170+
Graphics.Blit(src, dest);
171+
}
172+
}
173+
174+
private RenderTextureFormat GetDefaultFormat()
175+
{
176+
if (_originalRenderTarget != null)
177+
return _originalRenderTarget.format;
178+
179+
return _renderCamera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
180+
}
181+
182+
private Vector2Int GetDisplaySize()
183+
{
184+
if (_originalRenderTarget != null)
185+
return new Vector2Int(_originalRenderTarget.width, _originalRenderTarget.height);
186+
187+
return new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight);
188+
}
189+
}
190+
}

Source/Assets/DevsDaddy/XScaling/CameraUpscaler.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Assets/DevsDaddy/XScaling/Components.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Assets/DevsDaddy/XScaling/Components/Mesh.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Assets/DevsDaddy/XScaling/Components/Sprite.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Assets/DevsDaddy/XScaling/Components/Texture.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)