| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/ShaderCompiler/ShaderProgramParser.h>
- namespace anki
- {
- #define ANKI_PP_ERROR_MALFORMED() \
- ANKI_SHADER_COMPILER_LOGE("%s: Malformed expression: %s", fname.cstr(), line.cstr()); \
- return Error::USER_DATA
- #define ANKI_PP_ERROR_MALFORMED_MSG(msg_) \
- ANKI_SHADER_COMPILER_LOGE("%s: " msg_ ": %s", fname.cstr(), line.cstr()); \
- return Error::USER_DATA
- static const Array<CString, U32(ShaderType::COUNT)> SHADER_STAGE_NAMES = {
- {"VERTEX", "TESSELLATION_CONTROL", "TESSELLATION_EVALUATION", "GEOMETRY", "FRAGMENT", "COMPUTE", "RAY_GEN",
- "ANY_HIT", "CLOSEST_HIT", "MISS", "INTERSECTION", "CALLABLE"}};
- static const char* SHADER_HEADER = R"(#version 460 core
- #define ANKI_%s_SHADER 1
- #define ANKI_OS_ANDROID %d
- #define ANKI_OS_WINDOWS %d
- #define ANKI_OS_LINUX %d
- #define _ANKI_SUPPORTS_64BIT !ANKI_OS_ANDROID
- #define gl_VertexID gl_VertexIndex
- #extension GL_EXT_control_flow_attributes : require
- #define ANKI_UNROLL [[unroll]]
- #define ANKI_LOOP [[dont_unroll]]
- #define ANKI_BRANCH [[branch]]
- #define ANKI_FLATTEN [[flatten]]
- #extension GL_KHR_shader_subgroup_vote : require
- #extension GL_KHR_shader_subgroup_ballot : require
- #extension GL_KHR_shader_subgroup_shuffle : require
- #extension GL_KHR_shader_subgroup_arithmetic : require
- #extension GL_EXT_samplerless_texture_functions : require
- #extension GL_EXT_shader_image_load_formatted : require
- #extension GL_EXT_nonuniform_qualifier : enable
- #extension GL_EXT_buffer_reference : enable
- #extension GL_EXT_buffer_reference2 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_float32 : enable
- #if _ANKI_SUPPORTS_64BIT
- #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable
- #extension GL_EXT_shader_explicit_arithmetic_types_float64 : enable
- #extension GL_EXT_shader_atomic_int64 : enable
- #extension GL_EXT_shader_subgroup_extended_types_int64 : enable
- #endif
- #extension GL_EXT_nonuniform_qualifier : enable
- #extension GL_EXT_scalar_block_layout : enable
- #define ANKI_MAX_BINDLESS_TEXTURES %uu
- #define ANKI_MAX_BINDLESS_IMAGES %uu
- #if defined(ANKI_RAY_GEN_SHADER) || defined(ANKI_ANY_HIT_SHADER) || defined(ANKI_CLOSEST_HIT_SHADER) || defined(ANKI_MISS_SHADER) || defined(ANKI_INTERSECTION_SHADER) || defined(ANKI_CALLABLE_SHADER)
- # extension GL_EXT_ray_tracing : enable
- #endif
- #define ANKI_BINDLESS_SET(set_) \
- layout(set = set_, binding = 0) uniform utexture2D u_bindlessTextures2dU32[ANKI_MAX_BINDLESS_TEXTURES]; \
- layout(set = set_, binding = 0) uniform itexture2D u_bindlessTextures2dI32[ANKI_MAX_BINDLESS_TEXTURES]; \
- layout(set = set_, binding = 0) uniform texture2D u_bindlessTextures2dF32[ANKI_MAX_BINDLESS_TEXTURES]; \
- layout(set = set_, binding = 1) uniform readonly uimage2D u_bindlessImages2dU32[ANKI_MAX_BINDLESS_IMAGES]; \
- layout(set = set_, binding = 1) uniform readonly iimage2D u_bindlessImages2dI32[ANKI_MAX_BINDLESS_IMAGES]; \
- layout(set = set_, binding = 1) uniform readonly image2D u_bindlessImages2dF32[ANKI_MAX_BINDLESS_IMAGES]
- #define F32 float
- #define _ANKI_SIZEOF_float 4u
- #define Vec2 vec2
- #define _ANKI_SIZEOF_vec2 8u
- #define Vec3 vec3
- #define _ANKI_SIZEOF_vec3 12u
- #define Vec4 vec4
- #define _ANKI_SIZEOF_vec4 16u
- #define F16 float16_t
- #define _ANKI_SIZEOF_float16_t 2u
- #define HVec2 f16vec2
- #define _ANKI_SIZEOF_f16vec2 4u
- #define HVec3 f16vec3
- #define _ANKI_SIZEOF_f16vec3 6u
- #define HVec4 f16vec4
- #define _ANKI_SIZEOF_f16vec4 8u
- #define U8 uint8_t
- #define _ANKI_SIZEOF_uint8_t 1u
- #define U8Vec2 u8vec2
- #define _ANKI_SIZEOF_u8vec2 2u
- #define U8Vec3 u8vec3
- #define _ANKI_SIZEOF_u8vec3 3u
- #define U8Vec4 u8vec4
- #define _ANKI_SIZEOF_u8vec4 4u
- #define I8 int8_t
- #define _ANKI_SIZEOF_int8_t 1u
- #define I8Vec2 i8vec2
- #define _ANKI_SIZEOF_i8vec2 2u
- #define I8Vec3 i8vec3
- #define _ANKI_SIZEOF_i8vec3 3u
- #define I8Vec4 i8vec4
- #define _ANKI_SIZEOF_i8vec4 4u
- #define U16 uint16_t
- #define _ANKI_SIZEOF_uint16_t 2u
- #define U16Vec2 u16vec2
- #define _ANKI_SIZEOF_u16vec2 4u
- #define U16Vec3 u16vec3
- #define _ANKI_SIZEOF_u16vec3 6u
- #define U16Vec4 u16vec4
- #define _ANKI_SIZEOF_u16vec4 8u
- #define I16 int16_t
- #define _ANKI_SIZEOF_int16_t 2u
- #define I16Vec2 i16vec2
- #define _ANKI_SIZEOF_i16vec2 4u
- #define I16Vec3 i16vec3
- #define _ANKI_SIZEOF_i16vec3 6u
- #define i16Vec4 i16vec4
- #define _ANKI_SIZEOF_i16vec4 8u
- #define U32 uint
- #define _ANKI_SIZEOF_uint 4u
- #define UVec2 uvec2
- #define _ANKI_SIZEOF_uvec2 8u
- #define UVec3 uvec3
- #define _ANKI_SIZEOF_uvec3 12u
- #define UVec4 uvec4
- #define _ANKI_SIZEOF_uvec4 16u
- #define I32 int
- #define _ANKI_SIZEOF_int 4u
- #define IVec2 ivec2
- #define _ANKI_SIZEOF_ivec2 8u
- #define IVec3 ivec3
- #define _ANKI_SIZEOF_ivec3 12u
- #define IVec4 ivec4
- #define _ANKI_SIZEOF_ivec4 16u
- #if _ANKI_SUPPORTS_64BIT
- # define U64 uint64_t
- # define _ANKI_SIZEOF_uint64_t 8u
- # define U64Vec2 u64vec2
- # define _ANKI_SIZEOF_u64vec2 16u
- # define U64Vec3 u64vec3
- # define _ANKI_SIZEOF_u64vec3 24u
- # define U64Vec4 u64vec4
- # define _ANKI_SIZEOF_u64vec4 32u
- # define I64 int64_t
- # define _ANKI_SIZEOF_int64_t 8u
- # define I64Vec2 i64vec2
- # define _ANKI_SIZEOF_i64vec2 16u
- # define I64Vec3 i64vec3
- # define _ANKI_SIZEOF_i64vec3 24u
- # define I64Vec4 i64vec4
- # define _ANKI_SIZEOF_i64vec4 32u
- #endif
- #define Mat3 mat3
- #define Mat4 mat4
- #define _ANKI_SIZEOF_mat4 64u
- #define Mat3x4 mat3x4
- #define _ANKI_SIZEOF_mat3x4 48u
- #define Bool bool
- #if _ANKI_SUPPORTS_64BIT
- # define Address U64
- #else
- # define Address UVec2
- #endif
- #define _ANKI_SIZEOF_Address 8u
- #define _ANKI_CONCATENATE(a, b) a##b
- #define ANKI_CONCATENATE(a, b) _ANKI_CONCATENATE(a, b)
- #define ANKI_SIZEOF(type) _ANKI_CONCATENATE(_ANKI_SIZEOF_, type)
- #define ANKI_ALIGNOF(type) _ANKI_CONCATENATE(_ANKI_ALIGNOF_, type)
- #define _ANKI_SCONST_X(type, n, id) \
- layout(constant_id = id) const type n = type(1); \
- const U32 ANKI_CONCATENATE(n, _CONST_ID) = id
- #define _ANKI_SCONST_X2(type, componentType, n, id, constWorkaround) \
- layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_2_, n) = componentType(1); \
- layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_2_, n) = componentType(1); \
- constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_2_, n), ANKI_CONCATENATE(_anki_const_1_2_, n))
- #define _ANKI_SCONST_X3(type, componentType, n, id, constWorkaround) \
- layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_3_, n) = componentType(1); \
- layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_3_, n) = componentType(1); \
- layout(constant_id = id + 2u) const componentType ANKI_CONCATENATE(_anki_const_2_3_, n) = componentType(1); \
- constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_3_, n), ANKI_CONCATENATE(_anki_const_1_3_, n), \
- ANKI_CONCATENATE(_anki_const_2_3_, n))
- #define _ANKI_SCONST_X4(type, componentType, n, id, constWorkaround) \
- layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_4_, n) = componentType(1); \
- layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_4_, n) = componentType(1); \
- layout(constant_id = id + 2u) const componentType ANKI_CONCATENATE(_anki_const_2_4_, n) = componentType(1); \
- layout(constant_id = id + 3u) const componentType ANKI_CONCATENATE(_anki_const_3_4_, n) = componentType(1); \
- constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_4_, n), ANKI_CONCATENATE(_anki_const_1_4_, n), \
- ANKI_CONCATENATE(_anki_const_2_4_, n), ANKI_CONCATENATE(_anki_const_2_4_, n))
- #define ANKI_SPECIALIZATION_CONSTANT_I32(n, id) _ANKI_SCONST_X(I32, n, id)
- #define ANKI_SPECIALIZATION_CONSTANT_IVEC2(n, id) _ANKI_SCONST_X2(IVec2, I32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_IVEC3(n, id) _ANKI_SCONST_X3(IVec3, I32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_IVEC4(n, id) _ANKI_SCONST_X4(IVec4, I32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_U32(n, id) _ANKI_SCONST_X(U32, n, id)
- #define ANKI_SPECIALIZATION_CONSTANT_UVEC2(n, id) _ANKI_SCONST_X2(UVec2, U32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_UVEC3(n, id) _ANKI_SCONST_X3(UVec3, U32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_UVEC4(n, id) _ANKI_SCONST_X4(UVec4, U32, n, id, const)
- #define ANKI_SPECIALIZATION_CONSTANT_F32(n, id) _ANKI_SCONST_X(F32, n, id)
- #define ANKI_SPECIALIZATION_CONSTANT_VEC2(n, id) _ANKI_SCONST_X2(Vec2, F32, n, id,)
- #define ANKI_SPECIALIZATION_CONSTANT_VEC3(n, id) _ANKI_SCONST_X3(Vec3, F32, n, id,)
- #define ANKI_SPECIALIZATION_CONSTANT_VEC4(n, id) _ANKI_SCONST_X4(Vec4, F32, n, id,)
- #define ANKI_REF(type, alignment) \
- layout(buffer_reference, scalar, buffer_reference_align = (alignment)) buffer type##Ref \
- { \
- type m_value; \
- }
- #define ANKI_PADDING(bytes) U8 _padding_ ## __LINE__[bytes]
- layout(std140, row_major) uniform;
- layout(std140, row_major) buffer;
- )";
- static const U64 SHADER_HEADER_HASH = computeHash(SHADER_HEADER, sizeof(SHADER_HEADER));
- ShaderProgramParser::ShaderProgramParser(CString fname, ShaderProgramFilesystemInterface* fsystem,
- GenericMemoryPoolAllocator<U8> alloc,
- const ShaderCompilerOptions& compilerOptions)
- : m_alloc(alloc)
- , m_fname(alloc, fname)
- , m_fsystem(fsystem)
- , m_compilerOptions(compilerOptions)
- {
- }
- ShaderProgramParser::~ShaderProgramParser()
- {
- }
- void ShaderProgramParser::tokenizeLine(CString line, DynamicArrayAuto<StringAuto>& tokens) const
- {
- ANKI_ASSERT(line.getLength() > 0);
- StringAuto l(m_alloc, line);
- // Replace all tabs with spaces
- for(char& c : l)
- {
- if(c == '\t')
- {
- c = ' ';
- }
- }
- // Split
- StringListAuto spaceTokens(m_alloc);
- spaceTokens.splitString(l, ' ', false);
- // Create the array
- for(const String& s : spaceTokens)
- {
- tokens.emplaceBack(m_alloc, s);
- }
- }
- Error ShaderProgramParser::parsePragmaStart(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
- {
- ANKI_ASSERT(begin && end);
- if(begin >= end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- ShaderType shaderType = ShaderType::COUNT;
- if(*begin == "vert")
- {
- shaderType = ShaderType::VERTEX;
- }
- else if(*begin == "tessc")
- {
- shaderType = ShaderType::TESSELLATION_CONTROL;
- }
- else if(*begin == "tesse")
- {
- }
- else if(*begin == "geom")
- {
- shaderType = ShaderType::GEOMETRY;
- }
- else if(*begin == "frag")
- {
- shaderType = ShaderType::FRAGMENT;
- }
- else if(*begin == "comp")
- {
- shaderType = ShaderType::COMPUTE;
- }
- else if(*begin == "rgen")
- {
- shaderType = ShaderType::RAY_GEN;
- }
- else if(*begin == "ahit")
- {
- shaderType = ShaderType::ANY_HIT;
- }
- else if(*begin == "chit")
- {
- shaderType = ShaderType::CLOSEST_HIT;
- }
- else if(*begin == "miss")
- {
- shaderType = ShaderType::MISS;
- }
- else if(*begin == "int")
- {
- shaderType = ShaderType::INTERSECTION;
- }
- else if(*begin == "call")
- {
- shaderType = ShaderType::CALLABLE;
- }
- else
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- m_codeLines.pushBackSprintf("#ifdef ANKI_%s_SHADER", SHADER_STAGE_NAMES[shaderType].cstr());
- ++begin;
- if(begin != end)
- {
- // Should be the last token
- ANKI_PP_ERROR_MALFORMED();
- }
- // Set the mask
- const ShaderTypeBit mask = ShaderTypeBit(1 << shaderType);
- if(!!(mask & m_shaderTypes))
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start <shader> appearing more than once");
- }
- m_shaderTypes |= mask;
- // Check bounds
- if(m_insideShader)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start before you close the previous pragma start");
- }
- m_insideShader = true;
- return Error::NONE;
- }
- Error ShaderProgramParser::parsePragmaEnd(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
- {
- ANKI_ASSERT(begin && end);
- // Check tokens
- if(begin != end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- // Check bounds
- if(!m_insideShader)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma end before you open with a pragma start");
- }
- m_insideShader = false;
- // Write code
- m_codeLines.pushBack("#endif // Shader guard");
- return Error::NONE;
- }
- Error ShaderProgramParser::parsePragmaMutator(const StringAuto* begin, const StringAuto* end, CString line,
- CString fname)
- {
- ANKI_ASSERT(begin && end);
- if(begin >= end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- m_mutators.emplaceBack(m_alloc);
- Mutator& mutator = m_mutators.getBack();
- // Name
- {
- if(begin >= end)
- {
- // Need to have a name
- ANKI_PP_ERROR_MALFORMED();
- }
- // Check for duplicate mutators
- for(U32 i = 0; i < m_mutators.getSize() - 1; ++i)
- {
- if(m_mutators[i].m_name == *begin)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Duplicate mutator");
- }
- }
- if(begin->getLength() > MAX_SHADER_BINARY_NAME_LENGTH)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Too big name");
- }
- mutator.m_name.create(begin->toCString());
- ++begin;
- }
- // Values
- {
- // Gather them
- for(; begin < end; ++begin)
- {
- MutatorValue value = 0;
- if(tokenIsComment(begin->toCString()))
- {
- break;
- }
- if(begin->toNumber(value))
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- mutator.m_values.emplaceBack(value);
- }
- // Check for correct count
- if(mutator.m_values.getSize() < 2)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Mutator with less that 2 values doesn't make sense");
- }
- std::sort(mutator.m_values.getBegin(), mutator.m_values.getEnd());
- // Check for duplicates
- for(U32 i = 1; i < mutator.m_values.getSize(); ++i)
- {
- if(mutator.m_values[i - 1] == mutator.m_values[i])
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Same value appeared more than once");
- }
- }
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parsePragmaLibraryName(const StringAuto* begin, const StringAuto* end, CString line,
- CString fname)
- {
- ANKI_ASSERT(begin && end);
- if(begin >= end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- if(m_libName.getLength() > 0)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Library name already set");
- }
- m_libName = *begin;
- return Error::NONE;
- }
- Error ShaderProgramParser::parsePragmaRayType(const StringAuto* begin, const StringAuto* end, CString line,
- CString fname)
- {
- ANKI_ASSERT(begin && end);
- if(begin >= end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- if(m_rayType != MAX_U32)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Ray type already set");
- }
- ANKI_CHECK(begin->toNumber(m_rayType));
- if(m_rayType > 128)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Ray type has a very large value");
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parsePragmaRewriteMutation(const StringAuto* begin, const StringAuto* end, CString line,
- CString fname)
- {
- ANKI_ASSERT(begin && end);
- // Some basic sanity checks
- const U tokenCount = end - begin;
- constexpr U minTokenCount = 2 + 1 + 2; // Mutator + value + "to" + mutator + value
- if(tokenCount < minTokenCount)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- MutationRewrite& rewrite = *m_mutationRewrites.emplaceBack(m_alloc);
- Bool servingFrom = true;
- do
- {
- if(*begin == "to")
- {
- if(servingFrom == false)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- servingFrom = false;
- }
- else
- {
- // Mutator & value
- // Get mutator and value
- const CString mutatorName = *begin;
- ++begin;
- if(begin == end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- const CString valueStr = *begin;
- MutatorValue value;
- if(valueStr.toNumber(value))
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Malformed value");
- }
- // Get or create new record
- if(servingFrom)
- {
- MutationRewrite::Record& rec = *rewrite.m_records.emplaceBack();
- for(U32 i = 0; i < m_mutators.getSize(); ++i)
- {
- if(m_mutators[i].getName() == mutatorName)
- {
- rec.m_mutatorIndex = i;
- break;
- }
- }
- if(rec.m_mutatorIndex == MAX_U32)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
- }
- if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
- }
- rec.m_valueFrom = value;
- }
- else
- {
- Bool found = false;
- for(MutationRewrite::Record& rec : rewrite.m_records)
- {
- if(m_mutators[rec.m_mutatorIndex].m_name == mutatorName)
- {
- if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
- }
- rec.m_valueTo = value;
- found = true;
- break;
- }
- }
- if(!found)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- }
- }
- ++begin;
- } while(begin < end && !tokenIsComment(*begin));
- // Sort for some later cross checking
- std::sort(rewrite.m_records.getBegin(), rewrite.m_records.getEnd(),
- [](const MutationRewrite::Record& a, const MutationRewrite::Record& b) {
- return a.m_mutatorIndex < b.m_mutatorIndex;
- });
- // More cross checking
- for(U32 i = 1; i < rewrite.m_records.getSize(); ++i)
- {
- if(rewrite.m_records[i - 1].m_mutatorIndex == rewrite.m_records[i].m_mutatorIndex)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Mutator appeared more than once");
- }
- }
- for(U32 i = 0; i < m_mutationRewrites.getSize() - 1; ++i)
- {
- const MutationRewrite& other = m_mutationRewrites[i];
- if(other.m_records.getSize() != rewrite.m_records.getSize())
- {
- continue;
- }
- Bool same = true;
- for(U32 j = 0; j < rewrite.m_records.getSize(); ++j)
- {
- if(rewrite.m_records[j] != other.m_records[j])
- {
- same = false;
- break;
- }
- }
- if(same)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Mutation already exists");
- }
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parseInclude(const StringAuto* begin, const StringAuto* end, CString line, CString fname,
- U32 depth)
- {
- // Gather the path
- StringAuto path(m_alloc);
- for(; begin < end; ++begin)
- {
- path.append(*begin);
- }
- if(path.isEmpty())
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- // Check
- const char firstChar = path[0];
- const char lastChar = path[path.getLength() - 1];
- if((firstChar == '\"' && lastChar == '\"') || (firstChar == '<' && lastChar == '>'))
- {
- StringAuto fname2(m_alloc);
- fname2.create(path.begin() + 1, path.begin() + path.getLength() - 1);
- const Bool dontIgnore =
- fname2.find("AnKi/Shaders/") != String::NPOS || fname2.find("ThirdParty/") != String::NPOS;
- if(!dontIgnore)
- {
- // The shaders can't include C++ files. Ignore the include
- return Error::NONE;
- }
- if(parseFile(fname2, depth + 1))
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Error parsing include. See previous errors");
- }
- }
- else
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth)
- {
- // Tokenize
- DynamicArrayAuto<StringAuto> tokens(m_alloc);
- tokenizeLine(line, tokens);
- ANKI_ASSERT(tokens.getSize() > 0);
- const StringAuto* token = tokens.getBegin();
- const StringAuto* end = tokens.getEnd();
- // Skip the hash
- Bool foundAloneHash = false;
- if(*token == "#")
- {
- ++token;
- foundAloneHash = true;
- }
- if((token < end) && ((foundAloneHash && *token == "include") || *token == "#include"))
- {
- // We _must_ have an #include
- ANKI_CHECK(parseInclude(token + 1, end, line, fname, depth));
- }
- else if((token < end) && ((foundAloneHash && *token == "pragma") || *token == "#pragma"))
- {
- // We may have a #pragma once or a #pragma anki or something else
- ++token;
- if(*token == "once")
- {
- // Pragma once
- if(foundPragmaOnce)
- {
- ANKI_PP_ERROR_MALFORMED_MSG("Can't have more than one #pragma once per file");
- }
- if(token + 1 != end)
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- // Add the guard unique for this file
- foundPragmaOnce = true;
- const U64 hash = fname.computeHash();
- m_codeLines.pushBackSprintf("#ifndef _ANKI_INCL_GUARD_%llu\n"
- "#define _ANKI_INCL_GUARD_%llu",
- hash, hash);
- }
- else if(*token == "anki")
- {
- // Must be a #pragma anki
- ++token;
- if(*token == "mutator")
- {
- ANKI_CHECK(parsePragmaMutator(token + 1, end, line, fname));
- }
- else if(*token == "start")
- {
- ANKI_CHECK(parsePragmaStart(token + 1, end, line, fname));
- }
- else if(*token == "end")
- {
- ANKI_CHECK(parsePragmaEnd(token + 1, end, line, fname));
- }
- else if(*token == "rewrite_mutation")
- {
- ANKI_CHECK(parsePragmaRewriteMutation(token + 1, end, line, fname));
- }
- else if(*token == "library")
- {
- ANKI_CHECK(parsePragmaLibraryName(token + 1, end, line, fname));
- }
- else if(*token == "ray_type")
- {
- ANKI_CHECK(parsePragmaRayType(token + 1, end, line, fname));
- }
- else
- {
- ANKI_PP_ERROR_MALFORMED();
- }
- // Add the line as a comment because of hashing of the source
- m_codeLines.pushBackSprintf("//%s", line.cstr());
- }
- else
- {
- // Some other pragma
- ANKI_SHADER_COMPILER_LOGW("Ignoring: %s", line.cstr());
- m_codeLines.pushBack(line);
- }
- }
- else
- {
- // Ignore
- m_codeLines.pushBack(line);
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parseFile(CString fname, U32 depth)
- {
- // First check the depth
- if(depth > MAX_INCLUDE_DEPTH)
- {
- ANKI_SHADER_COMPILER_LOGE("The include depth is too high. Probably circular includance");
- }
- Bool foundPragmaOnce = false;
- // Load file in lines
- StringAuto txt(m_alloc);
- ANKI_CHECK(m_fsystem->readAllText(fname, txt));
- StringListAuto lines(m_alloc);
- lines.splitString(txt.toCString(), '\n');
- if(lines.getSize() < 1)
- {
- ANKI_SHADER_COMPILER_LOGE("Source is empty");
- }
- // Parse lines
- for(const String& line : lines)
- {
- if(line.find("pragma") != CString::NPOS || line.find("include") != CString::NPOS)
- {
- // Possibly a preprocessor directive we care
- ANKI_CHECK(parseLine(line.toCString(), fname, foundPragmaOnce, depth));
- }
- else
- {
- // Just append the line
- m_codeLines.pushBack(line.toCString());
- }
- }
- if(foundPragmaOnce)
- {
- // Append the guard
- m_codeLines.pushBack("#endif // Include guard");
- }
- return Error::NONE;
- }
- Error ShaderProgramParser::parse()
- {
- ANKI_ASSERT(!m_fname.isEmpty());
- ANKI_ASSERT(m_codeLines.isEmpty());
- const CString fname = m_fname;
- // Parse recursively
- ANKI_CHECK(parseFile(fname, 0));
- // Checks
- {
- if(!!(m_shaderTypes & ShaderTypeBit::COMPUTE))
- {
- if(m_shaderTypes != ShaderTypeBit::COMPUTE)
- {
- ANKI_SHADER_COMPILER_LOGE("Can't combine compute shader with other types of shaders");
- return Error::USER_DATA;
- }
- }
- else if(!!(m_shaderTypes & ShaderTypeBit::ALL_GRAPHICS))
- {
- if(!(m_shaderTypes & ShaderTypeBit::VERTEX))
- {
- ANKI_SHADER_COMPILER_LOGE("Missing vertex shader");
- return Error::USER_DATA;
- }
- if(!(m_shaderTypes & ShaderTypeBit::FRAGMENT))
- {
- ANKI_SHADER_COMPILER_LOGE("Missing fragment shader");
- return Error::USER_DATA;
- }
- }
- if(m_insideShader)
- {
- ANKI_SHADER_COMPILER_LOGE("Forgot a \"pragma anki end\"");
- return Error::USER_DATA;
- }
- }
- // Create the code lines
- if(m_codeLines.getSize())
- {
- m_codeLines.join("\n", m_codeSource);
- m_codeLines.destroy();
- }
- // Create the hash
- {
- if(m_codeSource.getLength())
- {
- m_codeSourceHash = appendHash(m_codeSource.getBegin(), m_codeSource.getLength(), SHADER_HEADER_HASH);
- }
- if(m_libName.getLength() > 0)
- {
- m_codeSourceHash = appendHash(m_libName.getBegin(), m_libName.getLength(), m_codeSourceHash);
- }
- m_codeSourceHash = appendHash(&m_rayType, sizeof(m_rayType), m_codeSourceHash);
- }
- return Error::NONE;
- }
- void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
- StringAuto& header)
- {
- header.sprintf(SHADER_HEADER, SHADER_STAGE_NAMES[shaderType].cstr(), ANKI_OS_ANDROID, ANKI_OS_WINDOWS,
- ANKI_OS_LINUX, compilerOptions.m_bindlessLimits.m_bindlessTextureCount,
- compilerOptions.m_bindlessLimits.m_bindlessImageCount);
- }
- Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation,
- ShaderProgramParserVariant& variant) const
- {
- // Sanity checks
- ANKI_ASSERT(m_codeSource.getLength() > 0);
- ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
- for(U32 i = 0; i < mutation.getSize(); ++i)
- {
- ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]) && "Value not found");
- }
- // Init variant
- ::new(&variant) ShaderProgramParserVariant();
- variant.m_alloc = m_alloc;
- // Create the mutator defines
- StringAuto mutatorDefines(m_alloc);
- for(U32 i = 0; i < mutation.getSize(); ++i)
- {
- mutatorDefines.append(StringAuto(m_alloc).sprintf("#define %s %d\n", m_mutators[i].m_name.cstr(), mutation[i]));
- }
- // Generate souce per stage
- for(ShaderType shaderType : EnumIterable<ShaderType>())
- {
- if(!(ShaderTypeBit(1u << shaderType) & m_shaderTypes))
- {
- continue;
- }
- // Create the header
- StringAuto header(m_alloc);
- generateAnkiShaderHeader(shaderType, m_compilerOptions, header);
- // Create the final source without the bindings
- StringAuto finalSource(m_alloc);
- finalSource.append(header);
- finalSource.append(mutatorDefines);
- finalSource.append(m_codeSource);
- // Move the source
- variant.m_sources[shaderType] = std::move(finalSource);
- }
- return Error::NONE;
- }
- Bool ShaderProgramParser::rewriteMutation(WeakArray<MutatorValue> mutation) const
- {
- // Checks
- ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
- for(U32 i = 0; i < mutation.getSize(); ++i)
- {
- ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]));
- }
- // Early exit
- if(mutation.getSize() == 0)
- {
- return false;
- }
- // Find if mutation exists
- for(const MutationRewrite& rewrite : m_mutationRewrites)
- {
- Bool found = true;
- for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
- {
- if(rewrite.m_records[i].m_valueFrom != mutation[rewrite.m_records[i].m_mutatorIndex])
- {
- found = false;
- break;
- }
- }
- if(found)
- {
- // Rewrite it
- for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
- {
- mutation[rewrite.m_records[i].m_mutatorIndex] = rewrite.m_records[i].m_valueTo;
- }
- return true;
- }
- }
- return false;
- }
- Bool ShaderProgramParser::mutatorHasValue(const ShaderProgramParserMutator& mutator, MutatorValue value)
- {
- for(MutatorValue v : mutator.m_values)
- {
- if(value == v)
- {
- return true;
- }
- }
- return false;
- }
- } // end namespace anki
|