Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/to cache pipeline #111

Open
wants to merge 13 commits into
base: metal-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cocos/math/HashAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include "math/HashAlgorithm.h"
#include "math/TimeUtils.h"

unsigned int HashAlgorithm::PJWHash(const char* str, size_t len)
{
// INIT_TIMER
// START_TIMER
unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;

for (size_t i = 0; i < len; i++)
{
hash = (hash << OneEighth) + str[i];

if ((test = hash & HighBits) != 0)
{
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
// STOP_TIMER("HashAlgorithm::PJWHash")
return hash;
}
/* End Of P. J. Weinberger Hash Function */
14 changes: 14 additions & 0 deletions cocos/math/HashAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#ifndef _HASHALGORITHM_H_
#define _HASHALGORITHM_H_
Mee-gu marked this conversation as resolved.
Show resolved Hide resolved

#include <stddef.h>

class HashAlgorithm {

public:
static unsigned int PJWHash(const char* str, size_t len);
};


#endif /* _HASHALGORITHM_H_ */
5 changes: 4 additions & 1 deletion src/backend/BlendState.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "BlendState.h"

CC_BACKEND_BEGIN

BlendState::BlendState(const BlendDescriptor& descriptor)
{
_blendDescriptor = descriptor;
Mee-gu marked this conversation as resolved.
Show resolved Hide resolved
}
CC_BACKEND_END
2 changes: 2 additions & 0 deletions src/backend/BlendState.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class BlendState : public cocos2d::Ref
{
protected:
virtual ~BlendState() = default;
BlendState(const BlendDescriptor& descriptor);
Mee-gu marked this conversation as resolved.
Show resolved Hide resolved
BlendDescriptor _blendDescriptor;
};

CC_BACKEND_END
7 changes: 6 additions & 1 deletion src/backend/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ class Device : public cocos2d::Ref
virtual Buffer* newBuffer(uint32_t size, BufferType type, BufferUsage usage) = 0;
// Create a render pass, not auto released.
virtual RenderPass* newRenderPass(const RenderPassDescriptor& descriptor) = 0;
// Cache RenderPass for reuse
virtual RenderPass* cacheRenderPass(const RenderPassDescriptor& descriptor) = 0;
Mee-gu marked this conversation as resolved.
Show resolved Hide resolved
// Create a texture, not auto released.
virtual Texture* newTexture(const TextureDescriptor& descriptor) = 0;
// Create a auto released shader module.
virtual ShaderModule* createShaderModule(ShaderStage stage, const std::string& source) = 0;
// Cache ShaderModule for reuse
virtual ShaderModule* cacheShaderModule(ShaderStage stage, const std::string& source) = 0;
// Create a auto released depth stencil state.
virtual DepthStencilState* createDepthStencilState(const DepthStencilDescriptor& descriptor) = 0;
// Create a auto released blend state.
virtual BlendState* createBlendState(const BlendDescriptor& descriptor) = 0;
// Create a render pipeline, not auto released.
virtual RenderPipeline* newRenderPipeline(const RenderPipelineDescriptor& descriptor) = 0;

// Cache RenderPipeline for reuse
virtual RenderPipeline* cacheRenderPipeline(const RenderPipelineDescriptor& descriptor) = 0;
private:
static Device* _instance;
};
Expand Down
16 changes: 16 additions & 0 deletions src/backend/RenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@ RenderPass::RenderPass(const RenderPassDescriptor& descriptor)
RenderPass::~RenderPass()
{}

bool RenderPass::Find(const RenderPassDescriptor& descriptor) const
{
if(_colorAttachmentsSet != descriptor.getColorAttachmentSet() ||
_depthStencilAttachmentSet != descriptor.getDepthStencilAttachmentSet())
return false;

const RenderPassColorAttachments colorAttachment = descriptor.getColorAttachments();
const RenderPassDepthStencilAttachment depthStencilAttachment = descriptor.getDepthStencilAttachment();

if(_colorAttachments == colorAttachment &&
_depthStencilAttachment == depthStencilAttachment)
return true;

return false;
}

CC_BACKEND_END
1 change: 1 addition & 0 deletions src/backend/RenderPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RenderPass : public cocos2d::Ref

inline const RenderPassDepthStencilAttachment& getDepthStencilAttachment() const { return _depthStencilAttachment; }
inline const RenderPassColorAttachments& getColorAttachments() const { return _colorAttachments; }
bool Find(const RenderPassDescriptor& descriptor) const;

protected:
RenderPass(const RenderPassDescriptor& descriptor);
Expand Down
25 changes: 25 additions & 0 deletions src/backend/RenderPassDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ void RenderPassColorAttachments::retainTextures() const
CC_SAFE_RETAIN(texture);
}

