From 9f0e9a808b34ba41981445655aa9e2361b16481b Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 3 Apr 2016 20:16:03 -1000 Subject: [PATCH] Implement Metal lighting shader --- Runtime/Graphics/CLineRendererShadersGLSL.cpp | 2 +- .../Graphics/CLineRendererShadersMetal.cpp | 2 +- Runtime/Graphics/CModelBoo.cpp | 7 +++- Runtime/Graphics/CModelShadersMetal.cpp | 39 +++++++++++++++++++ Runtime/Graphics/CMoviePlayer.cpp | 4 +- Runtime/GuiSys/CTextRenderBuffer.cpp | 4 +- Runtime/Particle/CElementGenShadersGLSL.cpp | 4 +- Runtime/Particle/CElementGenShadersMetal.cpp | 4 +- hecl | 2 +- specter | 2 +- 10 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Runtime/Graphics/CLineRendererShadersGLSL.cpp b/Runtime/Graphics/CLineRendererShadersGLSL.cpp index 8ff768da6..8f4fd755a 100644 --- a/Runtime/Graphics/CLineRendererShadersGLSL.cpp +++ b/Runtime/Graphics/CLineRendererShadersGLSL.cpp @@ -118,7 +118,7 @@ struct OGLLineDataBindingFactory : CLineRendererShaders::IDataBindingFactory boo::IGraphicsBuffer* uniforms[] = {renderer.m_uniformBuf}; renderer.m_shaderBind = ctx.newShaderDataBinding(pipeline, vtxFmt, renderer.m_vertBuf, - nullptr, nullptr, 1, uniforms, + nullptr, nullptr, 1, uniforms, nullptr, texCount, textures); } }; diff --git a/Runtime/Graphics/CLineRendererShadersMetal.cpp b/Runtime/Graphics/CLineRendererShadersMetal.cpp index 5fd09227c..a056d343e 100644 --- a/Runtime/Graphics/CLineRendererShadersMetal.cpp +++ b/Runtime/Graphics/CLineRendererShadersMetal.cpp @@ -117,7 +117,7 @@ struct MetalLineDataBindingFactory : CLineRendererShaders::IDataBindingFactory boo::IGraphicsBuffer* uniforms[] = {renderer.m_uniformBuf}; renderer.m_shaderBind = ctx.newShaderDataBinding(pipeline, nullptr, renderer.m_vertBuf, - nullptr, nullptr, 1, uniforms, + nullptr, nullptr, 1, uniforms, nullptr, texCount, textures); } }; diff --git a/Runtime/Graphics/CModelBoo.cpp b/Runtime/Graphics/CModelBoo.cpp index ae051b638..0ebec32cd 100644 --- a/Runtime/Graphics/CModelBoo.cpp +++ b/Runtime/Graphics/CModelBoo.cpp @@ -4,6 +4,7 @@ #include "Graphics/CLight.hpp" #include "hecl/HMDLMeta.hpp" #include "hecl/Runtime.hpp" +#include "boo/graphicsdev/Metal.hpp" #include "CModelShaders.hpp" namespace urde @@ -117,6 +118,10 @@ void CBooModel::BuildGfxToken() size_t thisOffs[3]; size_t thisSizes[3]; + static const boo::PipelineStage stages[3] = {boo::PipelineStage::Vertex, + boo::PipelineStage::Vertex, + boo::PipelineStage::Fragment}; + /* Enumerate surfaces and build data bindings */ for (const CBooSurface& surf : *x0_surfaces) { @@ -164,7 +169,7 @@ void CBooModel::BuildGfxToken() for (boo::IShaderPipeline* pipeline : pipelines) extendeds.push_back( ctx.newShaderDataBinding(pipeline, m_vtxFmt, - x8_vbo, nullptr, xc_ibo, 3, bufs, + x8_vbo, nullptr, xc_ibo, 3, bufs, stages, thisOffs, thisSizes, mat.textureIdxs.size(), texs.data())); } return true; diff --git a/Runtime/Graphics/CModelShadersMetal.cpp b/Runtime/Graphics/CModelShadersMetal.cpp index 008e36173..d57834700 100644 --- a/Runtime/Graphics/CModelShadersMetal.cpp +++ b/Runtime/Graphics/CModelShadersMetal.cpp @@ -3,10 +3,49 @@ namespace urde { +static const char* LightingMetal = +"struct Light\n" +"{\n" +" float4 pos;\n" +" float4 dir;\n" +" float4 color;\n" +" float4 linAtt;\n" +" float4 angAtt;\n" +"};\n" +"\n" +"struct LightingUniform\n" +"{\n" +" Light lights[" _XSTR(URDE_MAX_LIGHTS) "];\n" +" float4 ambient;\n" +"};\n" +"\n" +"static float4 LightingFunc(constant LightingUniform& lu, float4 mvPosIn, float4 mvNormIn)\n" +"{\n" +" float4 ret = lu.ambient;\n" +" \n" +" for (int i=0 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n" +" {\n" +" float3 delta = mvPosIn.xyz - lu.lights[i].pos.xyz;\n" +" float dist = length(delta);\n" +" float angDot = saturate(dot(normalize(delta), lu.lights[i].dir.xyz));\n" +" float att = 1.0 / (lu.lights[i].linAtt[2] * dist * dist +\n" +" lu.lights[i].linAtt[1] * dist +\n" +" lu.lights[i].linAtt[0]);\n" +" float angAtt = lu.lights[i].angAtt[2] * angDot * angDot +\n" +" lu.lights[i].angAtt[1] * angDot +\n" +" lu.lights[i].angAtt[0];\n" +" ret += lu.lights[i].color * saturate(angAtt) * att * saturate(dot(normalize(-delta), mvNormIn.xyz));\n" +" }\n" +" \n" +" return saturate(ret);\n" +"}\n"; + hecl::Runtime::ShaderCacheExtensions CModelShaders::GetShaderExtensionsMetal(boo::IGraphicsDataFactory::Platform plat) { + static const char* BlockNames[] = {"LightingUniform"}; hecl::Runtime::ShaderCacheExtensions ext(plat); + ext.registerExtensionSlot({LightingMetal, "LightingFunc"}, {}, 1, BlockNames); return ext; } diff --git a/Runtime/Graphics/CMoviePlayer.cpp b/Runtime/Graphics/CMoviePlayer.cpp index 2e37ccc04..fd0fe7297 100644 --- a/Runtime/Graphics/CMoviePlayer.cpp +++ b/Runtime/Graphics/CMoviePlayer.cpp @@ -457,7 +457,7 @@ CMoviePlayer::CMoviePlayer(const char* path, float preLoadSeconds, bool loop, bo { boo::ITexture* texs[] = {set.Y[j], set.U, set.V}; set.binding[j] = ctx.newShaderDataBinding(YUVShaderPipeline, vtxFmt, m_vertBuf, - nullptr, nullptr, 1, bufs, 3, texs); + nullptr, nullptr, 1, bufs, nullptr, 3, texs); } } else @@ -476,7 +476,7 @@ CMoviePlayer::CMoviePlayer(const char* path, float preLoadSeconds, bool loop, bo boo::IGraphicsBuffer* bufs[] = {m_blockBuf}; boo::ITexture* texs[] = {set.Y[0], set.U, set.V}; set.binding[0] = ctx.newShaderDataBinding(YUVShaderPipeline, vtxFmt, m_vertBuf, - nullptr, nullptr, 1, bufs, 3, texs); + nullptr, nullptr, 1, bufs, nullptr, 3, texs); } if (xf4_25_hasAudio) set.audioBuf.reset(new s16[x28_thpHead.maxAudioSamples * 2]); diff --git a/Runtime/GuiSys/CTextRenderBuffer.cpp b/Runtime/GuiSys/CTextRenderBuffer.cpp index 95d6f2afc..e896510ff 100644 --- a/Runtime/GuiSys/CTextRenderBuffer.cpp +++ b/Runtime/GuiSys/CTextRenderBuffer.cpp @@ -95,7 +95,7 @@ void CTextRenderBuffer::CommitResources() boo::ITexture* texs[] = {chs.m_font.GetObj()->GetTexture()->GetBooTexture()}; chs.m_dataBinding = ctx.newShaderDataBinding(g_TextShaderPipeline, vFmt, nullptr, chs.m_instBuf, nullptr, - 1, uniforms, 1, texs); + 1, uniforms, nullptr, 1, texs); } for (BooImage& img : m_images) @@ -126,7 +126,7 @@ void CTextRenderBuffer::CommitResources() boo::ITexture* texs[] = {tex->GetBooTexture()}; img.m_dataBinding.push_back(ctx.newShaderDataBinding(g_TextImageShaderPipeline, vFmt, nullptr, img.m_instBuf, nullptr, - 1, uniforms, 1, texs)); + 1, uniforms, nullptr, 1, texs)); } } return true; diff --git a/Runtime/Particle/CElementGenShadersGLSL.cpp b/Runtime/Particle/CElementGenShadersGLSL.cpp index 746184710..fc4de49de 100644 --- a/Runtime/Particle/CElementGenShadersGLSL.cpp +++ b/Runtime/Particle/CElementGenShadersGLSL.cpp @@ -259,11 +259,11 @@ struct OGLElementDataBindingFactory : CElementGenShaders::IDataBindingFactory if (regPipeline) gen.m_normalDataBind = ctx.newShaderDataBinding(regPipeline, vtxFmt, nullptr, gen.m_instBuf, nullptr, 1, uniforms, - texCount, textures); + nullptr, texCount, textures); if (redToAlphaPipeline) gen.m_redToAlphaDataBind = ctx.newShaderDataBinding(redToAlphaPipeline, vtxFmt, nullptr, gen.m_instBuf, nullptr, 1, uniforms, - texCount, textures); + nullptr, texCount, textures); } }; diff --git a/Runtime/Particle/CElementGenShadersMetal.cpp b/Runtime/Particle/CElementGenShadersMetal.cpp index 86dbe081a..828528121 100644 --- a/Runtime/Particle/CElementGenShadersMetal.cpp +++ b/Runtime/Particle/CElementGenShadersMetal.cpp @@ -240,11 +240,11 @@ struct MetalElementDataBindingFactory : CElementGenShaders::IDataBindingFactory if (regPipeline) gen.m_normalDataBind = ctx.newShaderDataBinding(regPipeline, nullptr, nullptr, gen.m_instBuf, nullptr, 1, uniforms, - texCount, textures); + nullptr, texCount, textures); if (redToAlphaPipeline) gen.m_redToAlphaDataBind = ctx.newShaderDataBinding(redToAlphaPipeline, nullptr, nullptr, gen.m_instBuf, nullptr, 1, uniforms, - texCount, textures); + nullptr, texCount, textures); } }; diff --git a/hecl b/hecl index d5f9da391..6050696cf 160000 --- a/hecl +++ b/hecl @@ -1 +1 @@ -Subproject commit d5f9da3914d0ae1f4b4a4d8d0fddde160e406a6e +Subproject commit 6050696cf9752c9a239f16920c216356649d5ed3 diff --git a/specter b/specter index d49a33ec1..a8ae3714d 160000 --- a/specter +++ b/specter @@ -1 +1 @@ -Subproject commit d49a33ec1b936052ff1b4d02586748d59a27c174 +Subproject commit a8ae3714d2983d9faa9123151d71259c1fb9e111