Skip to content

Commit 61100af

Browse files
authored
Merge pull request #105 from orels1/dev
Release 7.1.0
2 parents edce219 + 7f0733a commit 61100af

File tree

21 files changed

+508
-241
lines changed

21 files changed

+508
-241
lines changed

Packages/sh.orels.shaders.generator/Editor/ShaderDefinitionImporter.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,12 @@ private List<ShaderBlock> OptimizeBlocks(List<ShaderBlock> sourceBlocks)
10921092

10931093
// Matches SAMPLER()
10941094
private Regex _samplerRegex = new Regex(@"(?:SAMPLER)(?:_CMP)?\((?<identifier>[\w]+)\)");
1095+
1096+
// Matches TEXTURE2D_PARAM()
1097+
private Regex _texParamRegex = new Regex(@"TEXTURE2D_PARAM\((?<identifier>[\w]+),\s*(?<sampler>[\w]+)\)");
1098+
1099+
// Matches TEXTURE2D_ARGS()
1100+
private Regex _texArgsRegex = new Regex(@"TEXTURE2D_ARGS\((?<identifier>[\w]+),\s?(?<sampler>[\w]+)\)");
10951101

10961102
private enum DeDupeType
10971103
{
@@ -1476,13 +1482,15 @@ public string GenerateShader(bool stripSamplingMacros = false)
14761482
var skippingSampling = false;
14771483
foreach (var line in source)
14781484
{
1479-
if (line.Contains("// Sampling Library Module Start"))
1485+
if (line.Contains("// Sampling Library Module Start")
1486+
|| line.Contains("// BiRP to URP Sampling Macros Start"))
14801487
{
14811488
skippingSampling = true;
14821489
continue;
14831490
}
14841491

1485-
if (line.Contains("// Sampling Library Module End"))
1492+
if (line.Contains("// Sampling Library Module End")
1493+
|| line.Contains("// BiRP to URP Sampling Macros End"))
14861494
{
14871495
skippingSampling = false;
14881496
continue;
@@ -1508,7 +1516,25 @@ public string GenerateShader(bool stripSamplingMacros = false)
15081516
continue;
15091517
}
15101518

1511-
if (line.Contains("SAMPLE_TEXTURE2D_GRAD"))
1519+
var paramsMatch = _texParamRegex.Match(line);
1520+
if (paramsMatch.Success)
1521+
{
1522+
var newLine = line.Replace(paramsMatch.Value,
1523+
$"Texture2D<float4> {paramsMatch.Groups["identifier"].Value}, SamplerState {paramsMatch.Groups["sampler"].Value}");
1524+
processedSource.AppendLine(newLine);
1525+
continue;
1526+
}
1527+
1528+
var argsMatch = _texArgsRegex.Match(line);
1529+
if (argsMatch.Success)
1530+
{
1531+
var newLine = line.Replace(argsMatch.Value,
1532+
$"{argsMatch.Groups["identifier"].Value}, {argsMatch.Groups["sampler"].Value}");
1533+
processedSource.AppendLine(newLine);
1534+
continue;
1535+
}
1536+
1537+
if (line.Contains("SAMPLE_TEXTURE2D_GRAD("))
15121538
{
15131539
// search and parse parameters to rewrite into a `tex.SampleGrad` call
15141540
var substring = line.Substring(line.IndexOf("SAMPLE_TEXTURE2D_GRAD") + 21);
@@ -1538,36 +1564,39 @@ public string GenerateShader(bool stripSamplingMacros = false)
15381564
processedSource.AppendLine(newLine);
15391565
continue;
15401566
}
1541-
1542-
1567+
15431568
if (line.Contains("SAMPLE_TEXTURE2D_LOD"))
15441569
{
1545-
var newLine = line.Replace("SAMPLE_TEXTURE2D_LOD", "UNITY_SAMPLE_TEX2D_LOD_SAMPLER")
1546-
.Replace("sampler_", "_");
1570+
var newLine = line.Replace("SAMPLE_TEXTURE2D_LOD", "UNITY_SAMPLE_TEX2D_SAMPLER_LOD").Replace("sampler", "");
1571+
processedSource.AppendLine(newLine);
1572+
continue;
1573+
}
1574+
1575+
// Special Handling for ScreenSpace textures
1576+
if (line.Contains("SAMPLE_TEXTURE2D_X"))
1577+
{
1578+
var newLine = line.Replace("SAMPLE_TEXTURE2D_X", "UNITY_SAMPLE_TEX2D_SAMPLER").Replace("sampler", "");
15471579
processedSource.AppendLine(newLine);
15481580
continue;
15491581
}
15501582

15511583
if (line.Contains("SAMPLE_TEXTURE2D"))
15521584
{
1553-
var newLine = line.Replace("SAMPLE_TEXTURE2D", "UNITY_SAMPLE_TEX2D_SAMPLER")
1554-
.Replace("sampler_", "_");
1585+
var newLine = line.Replace("SAMPLE_TEXTURE2D", "UNITY_SAMPLE_TEX2D_SAMPLER").Replace("sampler", "");
15551586
processedSource.AppendLine(newLine);
15561587
continue;
15571588
}
15581589

15591590
if (line.Contains("SAMPLE_TEXTURECUBE_LOD"))
15601591
{
1561-
var newLine = line.Replace("SAMPLE_TEXTURECUBE_LOD", "UNITY_SAMPLE_TEXCUBE_SAMPLER_LOD")
1562-
.Replace("sampler_", "_");
1592+
var newLine = line.Replace("SAMPLE_TEXTURECUBE_LOD", "UNITY_SAMPLE_TEXCUBE_SAMPLER_LOD").Replace("sampler", "");
15631593
processedSource.AppendLine(newLine);
15641594
continue;
15651595
}
15661596

15671597
if (line.Contains("SAMPLE_TEXTURECUBE"))
15681598
{
1569-
var newLine = line.Replace("SAMPLE_TEXTURECUBE", "UNITY_SAMPLE_TEXCUBE_SAMPLER")
1570-
.Replace("sampler_", "_");
1599+
var newLine = line.Replace("SAMPLE_TEXTURECUBE", "UNITY_SAMPLE_TEXCUBE_SAMPLER").Replace("sampler", "");
15711600
processedSource.AppendLine(newLine);
15721601
continue;
15731602
}

Packages/sh.orels.shaders.generator/Editor/SourceImporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if UNITY_2022_3_OR_NEWER
1+
#if UNITY_2022_3_OR_NEWER
22
using UnityEditor.AssetImporters;
33
#else
44
using UnityEditor.Experimental.AssetImporters;
@@ -10,4 +10,4 @@ namespace ORL.ShaderGenerator
1010
public class SourceImporter : BaseTextImporter
1111
{
1212
}
13-
}
13+
}

