git.s-ol.nu ~forks/DiligentTools / 9efe0f1
ImGuiDiligentRenderer: added MSL shaders assiduous 7 months ago
1 changed file(s) with 100 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
205205 };
206206 // clang-format on
207207
208
209 static const char* ShadersMSL = R"(
210 #include <metal_stdlib>
211 #include <simd/simd.h>
212
213 using namespace metal;
214
215 struct VSConstants
216 {
217 float4x4 ProjectionMatrix;
218 };
219
220 struct VSIn
221 {
222 float2 pos [[attribute(0)]];
223 float2 uv [[attribute(1)]];
224 float4 col [[attribute(2)]];
225 };
226
227 struct VSOut
228 {
229 float4 col [[user(locn0)]];
230 float2 uv [[user(locn1)]];
231 float4 pos [[position]];
232 };
233
234 vertex VSOut vs_main(VSIn in [[stage_in]], constant VSConstants& Constants [[buffer(0)]])
235 {
236 VSOut out = {};
237 out.pos = Constants.ProjectionMatrix * float4(in.pos, 0.0, 1.0);
238 out.col = in.col;
239 out.uv = in.uv;
240 return out;
241 }
242
243 struct PSOut
244 {
245 float4 col [[color(0)]];
246 };
247
248 fragment PSOut ps_main(VSOut in [[stage_in]],
249 texture2d<float> Texture [[texture(0)]],
250 sampler Texture_sampler [[sampler(0)]])
251 {
252 PSOut out = {};
253 out.col = in.col * Texture.sample(Texture_sampler, in.uv);
254 return out;
255 }
256 )";
257
208258 ImGuiDiligentRenderer::ImGuiDiligentRenderer(IRenderDevice* pDevice,
209259 TEXTURE_FORMAT BackBufferFmt,
210260 TEXTURE_FORMAT DepthBufferFmt,
264314 ShaderCI.UseCombinedTextureSamplers = true;
265315 ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_DEFAULT;
266316
317 const auto& deviceCaps = m_pDevice->GetDeviceCaps();
318
267319 RefCntAutoPtr<IShader> pVS;
268320 {
269321 ShaderCI.Desc.ShaderType = SHADER_TYPE_VERTEX;
270322 ShaderCI.Desc.Name = "Imgui VS";
271 if (m_pDevice->GetDeviceCaps().IsVulkanDevice())
272 {
273 ShaderCI.ByteCode = VertexShader_SPIRV;
274 ShaderCI.ByteCodeSize = sizeof(VertexShader_SPIRV);
275 }
276 else
277 {
278 ShaderCI.Source = m_pDevice->GetDeviceCaps().IsD3DDevice() ? VertexShaderHLSL : VertexShaderGLSL;
323 switch (deviceCaps.DevType)
324 {
325 case RENDER_DEVICE_TYPE_VULKAN:
326 ShaderCI.ByteCode = VertexShader_SPIRV;
327 ShaderCI.ByteCodeSize = sizeof(VertexShader_SPIRV);
328 break;
329
330 case RENDER_DEVICE_TYPE_D3D11:
331 case RENDER_DEVICE_TYPE_D3D12:
332 ShaderCI.Source = VertexShaderHLSL;
333 break;
334
335 case RENDER_DEVICE_TYPE_GL:
336 case RENDER_DEVICE_TYPE_GLES:
337 ShaderCI.Source = VertexShaderGLSL;
338 break;
339
340 case RENDER_DEVICE_TYPE_METAL:
341 ShaderCI.Source = ShadersMSL;
342 ShaderCI.EntryPoint = "vs_main";
343 break;
344
345 default:
346 UNEXPECTED("Unknown render device type");
279347 }
280348 m_pDevice->CreateShader(ShaderCI, &pVS);
281349 }
284352 {
285353 ShaderCI.Desc.ShaderType = SHADER_TYPE_PIXEL;
286354 ShaderCI.Desc.Name = "Imgui PS";
287 if (m_pDevice->GetDeviceCaps().IsVulkanDevice())
288 {
289 ShaderCI.ByteCode = FragmentShader_SPIRV;
290 ShaderCI.ByteCodeSize = sizeof(FragmentShader_SPIRV);
291 }
292 else
293 {
294 ShaderCI.Source = m_pDevice->GetDeviceCaps().IsD3DDevice() ? PixelShaderHLSL : PixelShaderGLSL;
355 switch (deviceCaps.DevType)
356 {
357 case RENDER_DEVICE_TYPE_VULKAN:
358 ShaderCI.ByteCode = FragmentShader_SPIRV;
359 ShaderCI.ByteCodeSize = sizeof(FragmentShader_SPIRV);
360 break;
361
362 case RENDER_DEVICE_TYPE_D3D11:
363 case RENDER_DEVICE_TYPE_D3D12:
364 ShaderCI.Source = PixelShaderHLSL;
365 break;
366
367 case RENDER_DEVICE_TYPE_GL:
368 case RENDER_DEVICE_TYPE_GLES:
369 ShaderCI.Source = PixelShaderGLSL;
370 break;
371
372 case RENDER_DEVICE_TYPE_METAL:
373 ShaderCI.Source = ShadersMSL;
374 ShaderCI.EntryPoint = "ps_main";
375 break;
376
377 default:
378 UNEXPECTED("Unknown render device type");
295379 }
296380 m_pDevice->CreateShader(ShaderCI, &pPS);
297381 }