How to split the structure between C ++ / DirectX and HLSL?

I learn C ++ and DirectX, and I notice a lot of duplication, trying to keep the structure in my HLSL shaders and C ++ code in sync. I would like to separate the structures, since both languages ​​have the same #includesemantics and structure of the header file. I met success with

// ColorStructs.h
#pragma once

#ifdef __cplusplus

#include <DirectXMath.h>
using namespace DirectX;

using float4 = XMFLOAT4;

namespace ColorShader
{

#endif

    struct VertexInput
    {
        float4 Position;
        float4 Color;
    };

    struct PixelInput
    {
        float4 Position;
        float4 Color;
    };

#ifdef __cplusplus
}
#endif

The problem is that when compiling these shaders, FXC tells me input parameter 'input' missing sematicsabout the main function of my pixel shader:

#include "ColorStructs.h"

void main(PixelInput input)
{
    // Contents elided
}

I know that I need to have semantics like float4 Position : POSITION, but I cannot think of a way to do this so as not to violate the C ++ syntax.

Is there a way to maintain common structures between HLSL and C ++? Or is this impracticable and requires duplication of structures between two source trees?

+4
1

++

#ifdef __cplusplus
#define SEMANTIC(sem) 
#else
#define SEMANTIC(sem) : sem
#endif

struct VertexInput
{
    float4 Position SEMANTIC(POSITION0);
    float4 Color SEMANTIC(COLOR0);
};
+5

Source: https://habr.com/ru/post/1625022/


All Articles