Skip to content

Commit

Permalink
Updating code to match style
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Oct 18, 2023
1 parent 2d83176 commit a8c98e1
Show file tree
Hide file tree
Showing 24 changed files with 2,665 additions and 2,715 deletions.
6 changes: 5 additions & 1 deletion Sources/Plasma/FeatureLib/pfMetalPipeline/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ BreakBeforeBraces: Custom
AlignConsecutiveDeclarations: AcrossComments
AlignConsecutiveAssignments:
Enabled: False
ReflowComments: false
ReflowComments: true
LambdaBodyIndentation: OuterScope
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
PackConstructorInitializers: CurrentLine
ObjCBlockIndentWidth: 4
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesBeforeTrailingComments: 1
---
Language: ObjC
# Obj-C specific settings go here
758 changes: 374 additions & 384 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalDevice.cpp

Large diffs are not rendered by default.

257 changes: 126 additions & 131 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#ifndef _plMetalDevice_h_
#define _plMetalDevice_h_

#include "HeadSpin.h"

#include "hsGMatState.h"

#include "plMetalDeviceRef.h"
#include "hsMatrix44.h"
#include <simd/simd.h>

#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#include <simd/simd.h>

#include <unordered_map>
#include <condition_variable>
#include <unordered_map>

#include "HeadSpin.h"
#include "hsGMatState.h"
#include "hsMatrix44.h"
#include "plMetalDeviceRef.h"
#include "plSurface/plShader.h"
#include "plSurface/plShaderTable.h"

Expand All @@ -67,50 +64,49 @@ class plCubicEnvironmap;
class plLayerInterface;
class plMetalPipelineState;

//NOTE: Results of this will be row major
// NOTE: Results of this will be row major
matrix_float4x4* hsMatrix2SIMD(const hsMatrix44& src, matrix_float4x4* dst);

