OpenTTD Source 20261112-master-gbcbec5e63f
hlsl_shader.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
13#define _hlsl_cbuffer \
14R"STR(
15 cbuffer UniformConstantBuffer : register(b0)
16 {
17 float4 sprite;
18 float2 screen;
19 float zoom;
20 bool rgb;
21 bool crash;
22 };
23)STR"
24
26static const char _vertex_shader_sprite_hlsl[] =
28R"STR(
29 struct vs_out
30 {
31 float4 position : SV_POSITION;
32 float2 texcoord : TEXCOORD;
33 };
34
35 static const float4 vert_array[] = {
36 // x y u v
37 float4( 1.0, -1.0, 1.0, 1.0),
38 float4(-1.0, -1.0, 0.0, 1.0),
39 float4( 1.0, 1.0, 1.0, 0.0),
40 float4(-1.0, 1.0, 0.0, 0.0)
41 };
42
43 vs_out vs_main(uint id: SV_VertexID)
44 {
45 float2 size = sprite.zw / screen.xy;
46 float2 offset = ((2.0 * sprite.xy + sprite.zw) / screen.xy - 1.0) * float2(1.0, -1.0);
47
48 vs_out output;
49 output.texcoord = vert_array[id].zw;
50 output.position = float4(vert_array[id].xy * size + offset, 0.0, 1.0);
51
52 return output;
53 };
54)STR";
55
57static const char _frag_shader_direct_hlsl[] =
58R"STR(
59 uniform Texture2D colour_tex : register(t0);
60 uniform SamplerState texture_sampler : register(s0);
61
62 struct vs_out
63 {
64 float4 position : SV_POSITION;
65 float2 texcoord : TEXCOORD;
66 };
67
68 float4 ps_main(vs_out input) : SV_TARGET
69 {
70 return colour_tex.Sample(texture_sampler, input.texcoord);
71 };
72)STR";
73
75static const char _frag_shader_palette_hlsl[] =
76R"STR(
77 uniform Texture2D colour_tex : register(t0);
78 uniform Texture1D palette : register(t1);
79 uniform SamplerState texture_sampler : register(s0);
80
81 struct vs_out
82 {
83 float4 position : SV_POSITION;
84 float2 texcoord : TEXCOORD;
85 };
86
87 float4 ps_main(vs_out input) : SV_TARGET
88 {
89 float idx = colour_tex.Sample(texture_sampler, input.texcoord).x;
90 return palette.Sample(texture_sampler, idx);
91 };
92)STR";
93
95#define _frag_shader_remap_func_hlsl \
96R"STR(
97 float max3(float3 v)
98 {
99 return max(max(v.x, v.y), v.z);
100 }
101
102 float3 adj_brightness(float3 colour, float3 brightness)
103 {
104 float3 adj = colour * (brightness > 0.0 ? brightness / 0.5 : 1.0);
105 float3 ob_vec = clamp(adj - 1.0, 0.0, 1.0);
106 float ob = (ob_vec.r + ob_vec.g + ob_vec.b) / 2.0;
107 return clamp(adj + ob * (1.0 - adj), 0.0, 1.0);
108 }
109)STR"
110
115R"STR(
116 uniform Texture2D colour_tex : register(t0);
117 uniform Texture1D palette : register(t1);
118 uniform Texture2D remap_tex : register(t2);
119 uniform SamplerState texture_sampler : register(s0);
120
121 struct vs_out
122 {
123 float4 position : SV_POSITION;
124 float2 texcoord : TEXCOORD;
125 };
126
127 float4 ps_main(vs_out input) : SV_TARGET
128 {
129 float idx = remap_tex.SampleLevel(texture_sampler, input.texcoord, zoom).r;
130 float4 remap_col = palette.Sample(texture_sampler, idx);
131 float4 rgb_col = colour_tex.SampleLevel(texture_sampler, input.texcoord, zoom);
132
133 float4 output;
134 output.a = rgb ? rgb_col.a : remap_col.a;
135 output.rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;
136
137 return output;
138 };
139)STR";
140
145R"STR(
146 uniform Texture2D colour_tex : register(t0);
147 uniform Texture1D palette : register(t1);
148 uniform Texture2D remap_tex : register(t2);
149 uniform Texture1D pal : register(t3);
150 uniform SamplerState texture_sampler : register(s0);
151
152 struct vs_out
153 {
154 float4 position : SV_POSITION;
155 float2 texcoord : TEXCOORD;
156 };
157
158 float4 ps_main(vs_out input) : SV_TARGET
159 {
160 float idx = remap_tex.SampleLevel(texture_sampler, input.texcoord, zoom).r;
161 float r = pal.Sample(texture_sampler, idx).r;
162 float4 remap_col = palette.Sample(texture_sampler, r);
163 float4 rgb_col = colour_tex.SampleLevel(texture_sampler, input.texcoord, zoom);
164
165 if (crash && idx == 0.0)
166 rgb_col.rgb = float2(dot(rgb_col.rgb, float3(0.199325561523, 0.391342163085, 0.076004028320)), 0.0).rrr;
167
168 float4 output;
169 output.a = rgb && (r > 0.0 || idx == 0.0) ? rgb_col.a : remap_col.a;
170 output.rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;
171
172 return output;
173 };
174)STR";
static const char _frag_shader_palette_hlsl[]
HLSL pixel shader that reads the fragment colour from a 32bpp texture.
Definition hlsl_shader.h:75
static const char _frag_shader_direct_hlsl[]
HLSL pixel shader that reads the fragment colour from a 32bpp texture.
Definition hlsl_shader.h:57
#define _hlsl_cbuffer
Defines a constant buffer for HLSL shaders that contains the sprite position, screen size,...
Definition hlsl_shader.h:13
static const char _vertex_shader_sprite_hlsl[]
HLSL vertex shader that positions a sprite on screen.
Definition hlsl_shader.h:26
#define _frag_shader_remap_func_hlsl
Pixel shader function for remap brightness modulation.
Definition hlsl_shader.h:95
static const char _frag_shader_rgb_mask_blend_hlsl[]
HLSL pixel shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char _frag_shader_sprite_blend_hlsl[]
HLSL pixel shader that performs a palette lookup to read the colour from a sprite texture.