23
23
#include " vk_shader.h"
24
24
#include " vk_ppshader.h"
25
25
#include " vulkan/vk_renderdevice.h"
26
+ #include " vulkan/pipelines/vk_renderpass.h"
26
27
#include < zvulkan/vulkanbuilders.h>
27
28
#include " hw_shaderpatcher.h"
28
29
#include " filesystem.h"
@@ -224,7 +225,17 @@ static std::vector<BuiltinFieldDesc> fragShaderOutputs
224
225
{" FragNormal" , " " , UniformType::Vec4, FieldCondition::GBUFFER_PASS}, // 2
225
226
};
226
227
227
- void AddFields (FString &layoutBlock, int &index, bool is_in, const std::vector<VaryingFieldDesc> &fields)
228
+ static void AddVertexInFields (VulkanRenderDevice* fb, FString& layoutBlock, const VkShaderKey& key)
229
+ {
230
+ const VkVertexFormat& vfmt = *fb->GetRenderPassManager ()->GetVertexFormat (key.VertexFormat );
231
+ for (const FVertexBufferAttribute& attr : vfmt.Attrs )
232
+ {
233
+ const VaryingFieldDesc& desc = vertexShaderInputs[attr.location ];
234
+ layoutBlock.AppendFormat (" layout(location = %d) %s %s %s %s;\n " , attr.location , desc.Property .GetChars (), " in" , GetTypeStr (desc.Type ), desc.Name .GetChars ());
235
+ }
236
+ }
237
+
238
+ static void AddFields (FString &layoutBlock, int &index, bool is_in, const std::vector<VaryingFieldDesc> &fields)
228
239
{
229
240
for (auto &field : fields)
230
241
{
@@ -233,7 +244,7 @@ void AddFields(FString &layoutBlock, int &index, bool is_in, const std::vector<V
233
244
}
234
245
}
235
246
236
- void AddBuiltinFields (FString &layoutBlock, int &index, bool is_in, const std::vector<BuiltinFieldDesc> &fields, const VkShaderKey& key, bool hasClipDistance)
247
+ static void AddBuiltinFields (FString &layoutBlock, int &index, bool is_in, const std::vector<BuiltinFieldDesc> &fields, const VkShaderKey& key, bool hasClipDistance)
237
248
{
238
249
for (auto &field : fields)
239
250
{
@@ -294,8 +305,7 @@ void VkShaderManager::BuildLayoutBlock(FString &layoutBlock, bool isFrag, const
294
305
295
306
if (!isFrag)
296
307
{
297
- int index = 0 ;
298
- AddFields (layoutBlock, index, true , vertexShaderInputs);
308
+ AddVertexInFields (fb, layoutBlock, key);
299
309
}
300
310
301
311
{
@@ -314,11 +324,6 @@ void VkShaderManager::BuildLayoutBlock(FString &layoutBlock, bool isFrag, const
314
324
int index = 0 ;
315
325
AddBuiltinFields (layoutBlock, index, false , fragShaderOutputs, key, hasClipDistance);
316
326
}
317
-
318
- layoutBlock << " #line 1\n " ;
319
-
320
- layoutBlock << LoadPrivateShaderLump (" shaders/scene/layout_shared.glsl" ).GetChars () << " \n " ;
321
-
322
327
}
323
328
324
329
void VkShaderManager::BuildDefinesBlock (FString &definesBlock, const char *defines, bool isFrag, const VkShaderKey& key, const UserShaderDesc *shader)
@@ -426,6 +431,19 @@ void VkShaderManager::BuildDefinesBlock(FString &definesBlock, const char *defin
426
431
if (key.UseSpriteCenter ) definesBlock << " #define USE_SPRITE_CENTER\n " ;
427
432
428
433
definesBlock << ((key.Simple2D ) ? " #define uFogEnabled -3\n " : " #define uFogEnabled 0\n " );
434
+
435
+ // Setup fake variables for the 'in' attributes that aren't actually available because the garbage shader code thinks they exist
436
+ // God I hate this engine... :(
437
+ std::vector<bool > definedFields (vertexShaderInputs.size ());
438
+ bool hasNormal = false ;
439
+ const VkVertexFormat& vfmt = *fb->GetRenderPassManager ()->GetVertexFormat (key.VertexFormat );
440
+ for (const FVertexBufferAttribute& attr : vfmt.Attrs )
441
+ definedFields[attr.location ] = true ;
442
+ for (size_t i = 0 ; i < vertexShaderInputs.size (); i++)
443
+ {
444
+ if (!definedFields[i])
445
+ definesBlock << " #define " << vertexShaderInputs[i].Name << " " << GetTypeStr (vertexShaderInputs[i].Type ) << " (0)\n " ;
446
+ }
429
447
}
430
448
431
449
std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader (FString shadername, const char *vert_lump, const char *vert_lump_custom, const char *defines, const VkShaderKey& key, const UserShaderDesc *shader)
@@ -436,7 +454,6 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
436
454
FString layoutBlock;
437
455
BuildLayoutBlock (layoutBlock, false , key, shader);
438
456
439
-
440
457
FString codeBlock;
441
458
codeBlock << LoadPrivateShaderLump (vert_lump).GetChars () << " \n " ;
442
459
if (vert_lump_custom)
@@ -454,7 +471,8 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
454
471
.DebugName (shadername.GetChars ())
455
472
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
456
473
.AddSource (" DefinesBlock" , definesBlock.GetChars ())
457
- .AddSource (" shaders/scene/layout_shared.glsl" , layoutBlock.GetChars ())
474
+ .AddSource (" LayoutBlock" , layoutBlock.GetChars ())
475
+ .AddSource (" shaders/scene/layout_shared.glsl" , LoadPrivateShaderLump (" shaders/scene/layout_shared.glsl" ).GetChars ())
458
476
.AddSource (vert_lump_custom ? vert_lump_custom : vert_lump, codeBlock.GetChars ())
459
477
.OnIncludeLocal ([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude (headerName.c_str (), includerName.c_str (), depth, false ); })
460
478
.OnIncludeSystem ([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude (headerName.c_str (), includerName.c_str (), depth, true ); })
@@ -469,7 +487,6 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
469
487
FString layoutBlock;
470
488
BuildLayoutBlock (layoutBlock, true , key, shader);
471
489
472
-
473
490
FString codeBlock;
474
491
codeBlock << LoadPrivateShaderLump (frag_lump).GetChars () << " \n " ;
475
492
@@ -554,6 +571,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
554
571
.AddSource (" VersionBlock" , GetVersionBlock ().GetChars ())
555
572
.AddSource (" DefinesBlock" , definesBlock.GetChars ())
556
573
.AddSource (" LayoutBlock" , layoutBlock.GetChars ())
574
+ .AddSource (" shaders/scene/layout_shared.glsl" , LoadPrivateShaderLump (" shaders/scene/layout_shared.glsl" ).GetChars ())
557
575
.AddSource (" shaders/scene/includes.glsl" , LoadPrivateShaderLump (" shaders/scene/includes.glsl" ).GetChars ())
558
576
.AddSource (mateffectname.GetChars (), mateffectBlock.GetChars ())
559
577
.AddSource (materialname.GetChars (), materialBlock.GetChars ())
0 commit comments