Commit Graph

27 Commits

Author SHA1 Message Date
dan elksnitis
be2a80c92b [shaders/stb_preprocess] stb preprocessor bug fixes:
- fix line number mismatches caused due to multiline macro arguments by keeping leading/trailing whitespaces in cases of multiple lines encountered in a single macro argument (single line macro args still have whitespace trimmed as this is needed for token pasting)
- avoid early out preventing parsing of "defined" builtin macro outside of #if conditions; this allows using defined in #define directives (among other places)

#rb Yuriy.ODonnell

[CL 32051438 by dan elksnitis in ue5-main branch]
2024-03-06 09:07:22 -05:00
christopher waters
15bede992d Entire engine compiling with -DisableUnity -IncludeHeaders
[CL 31778133 by christopher waters in ue5-main branch]
2024-02-23 16:51:32 -05:00
henrik karlsson
e75785aaf0 [ShaderPreprocessor]
* Reverted change that should never have checked in that disabled sse opts

[CL 31651792 by henrik karlsson in ue5-main branch]
2024-02-20 13:57:20 -05:00
henrik karlsson
9983643fb9 Compile error fixes for iwyu when compiling UnrealEditor
#rb christopher.waters (and phil.pizlo for verse files)


#changelist validated
#virtualized

[CL 31228054 by henrik karlsson in ue5-main branch]
2024-02-06 15:28:47 -05:00
jason hoerner
903ff2eea7 Shader preprocessor: Fix for ASAN failure -- need to add SSE padding to substitution text generated for TEXT macros.
#jira UE-202569
#rnx
#rb dan.elksnitis

[CL 30331769 by jason hoerner in ue5-main branch]
2023-12-14 16:35:09 -05:00
marc audy
399bcf9971 Disable PVS warning V758
Silence V570 false positives for bit field assignments
Silence various other PVS warnings
#rnx

[CL 29706746 by marc audy in ue5-main branch]
2023-11-14 00:29:43 -05:00
josh adams
92f54ebbbc - Worked around crash in shader preprocessor on mac
#rb Jason.Hoerner

[CL 29612445 by josh adams in ue5-main branch]
2023-11-09 17:28:23 -05:00
jason hoerner
92db7f3070 Shader Preprocessor: Fix preprocessing for shader asserts. Need to preprocess the contents of TEXT macros to handle __FILE__ macros, or other possible macro generated text.
#rnx
#rb dan.elksnitis

[CL 29538506 by jason hoerner in ue5-main branch]
2023-11-07 18:01:37 -05:00
jason hoerner
9852294945 Shader Preprocessor: ASAN bug fix for stack use after return for undef_map identifiers, by enabling arena allocation. Strip out whitespace on blank lines -- doesn't measurably affect perf, and could improve deduplication or avoid certain shader invalidations, and makes stripped source more readable. Fix bug where line numbers aren't incremented when exiting the scanning loop on an identifier.
#jira UE-198065
#rnx
#rb dan.elksnitis yuriy.odonnell

[CL 29100213 by jason hoerner in ue5-main branch]
2023-10-25 16:04:13 -04:00
jason hoerner
0e18ea7e81 Shader Preprocessor: Roll TEXT macro substitution into the C preprocessor step, via custom macro callback feature, making it effectively free. Saves 3% overall on preprocess, but also represents a simplification by reducing the number of unique preprocess passes.
#jira UE-198496
#rnx
#rb dan.elksnitis yuriy.odonnell jason.nadro

[CL 29100104 by jason hoerner in ue5-main branch]
2023-10-25 16:01:11 -04:00
jason hoerner
bae18a5cc0 Shader Preprocessor platform specific compile error / warning fixes. Unused branch label on non-SSE platforms, char array index, conversion of 255 to char in SSE vector initialization (used by unsigned compare intrinsic so code was correct), strcpy_s not supported on some platforms -- switched to strncpy.
#rnx

[CL 28835617 by jason hoerner in ue5-main branch]
2023-10-17 07:01:26 -04:00
jason hoerner
2d5091c15e Shader Preprocessor: Early bloom filter and SSE optimizations. Overall 27.9% improvement to low level preprocessor, or 10.4% to ConditionalPreprocessShader as a whole.
* Moved identifier copy and macro bloom filter from maybe_expand_macro to test into copy_to_action_point / copy_to_action_point_macro_expansion.  13.1% of improvement.
* SSE implementation of scan_to_directive, 10x faster, 5.2%
* SSE implementation of identifier copy, 3x faster, 4.5%
* SSE ShaderConvertAndStripComments, 4x faster, 3.6%
* Fast inline string equality comparison, 4x faster, 1.5%

