Skip to content

Commit

Permalink
Another set of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Nov 4, 2023
1 parent 3a4333c commit 55ff113
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ enum plMetalPipelineType
ShadowCaster,
ShadowRender,
Clear,
Dynamic
Dynamic,
Text
};

//MARK: Base pipeline state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
217 changes: 28 additions & 189 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalTextFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ You can contact Cyan Worlds, Inc. by email [email protected]
Mead, WA 99021
*==LICENSE==*/
///////////////////////////////////////////////////////////////////////////////
// //
// plDXTextFont Class Functions //
// Cyan, Inc. //
// //
//// Version History //////////////////////////////////////////////////////////
// //
// 2.19.2001 mcn - Created. //
// //
///////////////////////////////////////////////////////////////////////////////

#include "plMetalTextFont.h"

Expand All @@ -61,14 +51,13 @@ You can contact Cyan Worlds, Inc. by email [email protected]
// * 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;
Expand All @@ -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();
Expand All @@ -109,97 +96,32 @@ 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<uint32_t[]>(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;
out->g = in->g * 255;
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)
{
}

//// IInitStateBlocks /////////////////////////////////////////////////////////

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 ///////////////////////////////////////////////////////////
Expand All @@ -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);
Expand All @@ -287,34 +172,14 @@ 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 ///////////////////////////////////////////////////////////////
// Flushes out and finishes any drawing left to be done.

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 ///////////////////////////////////////////////////////////////
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 55ff113

Please sign in to comment.