bool RenderPassColorAttachments::operator==(const RenderPassColorAttachments& colorAttachment) const
{
if(needClearColor != colorAttachment.needClearColor ||
clearColor != colorAttachment.clearColor)
return false;

return textures == colorAttachment.textures;
}

RenderPassDepthStencilAttachment::RenderPassDepthStencilAttachment(const RenderPassDepthStencilAttachment& rhs)
{
*this = rhs;
Expand All @@ -75,6 +84,22 @@ RenderPassDepthStencilAttachment& RenderPassDepthStencilAttachment::operator=(co
return *this;
}

bool RenderPassDepthStencilAttachment::operator==(const RenderPassDepthStencilAttachment& renderAttachment) const
{
if(needClearDepth != renderAttachment.needClearDepth ||
clearDepth != renderAttachment.clearDepth ||
needClearStencil != renderAttachment.needClearStencil ||
clearStencil != renderAttachment.clearStencil)
return false;

if(texture && renderAttachment.texture)
return texture->getTextureHashCode() == renderAttachment.texture->getTextureHashCode();
else if(!texture && !(renderAttachment.texture))
return true;

return false;
}

void RenderPassDescriptor::setColorAttachment(uint32_t attachment, Texture* texture)
{
assert(TextureUsage::RENDER_TARGET == texture->getTextureUsage());
Expand Down
4 changes: 4 additions & 0 deletions src/backend/RenderPassDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct RenderPassColorAttachments
~RenderPassColorAttachments();
RenderPassColorAttachments& operator=(const RenderPassColorAttachments& rhs);
void setTexture(uint32_t attachment, Texture* texture);
bool operator==(const RenderPassColorAttachments& colorAttachment) const;

std::vector<Texture*> textures;

Expand All @@ -35,6 +36,7 @@ struct RenderPassDepthStencilAttachment
RenderPassDepthStencilAttachment(const RenderPassDepthStencilAttachment& rhs);
~RenderPassDepthStencilAttachment();
RenderPassDepthStencilAttachment& operator =(const RenderPassDepthStencilAttachment& rhs);
bool operator==(const RenderPassDepthStencilAttachment& renderAttachment) const;

float clearDepth = 1.f;
bool needClearDepth = false;
Expand All @@ -58,6 +60,8 @@ class RenderPassDescriptor

inline const RenderPassDepthStencilAttachment& getDepthStencilAttachment() const { return _depthStencilAttachment; }
inline const RenderPassColorAttachments& getColorAttachments() const { return _colorAttachments; }
inline bool getColorAttachmentSet() const {return _colorAttachmentsSet;}
inline bool getDepthStencilAttachmentSet() const {return _depthStencilAttachmentSet;}

private:
bool _colorAttachmentsSet = false;
Expand Down
200 changes: 200 additions & 0 deletions src/backend/StringUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
//
// StringUtils.cpp
// Test
//
// Created by Cocos on 2018/11/2.
// Copyright © 2018 cocos. All rights reserved.
//

#include "StringUtils.hpp"

CC_BACKEND_BEGIN

std::string StringUtils::TextureFormat2String(const TextureFormat& textureFormat)
{
switch (textureFormat) {
case TextureFormat::R8G8B8A8:
return "R8G8B8A8";
case TextureFormat::R8G8B8:
return "R8G8B8";
case TextureFormat::A8:
return "A8";
default:
return "";
}
}

std::string StringUtils::TextureType2String(const TextureType& textureType)
{
switch (textureType) {
case TextureType::TEXTURE_2D:
return "TEXTURE_2D";
case TextureType::TEXTURE_CUBE:
return "TEXTURE_CUBE";
default:
return "";
}
}

std::string StringUtils::TextureUsage2String(const TextureUsage& textureUsage)
{
switch (textureUsage) {
case TextureUsage::READ:
return "READ";
case TextureUsage::WRITE:
return "WRITE";
case TextureUsage::RENDER_TARGET:
return "RENDER_TARGET";
default:
return "";
}
}

std::string StringUtils::SamplerFilterType2String(const SamplerFilter& filterType)
{
switch (filterType) {
case SamplerFilter::LINEAR:
return "LINEAR";
case SamplerFilter::NEAREST:
return "NEAREST";
default:
return "";
}
}

std::string StringUtils::SamplerAddressMode2String(const SamplerAddressMode& addressMode)
{
switch (addressMode) {
case SamplerAddressMode::REPEAT:
return "REPEAT";
case SamplerAddressMode::MIRROR_REPEAT:
return "MIRROR_REPEAT";
case SamplerAddressMode::CLAMP_TO_EDGE:
return "CLAMP_TO_EDGE";
default:
return "";
}
}

std::string StringUtils::SamplerDescriptor2String(const SamplerDescriptor& descriptor)
{
std::string samplerInfo = descriptor.mipmapEnabled ? "mipmapEnable":"mipmapDisable";
samplerInfo += SamplerFilterType2String(descriptor.magFilter);
samplerInfo += SamplerFilterType2String(descriptor.minFilter);
samplerInfo += SamplerFilterType2String(descriptor.mipmapFilter);
samplerInfo += SamplerAddressMode2String(descriptor.sAddressMode);
samplerInfo += SamplerAddressMode2String(descriptor.tAddressMode);
return samplerInfo;
}

std::string StringUtils::StencilOperation2String(const StencilOperation& operation)
{
switch (operation) {
case StencilOperation::KEEP:
return "KEEP";
case StencilOperation::ZERO:
return "ZERO";
case StencilOperation::REPLACE:
return "REPLACE";
case StencilOperation::INVERT:
return "INVERT";
case StencilOperation::INCREMENT_WRAP:
return "INCREMENT_WRAP";
case StencilOperation::DECREMENT_WRAP:
return "DECREMENT_WRAP";
default:
return "";
}
}

std::string StringUtils::CompareFunction2String(const CompareFunction& compareFunction)
{
switch (compareFunction) {
case CompareFunction::NEVER:
return "NEVER";
case CompareFunction::LESS:
return "LESS";
case CompareFunction::LESS_EQUAL:
return "LESS_EQUAL";
case CompareFunction::GREATER:
return "GREATER";
case CompareFunction::GREATER_EQUAL:
return "GREATER_EQUAL";
case CompareFunction::EQUAL:
return "EQUAL";
case CompareFunction::NOT_EQUAL:
return "NOT_EQUAL";
case CompareFunction::ALWAYS:
return "ALWAYS";
default:
return "";
}
}

std::string StringUtils::ColorWriteMask2String(const ColorWriteMask& colorWriteMask)
{
switch (colorWriteMask) {
case ColorWriteMask::NONE:
return "NONE";
case ColorWriteMask::RED:
return "RED";
case ColorWriteMask::GREEN:
return "GREEN";
case ColorWriteMask::BLUE:
return "BLUE";
case ColorWriteMask::ALPHA:
return "ALPHA";
case ColorWriteMask::ALL:
return "ALL";
default:
return "";
}
}

std::string StringUtils::BlendOperation2String(const BlendOperation& blendOperation)
{
switch (blendOperation) {
case BlendOperation::ADD:
return "ADD";
case BlendOperation::SUBTRACT:
return "SUBTRACT";
case BlendOperation::RESERVE_SUBTRACT:
return "RESERVE_SUBTRACT";
default:
return "";
}
}

std::string StringUtils::BlendFactor2String(const BlendFactor& blendFactor)
{
switch (blendFactor) {
case BlendFactor::ZERO:
return "ZERO";
case BlendFactor::ONE:
return "ONE";
case BlendFactor::SRC_COLOR:
return "SRC_COLOR";
case BlendFactor::ONE_MINUS_SRC_COLOR:
return "ONE_MINUS_SRC_COLOR";
case BlendFactor::SRC_ALPHA:
return "SRC_ALPHA";
case BlendFactor::ONE_MINUS_SRC_ALPHA:
return "ONE_MINUS_SRC_ALPHA";
case BlendFactor::DST_COLOR:
return "DST_COLOR";
case BlendFactor::ONE_MINUS_DST_COLOR:
return "ONE_MINUS_DST_COLOR";
case BlendFactor::DST_ALPHA:
return "DST_ALPHA";
case BlendFactor::ONE_MINUS_DST_ALPHA:
return "ONE_MINUS_DST_ALPHA";
case BlendFactor::SRC_ALPHA_SATURATE:
return "SRC_ALPHA_SATURATE";
case BlendFactor::BLEND_CLOLOR:
return "BLEND_CLOLOR";
default:
return "";
}
}

CC_BACKEND_END
Loading