To make SSE implementations "safe" without needing special cases near the end of a buffer, it's necessary to ensure padding is present in the relevant buffers, anything that goes through a preprocess_string call.  This includes the string arena allocator, temporary stbds arrays that hold strings, and file buffers passed in.  The latter all pass through ShaderConvertAndStripComments, where we can add padding.  (ShaderConvertAndStripComments itself has special cases for end of buffer).  Code related to original 1 and 2 character macro filter removed, since I can't see a reason to enable it over the bloom filter.

I also attempted SSE optimization of copy_to_action_point and copy_line_without_comments, but improvement wasn't big enough to be worth the complexity (around 2% for the former, but massive code complexity, 0.5% for the latter).  That's pretty much everything SSE friendly that's over 1% on a profile, although I think copy_argument can be made a lot faster, not primarily through SSE.

#jira UE-197212
#rnx
#rb yuriy.odonnell jason.nadro

[CL 28834324 by jason hoerner in ue5-main branch]
2023-10-17 05:18:57 -04:00
jason hoerner
312825f3d0 Build fix for Mac ARM64: inline functions in cond_expr.c produced link errors, not sure why, but force inlining them should fix the issue.
[CL 28538832 by jason hoerner in ue5-main branch]
2023-10-06 10:09:38 -04:00
jason hoerner
b5db1a507e Shader Preprocessor optimizations second pass: 10.7% overall improvement to ConditionalPreprocessShader, or 23.1% improvement to lower level PreprocessShader pass alone, factoring out other passes like dead code removal not affected by this CL.
* Low overhead bloom filter for identifier hash lookups in maybe_expand_macro  (3.1%)
* Optimized inline version of library calls (isspace, isalnum), and avoid strtoul for single digit numbers in evaluate_if   (2.6%)
* Array reserve function inlining  (1.7%)
* Fetch output size from preprocessor library instead of calling strlen  (1.2%)
* Avoid FName -> string -> FName round trip conversion of key in FShaderCompilerEnvironment::Merge   (0.3%)
* Use string concatentation instead of printf and inline storage in AddStbDefines   (0.3%)
* Other overhead reduction (remove unused fast_dest feature, smaller stbds_array_header struct, savings from inlining)    (1.5%)

#rnx
#rb dan.elksnitis jason.nadro

[CL 28537574 by jason hoerner in ue5-main branch]
2023-10-06 09:14:35 -04:00
jason hoerner
57a469b8d9 Build fix -- preprocessor compile error due to assigning a variable twice in one statement (once in the macro, once in the code). Second instance in the code, doh!
#rnx

[CL 28221361 by jason hoerner in ue5-main branch]
2023-09-26 07:30:24 -04:00
jason hoerner
fb2a55e5b7 Build fix -- preprocessor compile error due to assigning a variable twice in one statement (once in the macro, once in the code).
#rnx

[CL 28221307 by jason hoerner in ue5-main branch]
2023-09-26 07:29:25 -04:00
jason hoerner
046ace59ff ShaderCompiler: Preprocessor optimizations, first pass. Saves 16.7% overall on ConditionalPreprocessShader.
* Inline array memory allocation added to low level preprocessor for output and various temporary buffers to reduce dynamic memory allocation and reallocation overhead.  Saved 4.6%.
* FShaderPreprocessOutput::StripCode optimized to write to FString as TCHAR array, rather than using AppendChar (over 4x speedup).  Saved 2.9%
* Shader source file cache now also stores stripped and ANSI converted source, to avoid need to convert and strip the source, plus allocating a copy is avoided.  Saved 4.3%
* Uniform buffer structure declarations stored as ANSI converted source, avoiding convert and copy.  Saved 4.9%

#rnx
#rb dan.elksnitis jason.nadro

[CL 28219741 by jason hoerner in ue5-main branch]
2023-09-26 05:29:05 -04:00
dan elksnitis
6cc4950575 [shaders]
- modify versioning mechanisms to use UEMETADATA pragmas
- update commands which generate version.ush files to generate such pragmas instead of comments
- include the above pragma-driven version(s) in the input hash when preprocessed job cache is enabled (not needed for the disabled case since as before the entire contents of the version files are hashed)
- fix bug in stb_preprocessor that was causing it to stop expanding macros if any diagnostic was encountered (and rename the array field storing diagnostic messages to diagnostics, since it contains more than just errors)

#rb Yuriy.ODonnell

[CL 27515060 by dan elksnitis in ue5-main branch]
2023-08-31 04:49:18 -04:00
dan elksnitis
a21eb84e66 [shader preprocessor] implement #error directives
#rb Jason.Nadro
#preflight 644931ee1c2846595c0887c6

