;*********************************************************************************************
;SHADOWS, LIGHTS, REFLECTIONS AND DECALS 1
[ShaderRegex_Shadows1]
shader_model = ps_4_0 ps_5_0 cs_5_0
temps = stereo tmp1 tmp2 tmp3
ResourceInput = ref ps-cb0
run = CustomShaderInverseMatrix_cb0
;ResourceBackupPSCB13 = ref ps-cb13
ps-cb13 = copy ResourceInvViewMatrix
;post ps-cb13 = ResourceBackupPSCB13

[ShaderRegex_Shadows1.Pattern]
;mov (?P<pos1>r\d+)\.w, l\(1\.000000\)\n
\s*dp4 r\d+\.x, (?P<pos1>r\d+)\.xyzw, cb0\[53]\.xyzw\n
\s*dp4 r\d+\.y, (?P=pos1)\.xyzw, cb0\[54]\.xyzw\n
\s*dp4 r\d+\.z, (?P=pos1)\.xyzw, cb0\[55]\.xyzw\n
\s*dp4 (?P<depth>r\d+)\.(?P<swizzle>[xyzw]), (?P=pos1)\.xyzw, cb0\[(?P<number>\d+)\]\.xyzw\n
\s*div (?P<pos>r\d+)\.xyz, r\d+\.xyzx, (?P=depth)\.(?P=swizzle)(?P=swizzle)(?P=swizzle)(?P=swizzle)\n

[ShaderRegex_Shadows1.Pattern.Replace]
\n
${0}
\n
// DHR ShaderRegex:\n
ld_indexable(texture2d)(float,float,float,float) ${stereo}.xyzw, l(0, 0, 0, 0), t125.xyzw\n
//Translate to clip\n
mov ${tmp3}.xyz, ${pos}.xyz\n
mov ${tmp3}.w, l(1.000000)\n
dp4 ${tmp1}.x, ${tmp3}.xyzw, cb13[0].xyzw\n
dp4 ${tmp1}.y, ${tmp3}.xyzw, cb13[1].xyzw\n
dp4 ${tmp1}.z, ${tmp3}.xyzw, cb13[2].xyzw\n
dp4 ${tmp1}.w, ${tmp3}.xyzw, cb13[3].xyzw\n
//Fix\n
add ${tmp2}.w, ${tmp1}.w, -${stereo}.y\n
mul ${tmp2}.w, ${stereo}.x, ${tmp2}.w\n
add ${tmp1}.x, ${tmp1}.x, -${tmp2}.w\n
//Matrix\n
dp4 ${tmp3}.x, ${tmp1}.xyzw, cb0[53].xyzw\n
dp4 ${tmp3}.y, ${tmp1}.xyzw, cb0[54].xyzw\n
dp4 ${tmp3}.z, ${tmp1}.xyzw, cb0[55].xyzw\n
dp4 ${tmp3}.w, ${tmp1}.xyzw, cb0[56].xyzw\n
mov ${pos}.xyzw, ${tmp3}.xyzw\n
\n

[ShaderRegex_Shadows1.InsertDeclarations]
dcl_constantbuffer cb13[4], immediateIndexed
dcl_resource_texture2d (float,float,float,float) t125

;*********************************************************************************************
; LIGHTS SHAFTS AND VOLUMETRIC FOG
[ShaderRegex_Lights_Shafts1]
shader_model = ps_4_0 ps_5_0
temps = stereo tmp1 tmp2

[ShaderRegex_Lights_Shafts1.Pattern]
(?P<section1>
mov (?P<pos>r\d+)\.xy, v1\.zwzz\n
)
(?P<section2>
mov (?P=pos)\.zw, l\(0,0,1\.000000,1\.000000\)\n
)
(?P<section3>
\s*dp4 (?P<pos_x>r\d+)\.x, (?P=pos)\.xyww, cb0\[53]\.xyzw\n
\s*dp4 (?P=pos_x)\.y, (?P=pos)\.xyww, cb0\[54]\.xyzw\n
\s*dp4 (?P=pos_x)\.z, (?P=pos)\.xyww, cb0\[55]\.xyzw\n
\s*dp4 (?P<depth>r\d+)\.(?P<swizzle>[xyzw]), (?P=pos)\.xyzw, cb0\[(?P<number>\d+)\]\.xyzw\n
\s*div (?P=pos)\.xyz, (?P=pos_x)\.xyzx, (?P=depth)\.(?P=swizzle)(?P=swizzle)(?P=swizzle)(?P=swizzle)\n
)

[ShaderRegex_Lights_Shafts1.Pattern.Replace]
sample_l_indexable(texture2d)(float,float,float,float) ${pos}.z, v1.xyzx, t1.yzxw, s1, l(0.000000)\n
${section1}
mov ${pos}.w, l(1.000000)\n
\n
// Pattern Fix for Lights Shafts 1:\n
ld_indexable(texture2d)(float,float,float,float) ${stereo}.xyzw, l(0, 0, 0, 0), t125.xyzw\n
dp4 ${tmp2}.${swizzle}, ${pos}.xyzw, cb0[${number}].xyzw\n
mul ${tmp1}.x ${stereo}.y, ${tmp2}.${swizzle}\n
add ${tmp1}.x, ${tmp1}.x, l(-1.0)\n
mul ${tmp1}.x, ${stereo}.x, ${tmp1}.x\n
add ${pos}.x, ${pos}.x, ${tmp1}.x\n
\n
${section3}
;dp4 ${pos_x}.x, ${pos}.xyzw, cb0[53].xyzw\n
;dp4 ${pos_x}.y, ${pos}.xyzw, cb0[54].xyzw\n
;dp4 ${pos_x}.z, ${pos}.xyzw, cb0[55].xyzw\n
;dp4 ${depth}.${swizzle}, ${pos}.xyzw, cb0[56].xyzw\n
;div ${pos}.xyz, ${pos_x}.xyzx, ${depth}.${swizzle}${swizzle}${swizzle}${swizzle}\n


[ShaderRegex_Lights_Shafts1.InsertDeclarations]
dcl_resource_texture2d (float,float,float,float) t125


;***********************************************************************************
; INVERSE MATRIX FOR SHADERREGEX
[ResourceInvViewMatrix]
type = Buffer
format = R32G32B32A32_FLOAT
array = 4

[ResourceInput]
max_copies_per_frame = 1

[ResourceBackupCSCB0]
[ResourceBackupCSU0]
[ResourceBackupPSCB13]

[CustomShaderInverseMatrix_cb0]
cs = ShaderFixes/inverse-cs-cb0.hlsl
ResourceBackupCSCB0 = ref cs-cb0
ResourceBackupCSU0 = ref cs-u0
; Input constant buffer - the matrix to inverse is here:
cs-cb0 = ResourceInput
; Output buffer - this will hold the result:
cs-u0 = ResourceInvViewMatrix
Dispatch = 1, 1, 1
;post cs-cb0 = ResourceBackupCSCB0
;post cs-cb0 = null
;post cs-u0 = ResourceBackupCSU0