vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

hlsl.vim (5092B)


      1 vim9script
      2 # Language:    HLSL (High-Level Shader Language)
      3 # Maintainer:  Maxim Kim <habamax@gmail.com>
      4 # Last Change: 2026 Aug 11
      5 # https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-reference
      6 
      7 if exists("b:current_syntax")
      8     finish
      9 endif
     10 
     11 var expanded_types = [
     12     'float', 'double', 'int', 'uint', 'bool',
     13     'min10float', 'min16float', 'min12int', 'min16int', 'min16uint',
     14     'float16_t', 'int16_t', 'uint16_t', 'uint64_t', 'int64_t',
     15     'vector', 'matrix'
     16 ]
     17 
     18 # float1, float2 ...
     19 # ...
     20 # int1x1, int1x2 ...
     21 exe $"syn keyword hlslType {expanded_types->join()}"
     22 range(1, 4)->foreach((_, idxN) => {
     23     var typeN = expanded_types->mapnew((_, type) => type .. idxN)->join()
     24     exe $"syn keyword hlslType {typeN}"
     25     range(1, 4)->foreach((_, idxM) => {
     26         var typeNxM = expanded_types
     27             ->mapnew((_, type) => $"{type}{idxN}x{idxM}")->join()
     28         exe $"syn keyword hlslType {typeNxM}"
     29     })
     30 })
     31 syn keyword hlslType void dword half string texture sampler
     32 syn keyword hlslType extern nointerpolation precise shared groupshared static
     33 syn keyword hlslType uniform export extern volatile const row_major column_major
     34 syn keyword hlslType struct linear centroid noperspective sample
     35 syn keyword hlslType typedef namespace class interface enum
     36 syn keyword hlslType snorm unorm
     37 syn keyword hlslType vertexfragment pixelfragment
     38 syn keyword hlslType point line lineadj triangle triangleadj
     39 syn keyword hlslType technique10 technique11
     40 syn keyword hlslType cbuffer tbuffer
     41 syn keyword hlslType Buffer StructuredBuffer AppendStructuredBuffer
     42 syn keyword hlslType ByteAddressBuffer ConsumeStructuredBuffer
     43 syn keyword hlslType Texture1D Texture2D Texture3D Texture1DArray Texture2DArray
     44 syn keyword hlslType Texture2DMS Texture2DMSArray
     45 syn keyword hlslType TextureCube TextureCubeArray
     46 syn keyword hlslType RWBuffer RWByteAddressBuffer RWStructuredBuffer
     47 syn keyword hlslType RWTexture1D RWTexture1DArray RWTexture2D
     48 syn keyword hlslType RWTexture2DArray RWTexture3D
     49 syn keyword hlslType PointStream LineStream TriangleStream
     50 syn keyword hlslType SamplerState SamplerComparisonState
     51 syn keyword hlslType RasterizerState DepthStencilState BlendState
     52 syn keyword hlslType OutputPatch InputPatch
     53 # TODO: only recognize it in function parameter list
     54 syn keyword hlslType in out inout
     55 
     56 # legacy
     57 syn keyword hlslType sampler2D
     58 
     59 syn keyword hlslCondition if else switch case default
     60 syn keyword hlslRepeat while for do break continue
     61 syn keyword hlslStatement return discard compile compile_fragment packoffset
     62 syn keyword hlslStatement pass register fxgroup
     63 syn keyword hlslConstant true false NULL
     64 
     65 # simplified Semantic highlighting: SV_POSITION, SV_TARGET etc
     66 syn match hlslSemantic /:\s*\zs\k\+/
     67 
     68 # POSITIONT BINORMAL[n] BLENDINDICES[n] BLENDWEIGHT[n] COLOR[n] NORMAL[n]
     69 # POSITION[n] PSIZE[n] TANGENT[n] TEXCOORD[n]
     70 
     71 # SV_ClipDistance[n] SV_CullDistance[n] SV_Coverage SV_Depth
     72 # SV_DepthGreaterEqual SV_DepthLessEqual SV_DispatchThreadID SV_DomainLocation
     73 # SV_GroupID SV_GroupIndex SV_GroupThreadID SV_GSInstanceID SV_InnerCoverage
     74 # SV_InsideTessFactor SV_InstanceID SV_IsFrontFace SV_OutputControlPointID
     75 # SV_Position SV_PrimitiveID SV_RenderTargetArrayIndex SV_SampleIndex
     76 # SV_StencilRef SV_Target[n], where 0 <= n <= 7 SV_TessFactor SV_VertexID
     77 # SV_ViewportArrayIndex SV_ShadingRate
     78 
     79 syntax match hlslInteger "\v-?<[0-9]+%(_[0-9]+)*[uUlL]?>" display
     80 syntax match hlslFloat "\v-?<[0-9]+%(_[0-9]+)*%(\.[0-9]+%(_[0-9]+)*)%([eE][+-]=[0-9]+%(_[0-9]+)*)=[hHfFlL]?>" display
     81 syntax match hlslHex "\v<0[xX][0-9A-Fa-f]+%(_[0-9A-Fa-f]+)*[uUlL]?>" display
     82 syntax match hlslOct "\v<0[oO][0-7]+%(_[0-7]+)*[uUlL]?>" display
     83 syntax cluster hlslNumber contains=hlslInteger,hlslFloat,hlslHex,hlslOct
     84 
     85 syntax region hlslChar start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=hlslEscape
     86 syntax region hlslString start=+"+ skip=+\\\\\|\\'+ end=+"+ contains=hlslEscape
     87 syntax match  hlslEscape display contained /\\\([abefnrtv\\'"]\|\o\{3}\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
     88 
     89 # TODO: better preproc
     90 syn region hlslPreProc start=/^\s*\zs#/ end=/$/ contains=@hlslNumber,hlslString,hlslChar,@hlslComment
     91 
     92 syntax match   hlslTodo "TODO" contained
     93 syntax match   hlslTodo "XXX" contained
     94 syntax match   hlslTodo "FIXME" contained
     95 syntax region  hlslLineComment start=/\/\// end=/$/  contains=@Spell,hlslTodo
     96 syntax region  hlslBlockComment start=/\/\*/ end=/\*\// contains=@Spell,hlslTodo
     97 syntax cluster hlslComment contains=hlslLineComment,hlslBlockComment
     98 
     99 hi def link hlslType         Type
    100 hi def link hlslStatement    Statement
    101 hi def link hlslCondition    Statement
    102 hi def link hlslRepeat       Statement
    103 hi def link hlslInteger      Number
    104 hi def link hlslFloat        Float
    105 hi def link hlslHex          Number
    106 hi def link hlslOct          Number
    107 hi def link hlslLineComment  Comment
    108 hi def link hlslBlockComment Comment
    109 hi def link hlslTodo         Todo
    110 hi def link hlslChar         Character
    111 hi def link hlslString       String
    112 hi def link hlslEscape       Special
    113 hi def link hlslPreProc      PreProc
    114 hi def link hlslSemantic     Identifier
    115 
    116 b:current_syntax = "hlsl"