class plMetalDevice
{

friend plMetalPipeline;
friend class plMetalMaterialShaderRef;
friend class plMetalPlateManager;
friend class plMetalPipelineState;
public:

public:
typedef plMetalVertexBufferRef VertexBufferRef;
typedef plMetalIndexBufferRef IndexBufferRef;
typedef plMetalTextureRef TextureRef;
public:
plMetalPipeline* fPipeline;
hsWindowHndl fDevice;
hsWindowHndl fWindow;
const char* fErrorMsg;
MTL::RenderCommandEncoder* CurrentRenderCommandEncoder();
MTL::Device* fMetalDevice;
MTL::CommandQueue* fCommandQueue;
MTL::Buffer* fCurrentIndexBuffer;
size_t fActiveThread;
matrix_float4x4 fMatrixProj;
matrix_float4x4 fMatrixL2W;
matrix_float4x4 fMatrixW2L;
matrix_float4x4 fMatrixW2C;
matrix_float4x4 fMatrixC2W;
public:

struct plMetalLinkedPipeline {
const MTL::RenderPipelineState *pipelineState;
const MTL::Function *fragFunction;
const MTL::Function *vertexFunction;

public:
plMetalPipeline* fPipeline;

hsWindowHndl fDevice;
hsWindowHndl fWindow;

const char* fErrorMsg;

MTL::RenderCommandEncoder* CurrentRenderCommandEncoder();
MTL::Device* fMetalDevice;
MTL::CommandQueue* fCommandQueue;
MTL::Buffer* fCurrentIndexBuffer;

size_t fActiveThread;
matrix_float4x4 fMatrixProj;
matrix_float4x4 fMatrixL2W;
matrix_float4x4 fMatrixW2L;
matrix_float4x4 fMatrixW2C;
matrix_float4x4 fMatrixC2W;

public:
struct plMetalLinkedPipeline
{
const MTL::RenderPipelineState* pipelineState;
const MTL::Function* fragFunction;
const MTL::Function* vertexFunction;
};

plMetalDevice();

bool InitDevice();
Expand All @@ -128,9 +124,8 @@ class plMetalDevice
/** Translate our viewport into a GL viewport. */
void SetViewport();


bool BeginRender();

/* Device Ref Functions **************************************************/
void SetupVertexBufferRef(plGBufferGroup* owner, uint32_t idx, VertexBufferRef* vRef);
void CheckStaticVertexBuffer(VertexBufferRef* vRef, plGBufferGroup* owner, uint32_t idx);
Expand All @@ -144,126 +139,126 @@ class plMetalDevice
void CheckTexture(TextureRef* tRef);
void MakeTextureRef(TextureRef* tRef, plMipmap* img);
void MakeCubicTextureRef(TextureRef* tRef, plCubicEnvironmap* img);



const char* GetErrorString() const { return fErrorMsg; }

void SetProjectionMatrix(const hsMatrix44& src);
void SetWorldToCameraMatrix(const hsMatrix44& src);
void SetLocalToWorldMatrix(const hsMatrix44& src);
void PopulateTexture(plMetalDevice::TextureRef *tRef, plMipmap *img, uint slice);
uint ConfigureAllowedLevels(plMetalDevice::TextureRef *tRef, plMipmap *mipmap);
//stencil states are expensive to make, they should be cached
//FIXME: There should be a function to pair these with hsGMatState
MTL::DepthStencilState *fNoZReadStencilState;
MTL::DepthStencilState *fNoZWriteStencilState;
MTL::DepthStencilState *fNoZReadOrWriteStencilState;
MTL::DepthStencilState *fReverseZStencilState;
MTL::DepthStencilState *fDefaultStencilState;
uint8_t fSampleCount;
///Create a new command buffer to encode all the operations needed to draw a frame
//Currently requires a CA drawable and not a Metal drawable. In since CA drawable is only abstract implementation I know about, not sure where we would find others?
void CreateNewCommandBuffer(CA::MetalDrawable* drawable);

void PopulateTexture(plMetalDevice::TextureRef* tRef, plMipmap* img, uint slice);
uint ConfigureAllowedLevels(plMetalDevice::TextureRef* tRef, plMipmap* mipmap);

// stencil states are expensive to make, they should be cached
// FIXME: There should be a function to pair these with hsGMatState
MTL::DepthStencilState* fNoZReadStencilState;
MTL::DepthStencilState* fNoZWriteStencilState;
MTL::DepthStencilState* fNoZReadOrWriteStencilState;
MTL::DepthStencilState* fReverseZStencilState;
MTL::DepthStencilState* fDefaultStencilState;
uint8_t fSampleCount;

/// Create a new command buffer to encode all the operations needed to draw a frame
// Currently requires a CA drawable and not a Metal drawable. In since CA drawable is only abstract implementation I know about, not sure where we would find others?
void CreateNewCommandBuffer(CA::MetalDrawable* drawable);
MTL::CommandBuffer* GetCurrentCommandBuffer();
CA::MetalDrawable* GetCurrentDrawable();
///Submit the command buffer to the GPU and draws all the render passes. Clears the current command buffer.
void SubmitCommandBuffer();
void Clear(bool shouldClearColor, simd_float4 clearColor, bool shouldClearDepth, float clearDepth);
CA::MetalDrawable* GetCurrentDrawable();
/// Submit the command buffer to the GPU and draws all the render passes. Clears the current command buffer.
void SubmitCommandBuffer();
void Clear(bool shouldClearColor, simd_float4 clearColor, bool shouldClearDepth, float clearDepth);

void SetMaxAnsiotropy(uint8_t maxAnsiotropy);
void SetMSAASampleCount(uint8_t sampleCount);

MTL::SamplerState* SampleStateForClampFlags(hsGMatState::hsGMatClampFlags sampleState);

NS::UInteger CurrentTargetSampleCount() {

NS::UInteger CurrentTargetSampleCount()
{
if (fCurrentRenderTarget) {
return 1;
} else {
return fSampleCount;
}
}

void BlitTexture(MTL::Texture* src, MTL::Texture* dst);

void EncodeBlur(MTL::CommandBuffer* commandBuffer, MTL::Texture* texture, float sigma);

MTL::PixelFormat GetFramebufferFormat() { return fFramebufferFormat; };
private:

struct plMetalPipelineRecord {
MTL::PixelFormat depthFormat;
MTL::PixelFormat colorFormat;
NS::UInteger sampleCount;

private:
struct plMetalPipelineRecord
{
MTL::PixelFormat depthFormat;
MTL::PixelFormat colorFormat;
NS::UInteger sampleCount;
std::shared_ptr<plMetalPipelineState> state;
bool operator==(const plMetalPipelineRecord &p) const;

bool operator==(const plMetalPipelineRecord& p) const;
};



struct plMetalPipelineRecordHashFunction
{
std::size_t operator()(plMetalPipelineRecord const& s) const noexcept;
};
std::unordered_map<plMetalPipelineRecord, plMetalLinkedPipeline *, plMetalPipelineRecordHashFunction> fNewPipelineStateMap;
//the condition map allows consumers of pipeline states to wait until the pipeline state is ready
std::unordered_map<plMetalPipelineRecord, std::condition_variable *, plMetalPipelineRecordHashFunction> fConditionMap;
std::mutex fPipelineCreationMtx;
void StartPipelineBuild(plMetalPipelineRecord& record, std::condition_variable **condOut);
std::condition_variable* PrewarmPipelineStateFor(plMetalPipelineState* pipelineState);
protected:

std::unordered_map<plMetalPipelineRecord, plMetalLinkedPipeline*, plMetalPipelineRecordHashFunction> fNewPipelineStateMap;
// the condition map allows consumers of pipeline states to wait until the pipeline state is ready
std::unordered_map<plMetalPipelineRecord, std::condition_variable*, plMetalPipelineRecordHashFunction> fConditionMap;
std::mutex fPipelineCreationMtx;
void StartPipelineBuild(plMetalPipelineRecord& record, std::condition_variable** condOut);
std::condition_variable* PrewarmPipelineStateFor(plMetalPipelineState* pipelineState);

protected:
plMetalLinkedPipeline* PipelineState(plMetalPipelineState* pipelineState);

MTL::Texture* fGammaLUTTexture;

void SetFramebufferFormat(MTL::PixelFormat format);

private:
MTL::PixelFormat fFramebufferFormat;

//these are internal bits for backing the current render pass
//private because the functions should be used to keep a consistant
//render pass state
MTL::CommandBuffer* fCurrentCommandBuffer;
MTL::CommandBuffer* fCurrentOffscreenCommandBuffer;
MTL::RenderCommandEncoder* fCurrentRenderTargetCommandEncoder;

MTL::Texture* fCurrentDrawableDepthTexture;
MTL::Texture* fCurrentFragmentOutputTexture;
MTL::Texture* fCurrentUnprocessedOutputTexture;
MTL::Texture* fCurrentFragmentMSAAOutputTexture;

CA::MetalDrawable* fCurrentDrawable;
MTL::PixelFormat fCurrentDepthFormat;
simd_float4 fClearRenderTargetColor;
simd_float4 fClearDrawableColor;
bool fShouldClearRenderTarget;
bool fShouldClearDrawable;
float fClearRenderTargetDepth;
float fClearDrawableDepth;
plRenderTarget* fCurrentRenderTarget;
MTL::SamplerState* fSamplerStates[4];

MTL::CommandBuffer* fBlitCommandBuffer;
MTL::BlitCommandEncoder* fBlitCommandEncoder;

bool NeedsPostprocessing() {

private:
MTL::PixelFormat fFramebufferFormat;

// these are internal bits for backing the current render pass
// private because the functions should be used to keep a consistant
// render pass state
MTL::CommandBuffer* fCurrentCommandBuffer;
MTL::CommandBuffer* fCurrentOffscreenCommandBuffer;
MTL::RenderCommandEncoder* fCurrentRenderTargetCommandEncoder;

MTL::Texture* fCurrentDrawableDepthTexture;
MTL::Texture* fCurrentFragmentOutputTexture;
MTL::Texture* fCurrentUnprocessedOutputTexture;
MTL::Texture* fCurrentFragmentMSAAOutputTexture;

CA::MetalDrawable* fCurrentDrawable;
MTL::PixelFormat fCurrentDepthFormat;
simd_float4 fClearRenderTargetColor;
simd_float4 fClearDrawableColor;
bool fShouldClearRenderTarget;
bool fShouldClearDrawable;
float fClearRenderTargetDepth;
float fClearDrawableDepth;
plRenderTarget* fCurrentRenderTarget;
MTL::SamplerState* fSamplerStates[4];

MTL::CommandBuffer* fBlitCommandBuffer;
MTL::BlitCommandEncoder* fBlitCommandEncoder;

bool NeedsPostprocessing()
{
return fGammaLUTTexture != nullptr;
}
void PostprocessIntoDrawable();
void CreateGammaAdjustState();
void PostprocessIntoDrawable();
void CreateGammaAdjustState();
MTL::RenderPipelineState* fGammaAdjustState;

void BeginNewRenderPass();
void ReleaseSamplerStates();
void ReleaseFramebufferObjects();
//Blur states

// Blur states
std::unordered_map<float, NS::Object*> fBlurShaders;
};

Expand Down
Loading

0 comments on commit a8c98e1

Please sign in to comment.