Packages/sh.orels.shaders.generator/Runtime/Sources/Libraries/CoreRPShaderLibrary/BiRPtoURP.orlsource

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
%LibraryFunctions(-100)
22
{
3+
// BiRP to URP Library Module Start
4+
35
//--------------------------------------------------------------
46
// Macros to redefine built-in macros to those used by the URP
57
//--------------------------------------------------------------
@@ -69,7 +71,7 @@
6971
return inVec * rsqrt(dp3);
7072
}
7173

72-
74+
// BiRP to URP Sampling Macros Start
7375
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
7476

7577
#define SLICE_ARRAY_INDEX unity_StereoEyeIndex
@@ -107,7 +109,7 @@
107109
#define GATHER_GREEN_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_GREEN_TEXTURE2D(textureName, samplerName, coord2)
108110
#define GATHER_BLUE_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_BLUE_TEXTURE2D(textureName, samplerName, coord2)
109111
#endif
110-
112+
// BiRP to URP Sampling Macros End
111113

112114

113115
//-----------------------------------------------------------------------------
@@ -513,7 +515,7 @@
513515
Light GetMainLight()
514516
{
515517
Light light;
516-
518+
517519
light.direction = _WorldSpaceLightPos0.xyz;
518520
light.shadowAttenuation = 1.0;
519521
light.color = _LightColor0.rgb;
@@ -546,11 +548,11 @@
546548
#ifndef USING_DIRECTIONAL_LIGHT
547549
light.direction = normalize(UnityWorldSpaceLightDir(positionWS));
548550
#endif
549-
551+
550552
__BridgeShadowStruct input = (__BridgeShadowStruct) 0;
551553
input._ShadowCoord = shadowCoord;
552554
UNITY_LIGHT_ATTENUATION(lightAttenuation, input, positionWS);
553-
555+
554556
light.shadowAttenuation = lightAttenuation;
555557

556558
return light;
@@ -568,4 +570,6 @@
568570
#endif
569571
}
570572
#endif
571-
}
573+
574+
// BiRP to URP Library Module End
575+
}

