Skip to content

Commit

Permalink
Test Shader Dx10
Browse files Browse the repository at this point in the history
  • Loading branch information
soufianekhiat committed Jun 23, 2024
1 parent fedb980 commit 8ad14ee
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
184 changes: 184 additions & 0 deletions ImPlatform/ImPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <ImPlatform.h>
#endif

#include <string>

#include <imgui_internal.h>

#if (IM_CURRENT_PLATFORM == IM_PLATFORM_WIN32)
Expand Down Expand Up @@ -992,6 +994,188 @@ namespace ImPlatform
( *reinterpret_cast< ID3D12Resource** >( &id ) )->Release();
( *reinterpret_cast< ID3D12Resource** >( &id ) ) = NULL;

#endif
}

void ReplaceAll( std::string& sStr, std::string const& sToFind, std::string const& sReplaceBy )
{
size_t uStart = 0;
while ( ( uStart = sStr.find( sToFind, uStart ) ) != std::string::npos )
{
sStr.replace( uStart, sToFind.length(), sReplaceBy );
uStart += sReplaceBy.length();
}
}


ImShader ImCreateShader( char* source )
{
static const char* pShaderBase =
"#ifndef __IM_SHADER_H__\
#define __IM_SHADER_H__\
\
#if defined(IM_SHADER_HLSL)\
#define IMS_IN in\
#define IMS_OUT out\
#define IMS_INOUT inout\
#define IMS_UNIFORM uniform\
#elif defined(IMS_GLSL)\
#define IMS_IN in\
#define IMS_OUT out\
#define IMS_INOUT inout\
#define IMS_UNIFORM const\
#endif\
\
#if defined(IM_SHADER_HLSL)\
\
#define Mat44f matrix<float, 4, 4>\
#define Mat33f matrix<float, 3, 3>\
\
typedef bool Bool;\
#define f32 float\
typedef uint u32;\
typedef int i32;\
\
#endif\
\
#if defined(IMS_GLSL)\
\
#define Mat44f mat4\
#define Mat33f mat3\
\
#define Bool bool\
#define f32 float\
#define float2 vec2\
#define float3 vec3\
#define float4 vec4\
#define u32 uint\
#define uint2 uvec2\
#define uint3 uvec3\
#define uint4 uvec4\
#define i32 int\
#define int2 ivec2\
#define int3 ivec3\
#define int4 ivec4\
\
#define float4x4 mat4\
#define float3x3 mat3\
#define float2x2 mat2\
\
#endif";

static const char* pVertexShaderDefault =
"cbuffer vertexBuffer : register(b0) \
{\
float4x4 ProjectionMatrix; \
};\
struct VS_INPUT\
{\
float2 pos : POSITION;\
float4 col : COLOR0;\
float2 uv : TEXCOORD0;\
};\
\
struct PS_INPUT\
{\
float4 pos : SV_POSITION;\
float4 col : COLOR0;\
float2 uv : TEXCOORD0;\
};\
\
PS_INPUT main(VS_INPUT input)\
{\
PS_INPUT output;\
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
output.col = input.col;\
output.uv = input.uv;\
return output;\
}";

static const char* pPixelShaderTemplate =
"struct PS_INPUT\
{\
float4 pos : SV_POSITION;\
float4 col : COLOR0;\
float2 uv : TEXCOORD0;\
};\
sampler sampler0;\
Texture2D texture0;\
\
float4 main(PS_INPUT input) : SV_Target\
{\
float4 out_col;\
%IM_PLATFORM_WRITE_OUT_COL%\
out_col *= input.col * texture0.Sample(sampler0, input.uv); \
return out_col; \
}";

std::string sVS = pShaderBase;
sVS += pVertexShaderDefault;
std::string sPS = pShaderBase;

std::string sFunc = pPixelShaderTemplate;
ReplaceAll( sFunc, "%IM_PLATFORM_WRITE_OUT_COL%", source );
sPS += sFunc;

#if (IM_CURRENT_GFX == IM_GFX_OPENGL2) || (IM_CURRENT_GFX == IM_GFX_OPENGL3)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX9)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX10)

ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();

ID3D10VertexShader* pVertexShader = NULL;
ID3D10PixelShader* pPixelShader = NULL;

ID3DBlob* vertexShaderBlob;
if ( FAILED( D3DCompile( sVS.c_str(), sVS.size(), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vertexShaderBlob, nullptr ) ) )
return { NULL, NULL };; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
if ( bd->pd3dDevice->CreateVertexShader( vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &pVertexShader ) != S_OK )
{
vertexShaderBlob->Release();
return { NULL, NULL };
}

ID3DBlob* pixelShaderBlob;
if ( FAILED( D3DCompile( sPS.c_str(), sPS.size(), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &pixelShaderBlob, nullptr ) ) )
return { NULL, NULL }; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
if ( bd->pd3dDevice->CreatePixelShader( pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), &pPixelShader ) != S_OK )
{
pixelShaderBlob->Release();
return { NULL, NULL };
}
pixelShaderBlob->Release();

return { pVertexShader, pPixelShader };

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX11)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX12)

#endif

return { NULL, NULL };
}

void ImReleaseShader( ImShader& shader )
{
#if (IM_CURRENT_GFX == IM_GFX_OPENGL2) || (IM_CURRENT_GFX == IM_GFX_OPENGL3)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX9)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX10)

ID3D10VertexShader* pVertexShader = ( ID3D10VertexShader* )shader.vs;
ID3D10PixelShader* pPixelShader = ( ID3D10PixelShader* )shader.ps;

pVertexShader->Release();
pPixelShader->Release();

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX11)

#elif (IM_CURRENT_GFX == IM_GFX_DIRECTX12)

#endif
}

Expand Down
11 changes: 11 additions & 0 deletions ImPlatform/ImPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

// For Dx12: #define ImTextureID ImU64

#define IM_PLATFORM_ENABLE_CUSTOM_SHADER

#include <imgui.h>

// To override the link of glfw3 do not use, other it ImPlatform will use the lib included on imgui
Expand Down Expand Up @@ -315,6 +317,15 @@ namespace ImPlatform
ImTextureID ImCreateTexture2D ( char* pData, ImU32 const uWidth, ImU32 const uHeight, ImImageDesc const& oImgDesc );
void ImReleaseTexture2D ( ImTextureID id );

typedef void* ImShaderID;
struct ImShader
{
ImShaderID vs;
ImShaderID ps;
};
ImShader ImCreateShader( char* source );
void ImReleaseShader( ImShader& shader );

bool ImIsMaximized();

// Custom Title Bar
Expand Down
2 changes: 1 addition & 1 deletion ImPlatformDemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define __DEAR_WIN__
//#define IM_GLFW3_AUTO_LINK
//#define __DEAR_GLFW__
#define __DEAR_GFX_DX9__
#define __DEAR_GFX_DX10__
//#define __DEAR_GFX_OGL3__
//#define IM_CURRENT_TARGET IM_TARGET_GLFW_OPENGL3
//#define IM_THE_CHERNO_GLFW3
Expand Down

0 comments on commit 8ad14ee

Please sign in to comment.