如何在C++/DirectX和HLSL之间共享一个结构?

ber*_*wyn 6 c++ directx hlsl directx-11 visual-c++

我正在学习C++和DirectX,并且我注意到在尝试保持HLSL着色器和C++代码中的结构同步时有很多重复.我想分享结构,因为两种语言都有相同的#include语义和头文件结构.我遇到了成功

// 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
Run Code Online (Sandbox Code Playgroud)

但问题是在编译这些着色器时,FXC告诉input parameter 'input' missing sematics我Pixel着色器的主要功能:

#include "ColorStructs.h"

void main(PixelInput input)
{
    // Contents elided
}
Run Code Online (Sandbox Code Playgroud)

我知道我需要有类似的语义,float4 Position : POSITION但我无法想出一种不违反C++语法的方法.

有没有办法让HLSL和C++之间的结构保持一致?或者它是不可行的,并且需要复制两个源树之间的结构?

Con*_*des 5

您可以在编译C++时使用函数宏来删除语义说明符

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

struct VertexInput
{
    float4 Position SEMANTIC(POSITION0);
    float4 Color SEMANTIC(COLOR0);
};
Run Code Online (Sandbox Code Playgroud)