Packages/sh.orels.shaders.generator/Runtime/Sources/Libraries/Utilities.orlsource

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
return 1.0f + w3(a) / (w2(a) + w3(a)) + 0.5f;
9797
}
9898

99-
half4 tex2DFastBicubicSample(TEXTURE2D_PARAM(tex, sampler_tex), float2 uv)
99+
half4 tex2DFastBicubicSample(TEXTURE2D_PARAM(tex, samplertex), float2 uv)
100100
{
101101
#if !defined(PLAT_QUEST) && defined(BICUBIC_LIGHTMAP)
102102
half width;
@@ -122,17 +122,17 @@
122122
half h0y = h0(fy);
123123
half h1y = h1(fy);
124124

125-
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h0x, py + h0y) * 1.0f / width)) +
126-
g1x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h1x, py + h0y) * 1.0f / width))) +
127-
g1(fy) * (g0x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h0x, py + h1y) * 1.0f / width)) +
128-
g1x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h1x, py + h1y) * 1.0f / width)));
125+
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h0x, py + h0y) * 1.0f / width)) +
126+
g1x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h1x, py + h0y) * 1.0f / width))) +
127+
g1(fy) * (g0x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h0x, py + h1y) * 1.0f / width)) +
128+
g1x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h1x, py + h1y) * 1.0f / width)));
129129
return r;
130130
#else
131-
return SAMPLE_TEXTURE2D(tex, sampler_tex, uv);
131+
return SAMPLE_TEXTURE2D(tex, samplertex, uv);
132132
#endif
133133
}
134134

135-
float4 tex2DFastBicubicSampleNoChecks(TEXTURE2D_PARAM(tex, sampler_tex), float2 uv)
135+
float4 tex2DFastBicubicSampleNoChecks(TEXTURE2D_PARAM(tex, samplertex), float2 uv)
136136
{
137137
half width;
138138
half height;
@@ -157,14 +157,14 @@
157157
half h0y = h0(fy);
158158
half h1y = h1(fy);
159159

160-
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h0x, py + h0y) * 1.0f / width)) +
161-
g1x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h1x, py + h0y) * 1.0f / width))) +
162-
g1(fy) * (g0x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h0x, py + h1y) * 1.0f / width)) +
163-
g1x * SAMPLE_TEXTURE2D(tex, sampler_tex, (half2(px + h1x, py + h1y) * 1.0f / width)));
160+
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h0x, py + h0y) * 1.0f / width)) +
161+
g1x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h1x, py + h0y) * 1.0f / width))) +
162+
g1(fy) * (g0x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h0x, py + h1y) * 1.0f / width)) +
163+
g1x * SAMPLE_TEXTURE2D(tex, samplertex, (half2(px + h1x, py + h1y) * 1.0f / width)));
164164
return r;
165165
}
166166

