Skip to content

Commit

Permalink
Merge pull request #179 from hgy29/master
Browse files Browse the repository at this point in the history
WIP vector graphics, android binary plugins and a few bugfixes
  • Loading branch information
ar2rsawseen committed Oct 14, 2015
2 parents 2e850fe + a9b6038 commit ac0e92b
Show file tree
Hide file tree
Showing 43 changed files with 6,390 additions and 113 deletions.
3 changes: 3 additions & 0 deletions 2dsg/application.cpp
Expand Up @@ -482,6 +482,9 @@ void Application::setResolution(int width, int height)
{
width_ = width;
height_ = height;

if (ShaderEngine::Engine)
ShaderEngine::Engine->resizeFramebuffer(width_,height_);

calculateLogicalTransformation();
}
Expand Down
29 changes: 29 additions & 0 deletions 2dsg/gfxbackends/Shaders.cpp
Expand Up @@ -14,6 +14,8 @@ ShaderProgram *ShaderProgram::stdColor=NULL;
ShaderProgram *ShaderProgram::stdTexture=NULL;
ShaderProgram *ShaderProgram::stdTextureColor=NULL;
ShaderProgram *ShaderProgram::stdParticle=NULL;
ShaderProgram *ShaderProgram::pathShaderFillC=NULL;
ShaderProgram *ShaderProgram::pathShaderStrokeC=NULL;
ShaderEngine *ShaderEngine::Engine=NULL;

void ShaderProgram::Retain()
Expand Down Expand Up @@ -83,6 +85,33 @@ void ShaderEngine::reset(bool reinit)
oglVPProjection.identity();
oglModel.identity();
oglCombined.identity();
dsCurrent.dTest=false;
dsCurrent.sRef=0;
dsCurrent.sMask=0xFF;
dsCurrent.sClear=false;
dsCurrent.sFail=STENCIL_KEEP;
dsCurrent.dFail=STENCIL_KEEP;
dsCurrent.dPass=STENCIL_KEEP;
dsCurrent.sFunc=STENCIL_DISABLE;
while (!dsStack.empty())
dsStack.pop();
setDepthStencil(dsCurrent);
}

ShaderEngine::DepthStencil ShaderEngine::pushDepthStencil()
{
dsStack.push(dsCurrent);
return dsCurrent;
}

void ShaderEngine::popDepthStencil()
{
if (!dsStack.empty())
{
dsCurrent=dsStack.top();
dsStack.pop();
setDepthStencil(dsCurrent);
}
}

void ShaderEngine::prepareDraw(ShaderProgram *program)
Expand Down
46 changes: 43 additions & 3 deletions 2dsg/gfxbackends/Shaders.h
Expand Up @@ -63,15 +63,17 @@ class ShaderProgram
static ShaderProgram *stdTexture;
static ShaderProgram *stdTextureColor;
static ShaderProgram *stdParticle;
static ShaderProgram *pathShaderFillC;
static ShaderProgram *pathShaderStrokeC;
enum StdData {
DataVertex=0, DataColor=1, DataTexture=2
};
virtual void activate()=0;
virtual void deactivate()=0;
virtual void setData(int index,DataType type,int mult,const void *ptr,unsigned int count, bool modified, ShaderBufferCache **cache)=0;
virtual void setData(int index,DataType type,int mult,const void *ptr,unsigned int count, bool modified, ShaderBufferCache **cache,int stride=0,int offset=0)=0;
virtual void setConstant(int index,ConstantType type, int mult,const void *ptr)=0;
virtual void drawArrays(ShapeType shape, int first, unsigned int count)=0;
virtual void drawElements(ShapeType shape, unsigned int count, DataType type, const void *indices, bool modified, ShaderBufferCache **cache)=0;
virtual void drawElements(ShapeType shape, unsigned int count, DataType type, const void *indices, bool modified, ShaderBufferCache **cache,unsigned int first=0,unsigned int dcount=0)=0;
virtual bool isValid()=0;
virtual const char *compilationLog()=0;
void Retain();
Expand Down Expand Up @@ -123,10 +125,44 @@ class ShaderBuffer
virtual ~ShaderBuffer() { };
virtual void prepareDraw()=0;
virtual void readPixels(int x,int y,int width,int height,ShaderTexture::Format format,ShaderTexture::Packing packing,void *data)=0;
virtual void unbound()=0;
virtual void needDepthStencil()=0;
};