[CL 25199050 by dan elksnitis in ue5-main branch]
2023-04-26 11:23:31 -04:00
Jeremy Moore
3e7f174035 STB processor reemits line directives.
#preflight 63c838c902024f93d8dc9534

[CL 23760433 by Jeremy Moore in ue5-main branch]
2023-01-18 13:37:36 -05:00
dan elksnitis
93c67debf3 [shaders] move stb_ds and stb_alloc implementations to a .c file rather than using the single-header #define'd implementation pattern. IncludeTool doesn't like this.
#preflight 6388cb61cb3b75418301b20a
#rb Yuriy.ODonnell

[CL 23353516 by dan elksnitis in ue5-main branch]
2022-12-01 11:15:26 -05:00
dan elksnitis
7be75b5184 [shaders] fix missing closing " in stb header error msg (this path should not be hit but it's causing problems for IncludeTool static analyzer)
#rb trivial
#preflight none

[CL 23310769 by dan elksnitis in ue5-main branch]
2022-11-29 11:44:01 -05:00
dan elksnitis
4803fda014 [stb_preprocess]
- add handling of #pragma directives in macro definitions
- add handling of #pragma message to create diagnostic outputs
- add and update comments in preprocessor.h
- unifying memory allocation mechanisms between stb_ds, stb_alloc and preprocessor - allow configuring all functions performing memory allocation/freeing via macros which should be specified via a header whose include path is specified via the STB_CONFIG macro (this will be included in stb_common.h if defined)
- don't pass an stb_arena to the include resolution callback; this is unnecessary complexity (implementation can handle its own memory allocation as long as the resulting strings have the appropriate lifetime) and indeed the arena used was global (and so not threadsafe)
- expose function allowing external modification of error modes so we can disable the error case for preprocessor directives not being at the start of a line (and so handle the case where we #define a macro whose value is a #pragma, or any other string possibly containing a # that we want to pass unmodified to the output i.e. the case of #define COMPILER_DEFINE #define which we use to keep #defines in the preprocessed code for handling by the platform compilers)
- fix leak of pp diagnostic messages
- remove internal caches for include resolution; they are problematic since (a) they aren't threadsafe, and (b) the caching of resolved filenames based on the include path found in source is inherently problematic (can potentially cause issues with same-named files in different folders included via relative paths)
- strip all code related to explicit file loading and lists of include paths; this has no purpose for our use case. note this makes the callbacks for file load/unload and include resolve mandatory, comments in preprocessor.h updated to reflect this.
- fix a few other memory leaks

#preflight 638611a9fa053c489a54b2ec
#rb Yuriy.ODonnell

[CL 23305324 by dan elksnitis in ue5-main branch]
2022-11-29 09:34:03 -05:00
dan elksnitis
6aa37eca15 [shaders] additions & fixes to STB preprocessor for shader preprocessing. With the following all Lyra shaders compile and generate identical bytecode on PC SM5/6.
- add explicit extern "C" on preprocessor methods to allow linking from C++ code
- add custom callbacks for include path resolution and file loading/unloading
- add padding space following macro expansion ending with a > to work around DXC bug (not handling >> correctly if it occurs at the end of a templated type)
- adding explicit handling for HLSL infinity constant (1.#INF); the # character outside of a directive was previously putting the preprocessor into an error state
- change line directive output to use #line format rather than # (DXC is ok with the latter but minifier doesn't currently handle it), and also always include the filename (so when minifying don't have to be careful not to strip the line directive with the filename in it); this also matches mcpp behaviour
- adding const-correctness and casts where needed to avoid const casts/compile errors in calling C++ code and satisfy Mac/Intellisense compilers
- bugfix: parsing of '.' was immediately putting the scanning state machine into "number" mode resulting in skipped macro expansion in code of the form "var.MACRO.var2"/numbers; added extra state transitions and changing the default transition when encountering '.' to an explicit mode that can allow either transitioning to parsing identifiers or numbers depending on following characters.
- bugfix: macros with an empty parameter list were incorrectly terminating said list on expansion (adding extra rparens)
- bugfix: set file load mode as "callback" when callback used for loading, and check the mode in the free callbacks
- various minor changes to satisfy Clang code analysis

#preflight 637e3391e30d438849ddf902
#rb Yuriy.ODonnell

[CL 23249332 by dan elksnitis in ue5-main branch]
2022-11-23 09:58:24 -05:00
dan elksnitis
e01c10c613 [shaders] resubmit stb preprocessor code with static analysis fixes
#rb Jason.Nadro
#preflight 636d54ab1a66e7f73189e89f

[CL 23134601 by dan elksnitis in ue5-main branch]
2022-11-15 08:26:32 -05:00