diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/FixedPipelineShaders.metal b/Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/FixedPipelineShaders.metal index 2fb497d31e..77581983c7 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/FixedPipelineShaders.metal +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/FixedPipelineShaders.metal @@ -200,7 +200,7 @@ vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]], if (lightSource->scale == 0.0h) continue; - // w is attenation + // direction.w is attenuation float4 direction; if (lightSource->position.w == 0.0) { diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipelineState.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipelineState.h index 61882871b6..021baa0bcc 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipelineState.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipelineState.h @@ -58,7 +58,8 @@ enum plMetalPipelineType ShadowCaster, ShadowRender, Clear, - Dynamic + Dynamic, + Text }; //MARK: Base pipeline state diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPlateManager.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPlateManager.h index 3b824766f9..27d579302a 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPlateManager.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPlateManager.h @@ -70,7 +70,7 @@ class plMetalPlatePipelineState : public plMetalPipelineState void ConfigureVertexDescriptor(MTL::VertexDescriptor *vertexDescriptor) override; - void GetFunctionConstants(MTL::FunctionConstantValues *) const override; + void GetFunctionConstants(MTL::FunctionConstantValues*) const override; }; class plMetalPlateManager : public plPlateManager diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.cpp index ac7a398fe6..3f8a412ca4 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.cpp @@ -39,16 +39,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com Mead, WA 99021 *==LICENSE==*/ -/////////////////////////////////////////////////////////////////////////////// -// // -// plDXTextFont Class Functions // -// Cyan, Inc. // -// // -//// Version History ////////////////////////////////////////////////////////// -// // -// 2.19.2001 mcn - Created. // -// // -/////////////////////////////////////////////////////////////////////////////// #include "plMetalTextFont.h" @@ -61,14 +51,13 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com // * 4 primitives per char max (for bold text) // * 3 verts per primitive -// const uint32_t kNumVertsInBuffer(32768); -const uint32_t kNumVertsInBuffer(4608); +constexpr uint32_t kNumVertsInBuffer = 4608; uint32_t plMetalTextFont::fBufferCursor = 0; //// Constructor & Destructor ///////////////////////////////////////////////// -plMetalTextFont::plMetalTextFont(plPipeline *pipe, plMetalDevice *device) : plTextFont(pipe), +plMetalTextFont::plMetalTextFont(plPipeline *pipe, plMetalDevice* device) : plTextFont(pipe), fTexture() { fDevice = device; @@ -85,8 +74,6 @@ plMetalTextFont::~plMetalTextFont() void plMetalTextFont::ICreateTexture(uint16_t *data) { - printf("Create texture\n"); - MTL::TextureDescriptor *descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, fTextureWidth, fTextureHeight, false); fTexture->release(); @@ -109,10 +96,10 @@ void plMetalTextFont::ICreateTexture(uint16_t *data) uint8_t a; }; - uint32_t *outData = new uint32_t[fTextureWidth * fTextureHeight]; - for (int i = 0; i < fTextureWidth * fTextureHeight; i++) { - InDataValues *in = (InDataValues *)(data + i); - OutDataValues *out = (OutDataValues *)(outData + i); + auto outData = std::make_unique(fTextureWidth * fTextureHeight); + for (size_t i = 0; i < fTextureWidth * fTextureHeight; i++) { + InDataValues* in = (InDataValues*)(data + i); + OutDataValues* out = (OutDataValues*)(outData.get() + i); out->r = in->r * 255; out->b = in->b * 255; @@ -120,34 +107,14 @@ void plMetalTextFont::ICreateTexture(uint16_t *data) out->a = in->a * 255; } - fTexture->replaceRegion(MTL::Region(0, 0, fTextureWidth, fTextureHeight), 0, outData, 4 * fTextureWidth); - delete[] outData; - /* - HRESULT hr; - D3DLOCKED_RECT lockInfo; - D3DCAPS9 d3dCaps; - - - // Check to make sure we can support it - fDevice->GetDeviceCaps( &d3dCaps ); - hsAssert( fTextureWidth <= d3dCaps.MaxTextureWidth, "Cannot initialize DX font--texture size too big" ); - - // Create our texture object - hr = fDevice->CreateTexture(fTextureWidth, fTextureHeight, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, &fD3DTexture, nullptr); - hsAssert( !FAILED( hr ), "Cannot create D3D texture" ); - - // Lock the texture and write our values out - fD3DTexture->LockRect(0, &lockInfo, nullptr, 0); - memcpy( lockInfo.pBits, data, fTextureWidth * fTextureHeight * sizeof( uint16_t ) ); - fD3DTexture->UnlockRect( 0 ); - */ + fTexture->replaceRegion(MTL::Region(0, 0, fTextureWidth, fTextureHeight), 0, outData.get(), 4 * fTextureWidth); } -void plMetalTextFont::CreateShared(plMetalDevice *device) +void plMetalTextFont::CreateShared(plMetalDevice* device) { } -void plMetalTextFont::ReleaseShared(MTL::Device *device) +void plMetalTextFont::ReleaseShared(MTL::Device* device) { } @@ -155,51 +122,6 @@ void plMetalTextFont::ReleaseShared(MTL::Device *device) void plMetalTextFont::IInitStateBlocks() { - /* - for( int i = 0; i < 2; i++ ) - { - fDevice->BeginStateBlock(); - fDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); - fDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); - fDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); - fDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE ); - fDevice->SetRenderState( D3DRS_ALPHAREF, 0x08 ); - fDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL ); - fDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); - fDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); - - fDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); - fDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS ); - fDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); - fDevice->SetRenderState( D3DRS_DEPTHBIAS, 0 ); - - fDevice->SetRenderState( D3DRS_STENCILENABLE, FALSE ); - fDevice->SetRenderState( D3DRS_CLIPPING, TRUE ); - fDevice->SetRenderState( D3DRS_ANTIALIASEDLINEENABLE, FALSE ); - fDevice->SetRenderState( D3DRS_VERTEXBLEND, FALSE ); - fDevice->SetRenderState( D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE ); - fDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); - fDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); - fDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); - fDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); - fDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); - fDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); - fDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); - fDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); - fDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); - fDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE ); - fDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 ); - fDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 ); - fDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); - fDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); - fDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); - - if( i == 0 ) - fDevice->EndStateBlock( &fOldStateBlock ); - else - fDevice->EndStateBlock( &fTextStateBlock ); - } - */ } //// DestroyObjects /////////////////////////////////////////////////////////// @@ -211,69 +133,32 @@ void plMetalTextFont::DestroyObjects() //// IDrawPrimitive /////////////////////////////////////////////////////////// -void plMetalTextFont::IDrawPrimitive(uint32_t count, plFontVertex *array) +void plMetalTextFont::IDrawPrimitive(uint32_t count, plFontVertex* array) { - plFontVertex *v; + plFontVertex* v; - plMetalDevice::plMetalLinkedPipeline *linkedPipeline = plMetalTextFontPipelineState(fDevice).GetRenderPipelineState(); + plMetalDevice::plMetalLinkedPipeline* linkedPipeline = plMetalTextFontPipelineState(fDevice).GetRenderPipelineState(); fPipeline->fDevice.CurrentRenderCommandEncoder()->setRenderPipelineState(linkedPipeline->pipelineState); - const uint maxCount = 4096 / (sizeof(plFontVertex) * 3); - uint drawm = 0; + constexpr size_t maxCount = 4096 / (sizeof(plFontVertex) * 3); + + uint drawn = 0; while (count > 0) { uint drawCount = MIN(maxCount, count); - fPipeline->fDevice.CurrentRenderCommandEncoder()->setVertexBytes(array + (drawm * 3), drawCount * 3 * sizeof(plFontVertex), 0); + fPipeline->fDevice.CurrentRenderCommandEncoder()->setVertexBytes(array + (drawn * 3), drawCount * 3 * sizeof(plFontVertex), 0); fPipeline->fDevice.CurrentRenderCommandEncoder()->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), drawCount * 3); count -= drawCount; - drawm += drawCount; - } - - // if( !fBuffer ) - // return; - - /// Lock the buffer and write to it - /*if( fBufferCursor && (fBufferCursor + count * 3 < kNumVertsInBuffer) ) - { - // We can lock part of it - if( FAILED( fBuffer->Lock( fBufferCursor * sizeof( plFontVertex ), - count * 3 * sizeof( plFontVertex ), - (void **)&v, D3DLOCK_NOOVERWRITE ) ) ) - { - hsAssert( false, "Failed to lock vertex buffer for writing" ); - return; - } - - fBufferCursor += count * 3; + drawn += drawCount; } - else - { - // Gotta start over - FlushDraws(); - fBufferCursor = count * 3; - - if( FAILED( fBuffer->Lock( 0, count * 3 * sizeof( plFontVertex ), - (void **)&v, D3DLOCK_DISCARD ) ) ) - { - hsAssert( false, "Failed to lock vertex buffer for writing" ); - return; - } - } - - if (v != nullptr && array != nullptr) - { - memcpy( v, array, count * sizeof( plFontVertex ) * 3 ); - } - - fBuffer->Unlock();*/ } //// IDrawLines /////////////////////////////////////////////////////////////// -void plMetalTextFont::IDrawLines(uint32_t count, plFontVertex *array) +void plMetalTextFont::IDrawLines(uint32_t count, plFontVertex* array) { - plMetalDevice::plMetalLinkedPipeline *linkedPipeline = plMetalTextFontPipelineState(fDevice).GetRenderPipelineState(); + plMetalDevice::plMetalLinkedPipeline* linkedPipeline = plMetalTextFontPipelineState(fDevice).GetRenderPipelineState(); fPipeline->fDevice.CurrentRenderCommandEncoder()->setRenderPipelineState(linkedPipeline->pipelineState); fPipeline->fDevice.CurrentRenderCommandEncoder()->setVertexBytes(array, count * 2 * sizeof(plFontVertex), 0); @@ -287,16 +172,6 @@ void plMetalTextFont::IDrawLines(uint32_t count, plFontVertex *array) fPipeline->fDevice.CurrentRenderCommandEncoder()->setFragmentTexture(fTexture, 0); fPipeline->fDevice.CurrentRenderCommandEncoder()->drawPrimitives(MTL::PrimitiveTypeLine, NS::UInteger(0), count * 2); - /*if( !fBuffer ) - return; - - if (count == 0 || array == nullptr) - return; - - fDevice->SetVertexShader(nullptr); - fDevice->SetFVF(kFVF); - fDevice->SetStreamSource(0, fBuffer, 0, sizeof(plFontVertex)); - fDevice->DrawPrimitiveUP( D3DPT_LINELIST, count, (const void *)array, sizeof( plFontVertex ) );*/ } //// FlushDraws /////////////////////////////////////////////////////////////// @@ -304,17 +179,7 @@ void plMetalTextFont::IDrawLines(uint32_t count, plFontVertex *array) void plMetalTextFont::FlushDraws() { - /*if( !fBuffer ) - return; - - if( fBufferCursor > 0 ) - { - fDevice->SetVertexShader(nullptr); - fDevice->SetFVF(kFVF); - fDevice->SetStreamSource( 0, fBuffer, 0, sizeof( plFontVertex ) ); - fDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, fBufferCursor / 3 ); - fBufferCursor = 0; - }*/ + // Metal don't flush } //// SaveStates /////////////////////////////////////////////////////////////// @@ -328,77 +193,51 @@ void plMetalTextFont::SaveStates() mat.columns[3][1] = 1.0; fPipeline->fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&mat, sizeof(matrix_float4x4), 1); fPipeline->fDevice.CurrentRenderCommandEncoder()->setFragmentTexture(fTexture, 0); - /*if( !fInitialized ) - IInitObjects(); - - if (fOldStateBlock) - fOldStateBlock->Capture(); - if (fTextStateBlock) - fTextStateBlock->Apply(); - - fDevice->SetTexture( 0, fD3DTexture ); - fDevice->SetTransform( D3DTS_TEXTURE0, &d3dIdentityMatrix ); - - /// Set up the transform matrices so that the vertices can range (0-screenWidth,0-screenHeight) - fDevice->SetTransform( D3DTS_WORLD, &d3dIdentityMatrix ); - fDevice->SetTransform( D3DTS_VIEW, &d3dIdentityMatrix ); - D3DMATRIX mat; - mat = d3dIdentityMatrix; - mat.m[0][0] = 2.0f / (float)fPipe->Width(); - mat.m[1][1] = -2.0f / (float)fPipe->Height(); - mat.m[3][0] = -1.0; - mat.m[3][1] = 1.0; - fDevice->SetTransform( D3DTS_PROJECTION, &mat );*/ } //// RestoreStates //////////////////////////////////////////////////////////// void plMetalTextFont::RestoreStates() { - /*if (fOldStateBlock) - fOldStateBlock->Apply(); - - fDevice->SetTexture(0, nullptr); - fDevice->SetTransform( D3DTS_TEXTURE0, &d3dIdentityMatrix );*/ } -bool plMetalTextFontPipelineState::IsEqual(const plMetalPipelineState &p) const +bool plMetalTextFontPipelineState::IsEqual(const plMetalPipelineState& p) const { return true; } -plMetalPipelineState *plMetalTextFontPipelineState::Clone() +plMetalPipelineState* plMetalTextFontPipelineState::Clone() { return new plMetalTextFontPipelineState(fDevice); } -const MTL::Function *plMetalTextFontPipelineState::GetVertexFunction(MTL::Library *library) +const MTL::Function* plMetalTextFontPipelineState::GetVertexFunction(MTL::Library* library) { return library->newFunction(MTLSTR("textFontVertexShader")); } -const MTL::Function *plMetalTextFontPipelineState::GetFragmentFunction(MTL::Library *library) +const MTL::Function* plMetalTextFontPipelineState::GetFragmentFunction(MTL::Library* library) { return library->newFunction(MTLSTR("textFontFragmentShader")); } -const NS::String *plMetalTextFontPipelineState::GetDescription() +const NS::String* plMetalTextFontPipelineState::GetDescription() { return MTLSTR("Font Rendering"); } -void plMetalTextFontPipelineState::ConfigureBlend(MTL::RenderPipelineColorAttachmentDescriptor *descriptor) +void plMetalTextFontPipelineState::ConfigureBlend(MTL::RenderPipelineColorAttachmentDescriptor* descriptor) { descriptor->setSourceRGBBlendFactor(MTL::BlendFactorSourceAlpha); descriptor->setDestinationRGBBlendFactor(MTL::BlendFactorOneMinusSourceAlpha); } -void plMetalTextFontPipelineState::ConfigureVertexDescriptor(MTL::VertexDescriptor *vertexDescriptor) +void plMetalTextFontPipelineState::ConfigureVertexDescriptor(MTL::VertexDescriptor* vertexDescriptor) { return; } -void plMetalTextFontPipelineState::GetFunctionConstants(MTL::FunctionConstantValues *) const +void plMetalTextFontPipelineState::GetFunctionConstants(MTL::FunctionConstantValues*) const { return; } diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.h index f5eed85271..061dbfb5ee 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.h @@ -58,7 +58,7 @@ class plMetalTextFontPipelineState : public plMetalPipelineState public: plMetalTextFontPipelineState(plMetalDevice* device) : plMetalPipelineState(device){}; bool IsEqual(const plMetalPipelineState& p) const override; - uint16_t GetID() const override { return 6; }; + uint16_t GetID() const override { return plMetalPipelineType::Text; }; plMetalPipelineState* Clone() override; const MTL::Function* GetVertexFunction(MTL::Library* library) override; const MTL::Function* GetFragmentFunction(MTL::Library* library) override; diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.cpp index ebe2aa0a8d..7aeedf0f3b 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.cpp @@ -41,14 +41,14 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com *==LICENSE==*/ #include "plMetalVertexShader.h" -#include - #include "HeadSpin.h" -#include "hsWindows.h" + #include "plDrawable/plGBufferGroup.h" #include "plMetalPipeline.h" #include "plSurface/plShader.h" +#include + plMetalVertexShader::plMetalVertexShader(plShader* owner) : plMetalShader(owner) { @@ -61,9 +61,7 @@ plMetalVertexShader::~plMetalVertexShader() void plMetalVertexShader::Release() { - fPipe = nil; - - // ISetError(nil); + fPipe = nullptr; } bool plMetalVertexShader::ISetConstants(plMetalPipeline* pipe) diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.h index 0364dd789c..0f040ac832 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalVertexShader.h @@ -54,10 +54,10 @@ class plMetalVertexShader : public plMetalShader public: bool ISetConstants(plMetalPipeline* pipe) override; // On error, sets error string. plMetalVertexShader(plShader* owner); - virtual ~plMetalVertexShader(); + ~plMetalVertexShader() override; void Link(plMetalVertexShader** back) { plMetalDeviceRef::Link((plMetalDeviceRef**)back); } - plMetalVertexShader* GetNext() { return (plMetalVertexShader*)fNext; } + plMetalVertexShader* GetNext() const { return (plMetalVertexShader*)fNext; } void Release() override; }; diff --git a/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.cpp b/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.cpp index e97d5752e3..a2bafdad28 100644 --- a/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.cpp +++ b/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.cpp @@ -207,13 +207,10 @@ void hsGMaterial::SetLayer(plLayerInterface* layer, int32_t which, bool insert, } } -#if PLASMA_PIPELINE_GL || PLASMA_PIPELINE_METAL void hsGMaterial::SetDeviceRef(hsGDeviceRef* ref) { hsRefCnt_SafeAssign(fDeviceRef, ref); } -#endif - void hsGMaterial::Write(hsStream* s) { diff --git a/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.h b/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.h index a7bad93b40..72ee7b81c7 100644 --- a/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.h +++ b/Sources/Plasma/PubUtilLib/plSurface/hsGMaterial.h @@ -93,9 +93,7 @@ class hsGMaterial : public plSynchedObject float fLastUpdateTime; -#if PLASMA_PIPELINE_GL || PLASMA_PIPELINE_METAL hsGDeviceRef* fDeviceRef; -#endif void IClearLayers(); size_t IMakeExtraLayer(); @@ -134,10 +132,8 @@ class hsGMaterial : public plSynchedObject bool IsDecal() const { return (fCompFlags & kCompDecal); } bool NeedsBlendChannel() { return (fCompFlags & kCompNeedsBlendChannel); } -#if PLASMA_PIPELINE_GL || PLASMA_PIPELINE_METAL void SetDeviceRef(hsGDeviceRef* ref); hsGDeviceRef* GetDeviceRef() const { return fDeviceRef; } -#endif virtual void Read(hsStream* s); virtual void Write(hsStream* s);