class ShaderEngine
{
public:
enum StencilOp {
STENCIL_KEEP,
STENCIL_ZERO,
STENCIL_REPLACE,
STENCIL_INCR,
STENCIL_INCR_WRAP,
STENCIL_DECR,
STENCIL_DECR_WRAP,
STENCIL_INVERT
};
enum StencilFunc {
STENCIL_DISABLE,
STENCIL_NEVER,
STENCIL_LESS,
STENCIL_LEQUAL,
STENCIL_GREATER,
STENCIL_GEQUAL,
STENCIL_EQUAL,
STENCIL_NOTEQUAL,
STENCIL_ALWAYS
};
struct DepthStencil {
bool dTest;
StencilFunc sFunc;
int sRef;
unsigned int sMask;
StencilOp sFail;
StencilOp dFail;
StencilOp dPass;
bool sClear;
};
protected:
//CONSTANTS
Matrix4 oglProjection;
Expand Down Expand Up @@ -165,6 +201,8 @@ class ShaderEngine
int x,y,w,h;
};
std::stack<Scissor> scissorStack;
std::stack<DepthStencil> dsStack;
DepthStencil dsCurrent;
public:
enum BlendFactor
{
Expand Down Expand Up @@ -207,7 +245,9 @@ class ShaderEngine
virtual void setColor(float r,float g,float b,float a);
virtual void clearColor(float r,float g,float b,float a)=0;
virtual void bindTexture(int num,ShaderTexture *texture)=0;
virtual void setDepthTest(bool enable)=0;
virtual ShaderEngine::DepthStencil pushDepthStencil();
virtual void popDepthStencil();
virtual void setDepthStencil(DepthStencil state)=0;
virtual void setBlendFunc(BlendFactor sfactor, BlendFactor dfactor)=0;
//Clipping
virtual void pushClip(float x,float y,float w,float h);
Expand Down
41 changes: 41 additions & 0 deletions 2dsg/gfxbackends/dx11/PathFillC.hlsl
@@ -0,0 +1,41 @@
Texture2D myTexture : register(t0);
SamplerState samLinear : register(s0);

struct VOut
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD;
};

cbuffer cbv : register(b0)
{
float4x4 mvp;
};

VOut VShader(float4 position : vVertex)
{
VOut output;

position.w = 1.0f;
position.z = 0.0f;

output.position = mul(mvp, position);
output.uv = position.zw;

return output;
}


cbuffer cbp : register(b1)
{
float4 fColor;
};

float4 PShader(float4 position : SV_POSITION, float2 uv: TEXCOORD) : SV_TARGET
{
float u = uv.x;
float v = uv.x;
float4 frag = fColor;
if (u*u > v) discard;
return frag;
}
84 changes: 84 additions & 0 deletions 2dsg/gfxbackends/dx11/PathStrokeC.hlsl
@@ -0,0 +1,84 @@
Texture2D myTexture : register(t0);
SamplerState samLinear : register(s0);

struct VOut
{
float4 position : SV_POSITION;
float2 pos : TEXCOORD1;
float2 pq: TEXCOORD2;
float2 a: TEXCOORD3,b : TEXCOORD4, c : TEXCOORD5;
float2 ow : TEXCOORD6;
};

cbuffer cbv : register(b0)
{
float4x4 mvp;
};

VOut VShader(float4 d0 : dataA, float4 d1: dataB, float4 d2: dataC)
{
VOut output;

output.position = mul(mvp, float4(d0.xy,0.0f,1.0f));
output.pos = d0.xy;
output.pq = d0.zw;
output.a = d1.xy;
output.b = d1.zw;
output.c = d2.xy;
output.ow = d2.zw;

return output;
}


cbuffer cbp : register(b1)
{
float4 fColor;
};

#define M_PI 3.1415926535897932384626433832795

float2 evaluateQuadratic(float t, float2 a: TEXCOORD3, float2 b : TEXCOORD4, float2 c : TEXCOORD5)
{
return a * t * t + b * t + c;
}

bool check(float t, float2 pos: TEXCOORD1, float2 ow : TEXCOORD6, float2 a : TEXCOORD3, float2 b : TEXCOORD4, float2 c : TEXCOORD5)
{
if (0.0f <= t && t <= 1.0f)
{
float2 q = evaluateQuadratic(t,a,b,c) - pos;
if (dot(q, q) <= ow.y)
return false;
}

return true;
}

float cbrt(float x)
{
return sign(x) * pow(abs(x), 1.0f / 3.0f);
}