167-
half4 tex2DFastBicubicSampleLevel(TEXTURE2D_PARAM(tex, sampler_tex), float2 uv, int level)
167+
half4 tex2DFastBicubicSampleLevel(TEXTURE2D_PARAM(tex, samplertex), float2 uv, int level)
168168
{
169169
#if !defined(PLAT_QUEST) && defined(BICUBIC_LIGHTMAP)
170170
half width;
@@ -190,57 +190,57 @@
190190
half h0y = h0(fy);
191191
half h1y = h1(fy);
192192

193-
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D_LOD(tex, sampler_tex, (half2(px + h0x, py + h0y) * 1.0f / width), level) +
194-
g1x * SAMPLE_TEXTURE2D_LOD(tex, sampler_tex, (half2(px + h1x, py + h0y) * 1.0f / width), level)) +
195-
g1(fy) * (g0x * SAMPLE_TEXTURE2D_LOD(tex, sampler_tex, (half2(px + h0x, py + h1y) * 1.0f / width), level) +
196-
g1x * SAMPLE_TEXTURE2D_LOD(tex, sampler_tex, (half2(px + h1x, py + h1y) * 1.0f / width), level));
193+
half4 r = g0(fy) * (g0x * SAMPLE_TEXTURE2D_LOD(tex, samplertex, (half2(px + h0x, py + h0y) * 1.0f / width), level) +
194+
g1x * SAMPLE_TEXTURE2D_LOD(tex, samplertex, (half2(px + h1x, py + h0y) * 1.0f / width), level)) +
195+
g1(fy) * (g0x * SAMPLE_TEXTURE2D_LOD(tex, samplertex, (half2(px + h0x, py + h1y) * 1.0f / width), level) +
196+
g1x * SAMPLE_TEXTURE2D_LOD(tex, samplertex, (half2(px + h1x, py + h1y) * 1.0f / width), level));
197197
return r;
198198
#else
199-
return SAMPLE_TEXTURE2D_LOD(tex, sampler_tex, uv, level);
199+
return SAMPLE_TEXTURE2D_LOD(tex, samplertex, uv, level);
200200
#endif
201201
}
202202

203-
half getBakedNoise(TEXTURE2D_PARAM(noiseTex, sampler_noiseTex), float3 p)
203+
half getBakedNoise(TEXTURE2D_PARAM(noiseTex, samplernoiseTex), float3 p)
204204
{
205205
float3 i = floor(p); p -= i; p *= p * (3. - 2. * p);
206206
float2 uv = (p.xy + i.xy + float2(37, 17) * i.z + .5) / 256.;
207207
uv.y *= -1;
208-
p.xy = SAMPLE_TEXTURE2D_LOD(noiseTex, sampler_noiseTex, uv, 0).yx;
208+
p.xy = SAMPLE_TEXTURE2D_LOD(noiseTex, samplernoiseTex, uv, 0).yx;
209209
return lerp(p.x, p.y, p.z);
210210
}
211211

212212
// Manual bilinear sampling to avoid 8 bit precision issues
213213
// Based on https://www.shadertoy.com/view/MllSzX
214-
half getBakedNoiseBilinear(TEXTURE2D_PARAM(noiseTex, sampler_noiseTex), float4 texSize, float3 p)
214+
half getBakedNoiseBilinear(TEXTURE2D_PARAM(noiseTex, samplernoiseTex), float4 texSize, float3 p)
215215
{
216216
float3 i = floor(p); p -= i; p *= p * (3. - 2. * p);
217217
float2 uv = (p.xy + i.xy + float2(37, 17) * i.z + .5) / 256.;
218218
uv.y *= -1;
219-
219+
220220
float2 pixelUv = uv * texSize.zw + 0.5;
221221
float2 pixelUvFrac = frac(pixelUv);
222222

223223
pixelUv = (floor(pixelUv) / texSize.zw) - texSize.xy * 0.5;
224224

225-
float4 sample0 = SAMPLE_TEXTURE2D_LOD(noiseTex, sampler_noiseTex, pixelUv, 0);
226-
float4 sample1 = SAMPLE_TEXTURE2D_LOD(noiseTex, sampler_noiseTex, pixelUv + float2(texSize.x, 0), 0);
227-
float4 sample2 = SAMPLE_TEXTURE2D_LOD(noiseTex, sampler_noiseTex, pixelUv + float2(0, texSize.y), 0);
228-
float4 sample3 = SAMPLE_TEXTURE2D_LOD(noiseTex, sampler_noiseTex, pixelUv + float2(texSize.x, texSize.y), 0);
225+
float4 sample0 = SAMPLE_TEXTURE2D_LOD(noiseTex, samplernoiseTex, pixelUv, 0);
226+
float4 sample1 = SAMPLE_TEXTURE2D_LOD(noiseTex, samplernoiseTex, pixelUv + float2(texSize.x, 0), 0);
227+
float4 sample2 = SAMPLE_TEXTURE2D_LOD(noiseTex, samplernoiseTex, pixelUv + float2(0, texSize.y), 0);
228+
float4 sample3 = SAMPLE_TEXTURE2D_LOD(noiseTex, samplernoiseTex, pixelUv + float2(texSize.x, texSize.y), 0);
229229

230230
float4 filtered1 = lerp(sample0, sample1, pixelUvFrac.x);
231231
float4 filtered2 = lerp(sample2, sample3, pixelUvFrac.x);
232232
float4 filtered = lerp(filtered1, filtered2, pixelUvFrac.y);
233-
233+
234234
p.xy = filtered.yx;
235235
return lerp(p.x, p.y, p.z);
236236
}
237237