float4 PShader(float2 pq: TEXCOORD2, float2 ow : TEXCOORD6, float2 pos : TEXCOORD1, float2 a : TEXCOORD3, float2 b : TEXCOORD4, float2 c : TEXCOORD5) : SV_TARGET
{
float d = pq.y * pq.y / 4.0f + pq.x * pq.x * pq.x / 27.0f;

if (d >= 0.0f)
{
float c1 = -pq.y / 2.0f;
float c2 = sqrt(d);
if (check(cbrt(c1 + c2) + cbrt(c1 - c2) + ow.x,pos,ow,a,b,c)) discard;
}
else
{
float cos_3_theta = 3.0f * pq.y * sqrt(-3.0f / pq.x) / (2.0f * pq.x);
float theta = acos(cos_3_theta) / 3.0f;
float r = 2.0f * sqrt(-pq.x / 3.0f);

if (check(r * cos(theta) + ow.x, pos, ow, a,b,c) &&
check(r * cos(theta + 2.0f * M_PI / 3.0f) + ow.x, pos, ow, a,b,c) &&
check(r * cos(theta + 4.0f * M_PI / 3.0f) + ow.x, pos, ow, a,b,c)) discard;
}
return fColor;
}
8 changes: 8 additions & 0 deletions 2dsg/gfxbackends/dx11/compileshaders.bat
Expand Up @@ -13,8 +13,16 @@ fxc /T ps_4_0_level_9_3 /E PShader /Fo pTextureColor.cso TextureColor.hlsl
fxc /T vs_4_0_level_9_3 /E VShader /Fo vParticle.cso Particle.hlsl
fxc /T ps_4_0_level_9_3 /E PShader /Fo pParticle.cso Particle.hlsl

fxc /T vs_4_0_level_9_3 /E VShader /Fo vPathFillC.cso PathFillC.hlsl
fxc /T ps_4_0_level_9_3 /E PShader /Fo pPathFillC.cso PathFillC.hlsl

fxc /T vs_4_0_level_9_3 /E VShader /Fo vPathStrokeC.cso PathStrokeC.hlsl
fxc /T ps_4_0_level_9_3 /E PShader /Fo pPathStrokeC.cso PathStrokeC.hlsl

bin2c -o dx11_shaders.c vBasic.cso pBasic.cso ^
vColor.cso pColor.cso ^
vTexture.cso pTexture.cso ^
vTextureColor.cso pTextureColor.cso ^
vPathFillC.cso pPathFillC.cso ^
vPathStrokeC.cso pPathStrokeC.cso ^
vParticle.cso pParticle.cso
49 changes: 49 additions & 0 deletions 2dsg/gfxbackends/dx11/dx11ShaderBuffer.cpp
Expand Up @@ -14,6 +14,8 @@ dx11ShaderBuffer::dx11ShaderBuffer(ShaderTexture *texture)
ID3D11Resource *res;
((dx11ShaderTexture *)texture)->rsv->GetDesc(&desc);
((dx11ShaderTexture *)texture)->rsv->GetResource(&res);
width = ((dx11ShaderTexture *)texture)->width;
height = ((dx11ShaderTexture *)texture)->height;

D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;

Expand All @@ -24,6 +26,7 @@ dx11ShaderBuffer::dx11ShaderBuffer(ShaderTexture *texture)
renderTargetViewDesc.Texture2D.MipSlice = 0;

g_dev->CreateRenderTargetView(res, &renderTargetViewDesc, &renderTarget);
depthStencil = NULL;
}

dx11ShaderBuffer::~dx11ShaderBuffer()
Expand All @@ -35,6 +38,52 @@ void dx11ShaderBuffer::prepareDraw()
{
}

void dx11ShaderBuffer::unbound()
{
if (depthStencil)
{
depthStencil->Release();
depthStencilTexture->Release();
}
depthStencil = NULL;
}

void dx11ShaderBuffer::needDepthStencil()
{
if (!depthStencil)
{

//Depth / Stencil setup
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
g_dev->CreateTexture2D(&descDepth, NULL, &depthStencilTexture);

D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;// DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;

// Create the depth stencil view
g_dev->CreateDepthStencilView(depthStencilTexture, // Depth stencil texture
&descDSV, // Depth stencil desc
&depthStencil); // [out] Depth stencil view
//Attach the newly created depth/stencil buf
g_devcon->OMSetRenderTargets(1,&renderTarget,depthStencil);
}
}

void dx11ShaderBuffer::readPixels(int x,int y,int width,int height,ShaderTexture::Format format,ShaderTexture::Packing packing,void *data)
{
//TODO
Expand Down

0 comments on commit ac0e92b

Please sign in to comment.