238-
half getBakedNoiseBicubic(TEXTURE2D_PARAM(noiseTex, sampler_noiseTex), float3 p)
238+
half getBakedNoiseBicubic(TEXTURE2D_PARAM(noiseTex, samplernoiseTex), float3 p)
239239
{
240240
float3 i = floor(p); p -= i; p *= p * (3. - 2. * p);
241241
float2 uv = (p.xy + i.xy + float2(37, 17) * i.z + .5) / 256.;
242242
uv.y *= -1;
243-
p.xy = tex2DFastBicubicSampleLevel(TEXTURE2D_ARGS(noiseTex, sampler_noiseTex), uv, 0).yx;
243+
p.xy = tex2DFastBicubicSampleLevel(TEXTURE2D_ARGS(noiseTex, samplernoiseTex), uv, 0).yx;
244244
return lerp(p.x, p.y, p.z);
245245
}
246246

@@ -249,7 +249,7 @@
249249
return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
250250
}
251251

252-
half3 ApplyLut2D(TEXTURE2D_PARAM(lut2d, sampler_lut2d), half3 uvw)
252+
half3 ApplyLut2D(TEXTURE2D_PARAM(lut2d, samplerlut2d), half3 uvw)
253253
{
254254
half3 scaleOffset = half3(1.0 / 1024.0, 1.0 / 32.0, 31.0);
255255
// Strip format where `height = sqrt(width)`
@@ -258,15 +258,15 @@
258258
uvw.xy = uvw.xy * scaleOffset.z * scaleOffset.xy + scaleOffset.xy * 0.5;
259259
uvw.x += shift * scaleOffset.y;
260260
uvw.xyz = lerp(
261-
SAMPLE_TEXTURE2D(lut2d, sampler_lut2d, uvw.xy).rgb,
262-
SAMPLE_TEXTURE2D(lut2d, sampler_lut2d, uvw.xy + half2(scaleOffset.y, 0.0)).rgb,
261+
SAMPLE_TEXTURE2D(lut2d, samplerlut2d, uvw.xy).rgb,
262+
SAMPLE_TEXTURE2D(lut2d, samplerlut2d, uvw.xy + half2(scaleOffset.y, 0.0)).rgb,
263263
uvw.z - shift
264264
);
265265
return uvw;
266266
}
267267

268268
half3 HSV2RGB(half3 hsv)
269-
{
269+
{
270270
return hsv.z + hsv.z * hsv.y * (clamp(abs(fmod(hsv.x * 6.0 + half3(0,4,2), 6.0) - 3.0) - 1.0,0.0,1.0) - 1.0);
271271
}
272272

@@ -521,7 +521,7 @@
521521
return source;
522522
}
523523

524-
524+
525525
float GLSLMod(float x, float y)
526526
{
527527
return (((x)-(y)*floor((x)/(y))));
@@ -554,7 +554,7 @@
554554
float D = 0.20;
555555
float E = 0.02;
556556
float F = 0.30;
557-
557+
558558
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
559559
}
560560

@@ -597,7 +597,7 @@
597597
return gamma_applied_linear_rgb;
598598
}
599599

600-
600+
601601
// https://github.com/MochiesCode/Mochies-Unity-Shaders/
602602
// MIT License
603603

@@ -651,4 +651,4 @@
651651
{
652652
return length(p) - r;
653653
}
654-
}
654+
}

0 commit comments

Comments
 (0)