port from perforce

This commit is contained in:
2026-04-18 22:31:51 +02:00
commit 8d0ab5b7cc
8409 changed files with 3972376 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
#include "stdafx.h"
#include "Bone.h"
#include "IResourceFactory.h"
#include "ITransformationAnimation.h"
Bone::Bone(IEngine& argEngine)
: m_Engine(argEngine)
, m_Parent(NULL)
, m_TransformationAnimation(NULL)
, m_BoneIndex(-1)
, m_LastAnimationTime(-1.0f)
, m_MatrixArray(NULL)
{
}
Bone::~Bone()
{
delete m_MatrixArray;
m_Engine.get_ResourceFactory().DeleteBone(*const_cast<Bone*>(this), true);
}
void Bone::Initialize(const std::wstring& argName, int argIndex, const D3DXVECTOR3& argPoseTranslation, const D3DXVECTOR3& argPoseScale, const D3DXQUATERNION& argPoseRotation)
{
D3DXMatrixTransformation(&m_PoseTransformation, NULL, NULL, &argPoseScale, NULL, &argPoseRotation, &argPoseTranslation);
D3DXMatrixInverse(&m_InvPoseTransformation, NULL, &m_PoseTransformation);
}
const D3DXMATRIX& Bone::GetTransformationAtTime(float argT, const D3DXMATRIX& argRootTransformationMatrix)
{
if (m_TransformationAnimation != NULL)
{
if (argT != m_LastAnimationTime)
{
if (m_Parent != NULL)
{
m_Transformation = (m_TransformationAnimation->GetTransformationAtTime(argT) * m_InvPoseTransformation) * m_Parent->GetTransformationAtTime(argT, argRootTransformationMatrix);
m_Transformation = m_InvPoseTransformation * m_TransformationAnimation->GetTransformationAtTime(argT);
//m_Transformation = m_Transformation * ( * m_InvPoseTransformation);
}
else
m_Transformation = m_TransformationAnimation->GetTransformationAtTime(argT) * m_InvPoseTransformation;
}
}
return m_Transformation;
}
const D3DXMATRIX& Bone::get_PoseTransformation() const
{
return m_PoseTransformation;
}
void Bone::set_Parent(IBone* argParent)
{
if (m_Parent != NULL)
{
std::vector<IBone*>& children = m_Parent->get_ChildBones();
for (unsigned int i = 0; i < children.size(); ++i)
if (children[i] == this)
{
children.erase(children.begin() + i);
break;
}
}
m_Parent = argParent;
if (m_Parent != NULL)
m_Parent->get_ChildBones().push_back(this);
}
IBone* Bone::get_Parent() const
{
return m_Parent;
}
std::vector<IBone*>& Bone::get_ChildBones()
{
return m_Children;
}
std::wstring Bone::get_Name() const
{
return m_Name;
}
int Bone::get_Index() const
{
return m_BoneIndex;
}
void Bone::set_TransformationAnimation(ITransformationAnimation* argTa)
{
m_TransformationAnimation = argTa;
m_LastAnimationTime = -1.0f;
m_Transformation = D3DXMATRIX();
}
ITransformationAnimation* Bone::get_TransformationAnimation() const
{
return m_TransformationAnimation;
}
std::map<int, IBone*>& Bone::get_BoneIndexList()
{
if (m_Parent != NULL)
return m_Parent->get_BoneIndexList();
if (m_BoneIndexList.empty())
this->FillBoneIndexList(this, m_BoneIndexList);
return m_BoneIndexList;
}
D3DXMATRIX* Bone::get_MatrixArray()
{
if (m_MatrixArray == NULL)
{
m_MatrixArraySize = this->get_BoneIndexList().size();
m_MatrixArray = new D3DXMATRIX[m_MatrixArraySize];
}
return m_MatrixArray;
}
unsigned int Bone::get_MatrixArraySize()
{
if (m_MatrixArray == NULL)
this->get_MatrixArray();
return m_MatrixArraySize;
}
void Bone::FillBoneIndexList(IBone* argCurrentBone, std::map<int, IBone*>& argList)
{
argList[argCurrentBone->get_Index()] = argCurrentBone;
for (unsigned int i = 0; i < argCurrentBone->get_ChildBones().size(); ++i)
this->FillBoneIndexList(argCurrentBone->get_ChildBones()[i], argList);
}

View File

@@ -0,0 +1,57 @@
#pragma once
#include "IBone.h"
#include "IEngine.h"
class Bone
: public IBone
{
public:
Bone(IEngine& argEngine);
virtual ~Bone();
virtual void Initialize(const string16& argName, int argIndex, const D3DXVECTOR3& argPoseTranslation, const D3DXVECTOR3& argPoseScale, const D3DXQUATERNION& argPoseRotation);
virtual const D3DXMATRIX& GetTransformationAtTime(float argTime, const D3DXMATRIX& argRootTransformationMatrix);
virtual const D3DXMATRIX& get_PoseTransformation() const;
virtual void set_Parent(IBone* argParent_);
virtual IBone* get_Parent() const;
virtual std::vector<IBone*>& get_ChildBones();
virtual string16 get_Name() const;
virtual int get_Index() const;
virtual void set_TransformationAnimation(ITransformationAnimation* argTransformationAnimation);
virtual ITransformationAnimation* get_TransformationAnimation() const;
virtual std::map<int, IBone*>& get_BoneIndexList();
virtual D3DXMATRIX* get_MatrixArray();
virtual unsigned int get_MatrixArraySize();
private:
void FillBoneIndexList(IBone* argCurrentBone, std::map<int, IBone*>& argList);
private:
IEngine& m_Engine;
string16 m_Name;
int m_BoneIndex;
D3DXMATRIX m_PoseTransformation;
D3DXMATRIX m_InvPoseTransformation;
float m_LastAnimationTime;
D3DXMATRIX m_Transformation;
std::vector<IBone*> m_Children;
IBone* m_Parent;
ITransformationAnimation* m_TransformationAnimation;
std::map<int, IBone*> m_BoneIndexList;
D3DXMATRIX* m_MatrixArray;
unsigned int m_MatrixArraySize;
};

View File

@@ -0,0 +1,118 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "BoneController.h"
#include "../ShaderParameterCollection/Types/Types.h"
BoneController::BoneController(IEngine& argEngine)
: ShaderParameterCollection(argEngine)
, m_StartTime(0.0)
, m_LastMatrixArray(NULL)
{
}
BoneController::~BoneController()
{
m_Engine.get_ResourceFactory().DeleteBoneController(*const_cast<BoneController*>(this), true);
}
void BoneController::Initialize(IBone& argRootBone)
{
m_RootBone = &argRootBone;
m_Bones = m_RootBone->get_BoneIndexList();
}
void BoneController::Initialize(const IBoneController& argOther)
{
m_RootBone = &argOther.get_RootBone();
m_Bones = m_RootBone->get_BoneIndexList();
m_CurrentSequence = argOther.get_CurrentSequence();
m_Sequences = argOther.get_Sequences();
this->PlaySequence(m_CurrentSequence);
}
void BoneController::Apply()
{
float lf_Time = 0.0f;
if (!m_CurrentSequence.empty())
lf_Time = get_CurrentTime();
int i = 0;
D3DXMATRIX* lr_MatrixArray_ = m_RootBone->get_MatrixArray();
for (std::map<int, IBone*>::const_iterator lk_Iter = m_Bones.begin(); lk_Iter != m_Bones.end(); ++lk_Iter, ++i)
lr_MatrixArray_[i] = lk_Iter->second->GetTransformationAtTime(lf_Time, D3DXMATRIX());
if (m_LastMatrixArray != lr_MatrixArray_)
{
m_LastMatrixArray = lr_MatrixArray_;
this->SetParameter("BoneMatrix", new Matrix4x4ShaderParameter(m_LastMatrixArray, m_RootBone->get_MatrixArraySize(), ParameterBindType::BindBySemantic));
}
}
void BoneController::RegisterSequence(const string16& argName, float argStart, float argEnd, bool argLoop)
{
Sequence lr_NewSequence;
lr_NewSequence.m_Start = argStart;
lr_NewSequence.m_End = argEnd;
lr_NewSequence.m_Loop = argLoop;
m_Sequences[argName] = lr_NewSequence;
}
void BoneController::PlaySequence(const string16& argName)
{
m_StartTime = m_Engine.get_Timeline().get_DemoTime();
std::map<string16, Sequence >::iterator lk_Iter = m_Sequences.find(argName);
if (lk_Iter != m_Sequences.end())
{
m_CurrentSequenceInfo = lk_Iter->second;
m_CurrentSequence = argName;
}
else
m_CurrentSequence = string16();
}
const string16& BoneController::get_CurrentSequence() const
{
return m_CurrentSequence;
}
float BoneController::get_CurrentTime()
{
float lf_CurrentTime = static_cast<float>(m_Engine.get_Timeline().get_DemoTime() - m_StartTime);
lf_CurrentTime = lf_CurrentTime / (m_CurrentSequenceInfo.m_End - m_CurrentSequenceInfo.m_Start);
lf_CurrentTime = (m_CurrentSequenceInfo.m_End - m_CurrentSequenceInfo.m_Start) * lf_CurrentTime + m_CurrentSequenceInfo.m_Start;
if (lf_CurrentTime >= m_CurrentSequenceInfo.m_End)
{
while (m_CurrentSequenceInfo.m_Loop && lf_CurrentTime >= m_CurrentSequenceInfo.m_End)
{
lf_CurrentTime -= m_CurrentSequenceInfo.m_End;
m_StartTime += m_CurrentSequenceInfo.m_End;
}
if (!m_CurrentSequenceInfo.m_Loop)
lf_CurrentTime = m_CurrentSequenceInfo.m_End;
}
else if (lf_CurrentTime < m_CurrentSequenceInfo.m_Start)
{
while (m_CurrentSequenceInfo.m_Loop && lf_CurrentTime < m_CurrentSequenceInfo.m_Start)
{
lf_CurrentTime += m_CurrentSequenceInfo.m_Start;
m_StartTime -= m_CurrentSequenceInfo.m_Start;
}
if (!m_CurrentSequenceInfo.m_Loop)
lf_CurrentTime = m_CurrentSequenceInfo.m_Start;
}
return lf_CurrentTime;
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include "IBone.h"
#include "IEngine.h"
#include "ITimeLine.h"
#include "IUpdatable.h"
#include "../ShaderParameterCollection/ShaderParameterCollection.h"
class BoneController
: public IBoneController
, public ShaderParameterCollection
, public IUpdatable
{
public:
BoneController(IEngine& argEngine);
virtual ~BoneController();
virtual void Initialize(IBone& argRootBone);
virtual void Initialize(const IBoneController& argOther);
virtual void Apply();
virtual void RegisterSequence(const string16& argName, float argStart, float argEnd, bool argLoop);
virtual void PlaySequence(const string16& argName);
virtual const string16& get_CurrentSequence() const;
virtual const std::map<string16, Sequence >& get_Sequences() const { return m_Sequences; }
virtual IBone& get_RootBone() const { return *m_RootBone; }
// IUpdatable
virtual void Update(bool argForceUpdate) { this->Apply(); }
virtual bool get_WantsUpdate() const { return true; }
protected:
// ICommandUser
virtual string8 get_UserName() const { return "BoneController"; }
private:
float get_CurrentTime();
IBone* m_RootBone;
double m_StartTime;
string16 m_CurrentSequence;
Sequence m_CurrentSequenceInfo;
std::map<string16, Sequence > m_Sequences;
std::map<int, IBone*> m_Bones;
D3DXMATRIX* m_LastMatrixArray;
};

View File

@@ -0,0 +1,43 @@
#include "stdafx.h"
#include "BaseCamera.h"
BaseCamera::BaseCamera(IEngine& argEngine)
: Transformation(argEngine)
, m_NearClip(1.0f)
, m_FarClip(100.0f)
, m_AspectRatio(1.0f)
{
this->set_IsPreconditionForFollowingShaders(true);
this->RecreateAllShaderParameters();
}
void BaseCamera::set_AspectRatio(float argValue)
{
m_AspectRatio = argValue;
m_IsDirty = true;
}
void BaseCamera::set_FarClip(float argValue)
{
m_FarClip = argValue;
m_IsDirty = true;
}
void BaseCamera::set_NearClip(float argValue)
{
m_NearClip = argValue;
m_IsDirty = true;
}
void BaseCamera::RecreateAllShaderParameters()
{
this->SetParameter("ViewMatrix", &m_ViewMatrix, ParameterBindType::BindBySemantic);
this->SetParameter("ProjectionMatrix", &m_ProjectionMatrix, ParameterBindType::BindBySemantic);
Transformation::RecreateAllShaderParameters();
}

View File

@@ -0,0 +1,40 @@
#pragma once
#include "ICamera.h"
#include "IEngine.h"
#include "../Transformation/Transformation.h"
class BaseCamera
: public Transformation
, public ICamera
{
public:
BaseCamera(IEngine& argEngine);
virtual const D3DXMATRIX& get_ProjectionMatrix() const { return m_ProjectionMatrix; }
virtual const D3DXMATRIX& get_ViewMatrix() const { return m_ViewMatrix; }
virtual void set_AspectRatio(float argValue);
virtual float get_AspectRatio() const { return m_AspectRatio; }
virtual void set_FarClip(float argValue);
virtual float get_FarClip() const { return m_FarClip; }
virtual void set_NearClip(float argValue);
virtual float get_NearClip() const { return m_NearClip; }
virtual const ViewFrustum& get_ViewFrustum() const { return m_Frustum; }
protected:
virtual void RecreateAllShaderParameters();
protected:
float m_NearClip;
float m_FarClip;
float m_AspectRatio;
D3DXMATRIX m_ProjectionMatrix;
D3DXMATRIX m_ViewMatrix;
ViewFrustum m_Frustum;
};

View File

@@ -0,0 +1,89 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "OrthographicCamera.h"
OrthographicCamera::OrthographicCamera(IEngine& argEngine)
: BaseCamera(argEngine)
, m_Width(1.0f)
, m_Height(1.0f)
{
}
OrthographicCamera::~OrthographicCamera()
{
m_Engine.get_ResourceFactory().DeleteOrthographicCamera(*const_cast<OrthographicCamera*>(this), true);
}
void OrthographicCamera::set_Width(float argValue)
{
m_Width = argValue;
m_IsDirty = true;
}
void OrthographicCamera::set_Height(float argValue)
{
m_Height = argValue;
m_IsDirty = true;
}
void OrthographicCamera::Update(bool argForceUpdate)
{
BaseCamera::Update(argForceUpdate);
D3DXMATRIXA16 projectionMatrix;
D3DXMATRIXA16 viewMatrix;
D3DXVECTOR3 worldPos = this->get_WorldPosition();
D3DXVECTOR3 worldDirection = this->get_WorldDirection();
D3DXVECTOR3 worldUpDirection = this->get_WorldUpDirection();
::D3DXMatrixOrthoLH(&projectionMatrix, m_Width, m_Height, m_NearClip, m_FarClip);
::D3DXMatrixLookAtLH(&viewMatrix, &worldPos, &(worldPos + worldDirection), &worldUpDirection);
m_ProjectionMatrix = projectionMatrix;
m_ViewMatrix = viewMatrix;
// generate view frustum
D3DXMATRIXA16 viewProjection = viewMatrix * projectionMatrix;
// Left plane
m_Frustum.Plane[0].a = viewProjection._14 + viewProjection._11;
m_Frustum.Plane[0].b = viewProjection._24 + viewProjection._21;
m_Frustum.Plane[0].c = viewProjection._34 + viewProjection._31;
m_Frustum.Plane[0].d = viewProjection._44 + viewProjection._41;
// Right plane
m_Frustum.Plane[1].a = viewProjection._14 - viewProjection._11;
m_Frustum.Plane[1].b = viewProjection._24 - viewProjection._21;
m_Frustum.Plane[1].c = viewProjection._34 - viewProjection._31;
m_Frustum.Plane[1].d = viewProjection._44 - viewProjection._41;
// Top plane
m_Frustum.Plane[2].a = viewProjection._14 - viewProjection._12;
m_Frustum.Plane[2].b = viewProjection._24 - viewProjection._22;
m_Frustum.Plane[2].c = viewProjection._34 - viewProjection._32;
m_Frustum.Plane[2].d = viewProjection._44 - viewProjection._42;
// Bottom plane
m_Frustum.Plane[3].a = viewProjection._14 + viewProjection._12;
m_Frustum.Plane[3].b = viewProjection._24 + viewProjection._22;
m_Frustum.Plane[3].c = viewProjection._34 + viewProjection._32;
m_Frustum.Plane[3].d = viewProjection._44 + viewProjection._42;
// Near plane
m_Frustum.Plane[4].a = viewProjection._13;
m_Frustum.Plane[4].b = viewProjection._23;
m_Frustum.Plane[4].c = viewProjection._33;
m_Frustum.Plane[4].d = viewProjection._43;
// Far plane
m_Frustum.Plane[5].a = viewProjection._14 - viewProjection._13;
m_Frustum.Plane[5].b = viewProjection._24 - viewProjection._23;
m_Frustum.Plane[5].c = viewProjection._34 - viewProjection._33;
m_Frustum.Plane[5].d = viewProjection._44 - viewProjection._43;
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include "BaseCamera.h"
class OrthographicCamera
: public BaseCamera
, public IOrthographicCamera
{
public:
OrthographicCamera(IEngine& argEngine);
virtual ~OrthographicCamera();
virtual void set_Width(float argValue);
virtual float get_Width() const { return m_Width; }
virtual void set_Height(float argValue);
virtual float get_Height() const { return m_Height; }
virtual void Update(bool argForceUpdate);
virtual ICamera& get_Base() { return *dynamic_cast<ICamera*>(this); }
protected:
// ICommandUser
virtual string8 get_UserName() const { return "OrthographicCamera"; }
private:
float m_Width;
float m_Height;
};

View File

@@ -0,0 +1,80 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "PerspectiveCamera.h"
PerspectiveCamera::PerspectiveCamera(IEngine& argEngine)
: BaseCamera(argEngine)
, m_Fov(90.0f)
{
}
PerspectiveCamera::~PerspectiveCamera()
{
m_Engine.get_ResourceFactory().DeletePerspectiveCamera(*const_cast<PerspectiveCamera*>(this), true);
}
void PerspectiveCamera::set_Fov(float argFov)
{
m_Fov = argFov;
m_IsDirty = true;
}
void PerspectiveCamera::Update(bool argForceUpdate)
{
BaseCamera::Update(argForceUpdate);
D3DXMATRIXA16 projectionMatrix;
D3DXMATRIXA16 viewMatrix;
D3DXVECTOR3 worldPos = this->get_WorldPosition();
D3DXVECTOR3 worldDirection = this->get_WorldDirection();
D3DXVECTOR3 worldUpDirection = this->get_WorldUpDirection();
::D3DXMatrixPerspectiveFovLH(&projectionMatrix, static_cast<float>(D3DXToRadian(m_Fov)), m_AspectRatio, m_NearClip, m_FarClip);
::D3DXMatrixLookAtLH(&viewMatrix, &worldPos, &(worldPos + worldDirection), &worldUpDirection);
m_ProjectionMatrix = projectionMatrix;
m_ViewMatrix = viewMatrix;
// generate view frustum
D3DXMATRIXA16 viewProjection = viewMatrix * projectionMatrix;
// Left plane
m_Frustum.Plane[0].a = viewProjection._14 + viewProjection._11;
m_Frustum.Plane[0].b = viewProjection._24 + viewProjection._21;
m_Frustum.Plane[0].c = viewProjection._34 + viewProjection._31;
m_Frustum.Plane[0].d = viewProjection._44 + viewProjection._41;
// Right plane
m_Frustum.Plane[1].a = viewProjection._14 - viewProjection._11;
m_Frustum.Plane[1].b = viewProjection._24 - viewProjection._21;
m_Frustum.Plane[1].c = viewProjection._34 - viewProjection._31;
m_Frustum.Plane[1].d = viewProjection._44 - viewProjection._41;
// Top plane
m_Frustum.Plane[2].a = viewProjection._14 - viewProjection._12;
m_Frustum.Plane[2].b = viewProjection._24 - viewProjection._22;
m_Frustum.Plane[2].c = viewProjection._34 - viewProjection._32;
m_Frustum.Plane[2].d = viewProjection._44 - viewProjection._42;
// Bottom plane
m_Frustum.Plane[3].a = viewProjection._14 + viewProjection._12;
m_Frustum.Plane[3].b = viewProjection._24 + viewProjection._22;
m_Frustum.Plane[3].c = viewProjection._34 + viewProjection._32;
m_Frustum.Plane[3].d = viewProjection._44 + viewProjection._42;
// Near plane
m_Frustum.Plane[4].a = viewProjection._13;
m_Frustum.Plane[4].b = viewProjection._23;
m_Frustum.Plane[4].c = viewProjection._33;
m_Frustum.Plane[4].d = viewProjection._43;
// Far plane
m_Frustum.Plane[5].a = viewProjection._14 - viewProjection._13;
m_Frustum.Plane[5].b = viewProjection._24 - viewProjection._23;
m_Frustum.Plane[5].c = viewProjection._34 - viewProjection._33;
m_Frustum.Plane[5].d = viewProjection._44 - viewProjection._43;
}

View File

@@ -0,0 +1,27 @@
#pragma once
#include "BaseCamera.h"
class PerspectiveCamera
: public BaseCamera
, public IPerspectiveCamera
{
public:
PerspectiveCamera(IEngine& argEngine);
virtual ~PerspectiveCamera();
virtual void set_Fov(float argValue);
virtual float get_Fov() const { return m_Fov; }
virtual void Update(bool argForceUpdate);
virtual ICamera& get_Base() { return *dynamic_cast<ICamera*>(this); }
protected:
// ICommandUser
virtual string8 get_UserName() const { return "PerspectiveCamera"; }
private:
float m_Fov;
};

View File

@@ -0,0 +1,431 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "GeometryBuffer.h"
GeometryBuffer::GeometryBuffer(IEngine& argEngine)
: m_Engine(argEngine)
, m_VertexBuffer(NULL)
, m_IndexBuffer(NULL)
, m_InputElementDesc(NULL)
, m_VertexCount(0)
, m_VertexElementSize(0)
, m_IndexCount(0)
, m_VertexOffset(0)
, m_VertexDataMapped(false)
, m_IndexDataMapped(false)
, m_RawVertexData_(NULL)
, m_RawIndexData(NULL)
, m_PrimitiveTopology(PrimitiveTopology::TriangleList)
, m_IndexOffset(0)
, m_UsableIndexCount(0)
{
m_Commands.push_back(new Command(this, CommandFlags::None, SetIndexBufferCommandType, 1));
m_Commands.push_back(new Command(this, CommandFlags::None, SetVertexBufferCommandType, 0));
m_Commands.push_back(new Command(this, CommandFlags::EndChain | CommandFlags::Unique, DrawGeometryCommandType, 6));
}
GeometryBuffer::~GeometryBuffer()
{
this->Uninitialize();
m_Engine.get_ResourceFactory().DeleteGeometryBuffer(*const_cast<GeometryBuffer*>(this), true);
}
void GeometryBuffer::Uninitialize()
{
if (m_Engine.get_EngineStates().LastIndexBufferProvider == this)
m_Engine.get_EngineStates().LastIndexBufferProvider = NULL;
if (m_Engine.get_EngineStates().LastVertexBufferProvider == this)
m_Engine.get_EngineStates().LastVertexBufferProvider = NULL;
this->DeleteVertexData();
this->DeleteIndexData();
}
void GeometryBuffer::SetVertexData(uint32 argVertexCount, uint32 argVertexElementSize, void* a_VertexData_, const std::vector<VertexElement>& argVertexElements, bool argNeedsDynamicAccess)
{
this->DeleteVertexData();
D3D10_BUFFER_DESC desc;
desc.Usage = argNeedsDynamicAccess ? D3D10_USAGE_DYNAMIC : D3D10_USAGE_DEFAULT;
desc.ByteWidth = argVertexElementSize * argVertexCount;
desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = argNeedsDynamicAccess ? D3D10_CPU_ACCESS_WRITE : 0;
desc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = a_VertexData_;
if (S_OK != m_Engine.get_DX10Device().CreateBuffer(&desc, &initData, &m_VertexBuffer))
{
throw L"GeometryBuffer: Unable to create vertex buffer.";
}
m_VertexCount = argVertexCount;
m_VertexElementSize = argVertexElementSize;
m_VertexElements = argVertexElements;
m_RawVertexData_ = new char[argVertexElementSize * argVertexCount];
::memcpy(m_RawVertexData_, a_VertexData_, argVertexElementSize * argVertexCount);
if (m_InputElementDesc == NULL)
{
if (!m_VertexElements.empty())
{
m_InputElementDesc = new D3D10_INPUT_ELEMENT_DESC[m_VertexElements.size()];
for (uint32 i = 0; i < m_VertexElements.size(); ++i)
{
D3D10_INPUT_ELEMENT_DESC desc;
VertexElement& element = m_VertexElements[i];
desc.AlignedByteOffset = D3D10_APPEND_ALIGNED_ELEMENT;
desc.Format = (DXGI_FORMAT)element.m_ElementFormat;
desc.InputSlot = 0; // should this be a parameter too?
desc.InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
desc.InstanceDataStepRate = 0;
desc.SemanticIndex = element.m_NameIndex;
desc.SemanticName = element.m_SemanticName.c_str();
m_InputElementDesc[i] = desc;
}
}
}
}
void GeometryBuffer::DeleteVertexData()
{
if (m_VertexBuffer != NULL)
m_Engine.get_DX10Device().ClearState();
m_VertexElements.clear();
if (m_VertexBuffer != NULL)
m_VertexBuffer->Release();
m_VertexBuffer = NULL;
m_VertexCount = 0;
m_VertexElementSize = 0;
m_VertexDataMapped = false;
if (m_RawVertexData_ != NULL)
delete [] m_RawVertexData_;
m_RawVertexData_ = NULL;
if (m_InputElementDesc != NULL)
delete [] m_InputElementDesc;
m_InputElementDesc = NULL;
}
void GeometryBuffer::SetIndexData(uint32 argIndexCount, uint32* aui_IndexData_, bool argNeedsDynamicAccess)
{
this->DeleteIndexData();
D3D10_BUFFER_DESC desc;
desc.Usage = D3D10_USAGE_DEFAULT;
desc.ByteWidth = sizeof(uint32) * argIndexCount;
desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
desc.CPUAccessFlags = argNeedsDynamicAccess ? D3D10_CPU_ACCESS_WRITE : 0;
desc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = aui_IndexData_;
initData.SysMemPitch = 0;
initData.SysMemSlicePitch = 0;
if (S_OK != m_Engine.get_DX10Device().CreateBuffer(&desc, &initData, &m_IndexBuffer))
{
throw L"GeometryBuffer: Unable to create index buffer.";
}
m_IndexCount = argIndexCount;
m_UsableIndexCount = argIndexCount;
m_IndexOffset = 0;
m_RawIndexData = new uint32[m_IndexCount];
::memcpy(m_RawIndexData, aui_IndexData_, sizeof(uint32) * m_IndexCount);
}
void GeometryBuffer::DeleteIndexData()
{
if (m_IndexBuffer != NULL)
m_Engine.get_DX10Device().ClearState();
if (m_IndexBuffer != NULL)
m_IndexBuffer->Release();
m_IndexBuffer = NULL;
if (m_RawIndexData != NULL)
delete [] m_RawIndexData;
m_RawIndexData = NULL;
m_IndexCount = 0;
while (!m_VertexLayoutsPerShaderAndPass.empty())
{
while (!m_VertexLayoutsPerShaderAndPass.begin()->second.empty())
{
m_VertexLayoutsPerShaderAndPass.begin()->second.back()->Release();
m_VertexLayoutsPerShaderAndPass.begin()->second.pop_back();
}
m_VertexLayoutsPerShaderAndPass.erase(m_VertexLayoutsPerShaderAndPass.begin());
}
}
void* GeometryBuffer::MapVertexBuffer(DataAccessMode::Enumeration argAccessMode)
{
if (m_VertexBuffer == NULL)
{
throw L"GeometryBuffer: Access forbidden until resource has been initialized.";
}
if (m_VertexDataMapped)
{
throw L"GeometryBuffer: Only one access to the vertex buffer at one time possible.";
}
void* dataPointer = NULL;
if (S_OK != m_VertexBuffer->Map((D3D10_MAP)argAccessMode, 0, &dataPointer))
{
throw L"GeometryBuffer: Access to the vertex buffer was not successfull.";
}
m_VertexDataMapped = true;
return dataPointer;
}
void GeometryBuffer::UnmapVertexBuffer()
{
if (m_VertexBuffer == NULL)
{
throw L"GeometryBuffer: Access forbidden until resource has been initialized.";
}
if (!m_VertexDataMapped)
{
throw L"GeometryBuffer: No access was done ending the access to the vertex buffer is not possible.";
}
m_VertexBuffer->Unmap();
m_VertexDataMapped = false;
}
uint32* GeometryBuffer::MapIndexBuffer(DataAccessMode::Enumeration argAccessMode)
{
if (m_IndexBuffer == NULL)
{
throw L"GeometryBuffer: Access forbidden until resource has been initialized.";
}
if (m_IndexDataMapped)
{
throw L"GeometryBuffer: Only one access to the index buffer at one time possible.";
}
void* dataPointer = NULL;
if (S_OK != m_IndexBuffer->Map((D3D10_MAP)argAccessMode, 0, &dataPointer))
{
throw L"GeometryBuffer: Access to the index buffer was not successfull.";
}
m_IndexDataMapped = true;
return (uint32*)dataPointer;
}
void GeometryBuffer::UnmapIndexBuffer()
{
if (m_IndexBuffer == NULL)
{
throw L"GeometryBuffer: Access forbidden until resource has been initialized.";
}
if (!m_IndexDataMapped)
{
throw L"GeometryBuffer: No access was done ending the access to the index buffer is not possible.";
}
m_IndexBuffer->Unmap();
m_IndexDataMapped = false;
}
void GeometryBuffer::ConvertToAdjacency()
{
switch (m_PrimitiveTopology)
{
case PrimitiveTopology::LineList:
case PrimitiveTopology::LineStrip:
case PrimitiveTopology::TriangleList:
case PrimitiveTopology::TriangleStrip:
break;
default:
return;
}
D3D10_INPUT_ELEMENT_DESC inputLayout[2] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "END", 0, DXGI_FORMAT_R8_UINT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
inputLayout[1].AlignedByteOffset = m_VertexElementSize - 1;
// create the mesh
uint32 numVertices = m_VertexCount;
uint32 numIndices = m_IndexCount;
uint32 Options = D3DX10_MESH_32_BIT;
ID3DX10Mesh* mesh = NULL;
if (S_OK != D3DX10CreateMesh(&m_Engine.get_DX10Device(),
inputLayout,
2,
inputLayout[0].SemanticName,
numVertices,
numIndices / 3,
Options,
&mesh ) )
{
throw L"GeometryBuffer: Unable to create temp. mesh for adjacency data generation.";
}
//set the VB
mesh->SetVertexData(0, (void*)m_RawVertexData_);
//set the IB
mesh->SetIndexData((void*)m_RawIndexData, numIndices);
//generate adjacency
const float epsilon = 0.0f;
mesh->GenerateAdjacencyAndPointReps(epsilon);
//generate adjacency indices
mesh->GenerateGSAdjacency();
//get the adjacency data out of the mesh
ID3DX10MeshBuffer* indexBufferMesh = NULL;
unsigned char* adjIndices = NULL;
SIZE_T Size = 0;
if(S_OK != mesh->GetIndexBuffer(&indexBufferMesh))
{
throw L"GeometryBuffer: Unable to retrive indexdata for adjacency data generation.";
}
if(S_OK != indexBufferMesh->Map((void**)&adjIndices, &Size))
{
throw L"GeometryBuffer: Unable to map indexdata for adjacency data generation.";
}
this->SetIndexData((uint32)Size/sizeof(uint32), (uint32*)adjIndices, false);
//cleanup
indexBufferMesh->Unmap();
indexBufferMesh->Release();
mesh->Release();
switch (m_PrimitiveTopology)
{
case PrimitiveTopology::LineList:
this->set_PrimitiveTopology(PrimitiveTopology::LineListAdjacency);
break;
case PrimitiveTopology::LineStrip:
this->set_PrimitiveTopology(PrimitiveTopology::LineStripAdjacency);
break;
case PrimitiveTopology::TriangleList:
this->set_PrimitiveTopology(PrimitiveTopology::TriangleListAdjacency);
break;
case PrimitiveTopology::TriangleStrip:
this->set_PrimitiveTopology(PrimitiveTopology::TriangleStripAdjacency);
break;
}
}
CommandExecuteResult::Enumeration GeometryBuffer::ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon)
{
ID3D10Device& device = m_Engine.get_DX10Device();
if (argCommandType == GeometryBuffer::SetIndexBufferCommandType)
{
if (m_Engine.get_EngineStates().LastIndexBufferProvider != this)
{
device.IASetIndexBuffer(m_IndexBuffer, DXGI_FORMAT_R32_UINT, m_IndexOffset);
m_Engine.get_EngineStates().LastIndexBufferProvider = this;
}
}
else if (argCommandType == GeometryBuffer::SetVertexBufferCommandType)
{
if (m_Engine.get_EngineStates().LastVertexBufferProvider != this)
{
device.IASetVertexBuffers(0, 1, &m_VertexBuffer, &m_VertexElementSize, &m_VertexOffset);
device.IASetPrimitiveTopology((D3D10_PRIMITIVE_TOPOLOGY)m_PrimitiveTopology);
m_Engine.get_EngineStates().LastVertexBufferProvider = this;
}
}
else if (argCommandType == GeometryBuffer::DrawGeometryCommandType)
{
if (m_Engine.get_EngineStates().LastShader == NULL)
std::wcout << std::red << "GeometryBuffer could not render without a valid shader." << std::white << std::endl;
else if (m_Engine.get_EngineStates().LastVertexBufferProvider == NULL)
std::wcout << std::red << "GeometryBuffer could not render without a valid vertex buffer." << std::white << std::endl;
else
this->Render(*m_Engine.get_EngineStates().LastShader);
}
return CommandExecuteResult::None;
}
void GeometryBuffer::Render(IShader& argShader)
{
if (m_VertexElements.empty())
return;
uint32 currentPass = argShader.get_CurrentRenderPass();
ID3D10EffectPass* currentEffectPass = argShader.get_DX10Technique()->GetPassByIndex(currentPass);
ID3D10InputLayout* vertexLayout = NULL;
tk_VertexLayoutsPerShaderAndPass::iterator found = m_VertexLayoutsPerShaderAndPass.find(&argShader);
if (found != m_VertexLayoutsPerShaderAndPass.end() && found->second.size() > currentPass)
{
vertexLayout = found->second[currentPass];
}
else
{
D3D10_PASS_DESC passDesc;
currentEffectPass->GetDesc(&passDesc);
if (S_OK == m_Engine.get_DX10Device().CreateInputLayout(m_InputElementDesc, m_VertexElements.size(), passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &vertexLayout ))
{
if (found == m_VertexLayoutsPerShaderAndPass.end())
{
std::vector<ID3D10InputLayout*> val;
val.push_back(vertexLayout);
m_VertexLayoutsPerShaderAndPass[&argShader] = val;
}
else
found->second.push_back(vertexLayout);
}
}
if (vertexLayout != NULL)
{
currentEffectPass->Apply(0);
m_Engine.get_DX10Device().IASetInputLayout(vertexLayout);
if (m_IndexBuffer == NULL)
m_Engine.get_DX10Device().Draw(m_VertexCount, 0);
else
m_Engine.get_DX10Device().DrawIndexed(m_UsableIndexCount, m_IndexOffset, 0);
}
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include "IEngine.h"
#include "IGeometryBuffer.h"
#include "IShader.h"
#include "../Commands/CommandUserBase.h"
class GeometryBuffer
: public IGeometryBuffer
, public CommandUserBase
{
protected:
static const unsigned char SetIndexBufferCommandType = 0;
static const unsigned char SetVertexBufferCommandType = 1;
static const unsigned char DrawGeometryCommandType = 2;
public:
GeometryBuffer(IEngine& argEngine);
virtual ~GeometryBuffer();
virtual void Uninitialize();
virtual void SetVertexData(uint32 argVertexCount, uint32 argVertexElementSize, void* a_VertexData_, const std::vector<VertexElement>& argVertexElements, bool argNeedsDynamicAccess);
virtual void DeleteVertexData();
virtual void SetIndexData(uint32 argIndexCount, uint32* aui_IndexData_, bool argNeedsDynamicAccess);
virtual void DeleteIndexData();
virtual void* MapVertexBuffer(DataAccessMode::Enumeration argAccessMode);
virtual void UnmapVertexBuffer();
virtual uint32* MapIndexBuffer(DataAccessMode::Enumeration argAccessMode);
virtual void UnmapIndexBuffer();
virtual uint32 get_IndexBufferOffset() const { return m_IndexOffset; }
virtual void set_IndexBufferOffset(uint32 argValue) { m_IndexOffset = argValue; }
virtual uint32 get_IndexBufferUsableLength() const { return m_UsableIndexCount; }
virtual void set_IndexBufferUsableLength(uint32 argValue) { m_UsableIndexCount = argValue; }
virtual uint32 get_IndexBufferLength() const { return m_IndexCount; }
virtual uint32 get_VertexBufferLength() const { return m_VertexCount; }
virtual PrimitiveTopology::Enumeration get_PrimitiveTopology() const { return m_PrimitiveTopology; }
virtual void set_PrimitiveTopology(PrimitiveTopology::Enumeration argValue) { m_PrimitiveTopology = argValue; }
virtual void ConvertToAdjacency();
// ICommandUser
virtual CommandExecuteResult::Enumeration ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon);
virtual string8 get_UserName() const { return "GeometryBuffer"; }
protected:
void Render(IShader& argShader);
protected:
IEngine& m_Engine;
uint32 m_IndexCount;
uint32 m_IndexOffset;
uint32 m_UsableIndexCount;
ID3D10Buffer* m_IndexBuffer;
uint32* m_RawIndexData;
uint32 m_VertexOffset;
uint32 m_VertexCount;
uint32 m_VertexElementSize;
ID3D10Buffer* m_VertexBuffer;
void* m_RawVertexData_;
bool m_VertexDataMapped;
bool m_IndexDataMapped;
PrimitiveTopology::Enumeration m_PrimitiveTopology;
std::vector<VertexElement> m_VertexElements;
D3D10_INPUT_ELEMENT_DESC* m_InputElementDesc;
typedef std::map<IShader*, std::vector<ID3D10InputLayout*> > tk_VertexLayoutsPerShaderAndPass;
tk_VertexLayoutsPerShaderAndPass m_VertexLayoutsPerShaderAndPass;
};

View File

@@ -0,0 +1,186 @@
#include "stdafx.h"
#include <algorithm>
#include "../Commands/CommandBuffer.h"
#include "IResourceFactory.h"
#include "RenderCommandNode.h"
RenderCommandNode::RenderCommandNode(IEngine& argEngine)
: m_Engine(argEngine)
, m_Dirty(true)
, m_ParentNode(NULL)
, m_CommandBuffer(NULL)
{
}
RenderCommandNode::~RenderCommandNode()
{
if (m_ParentNode != NULL)
m_ParentNode->RemoveCommandUser(*this);
while (!m_CommandUsers.empty())
RemoveCommandUser(**m_CommandUsers.begin());
delete m_CommandBuffer;
m_Engine.get_ResourceFactory().DeleteRenderCommandNode(*const_cast<RenderCommandNode*>(this), true);
}
const std::vector<ICommandUser*> RenderCommandNode::get_CommandUsers() const
{
return m_CommandUsers;
}
void RenderCommandNode::set_Parent(IRenderCommandNode* ar_Node_)
{
m_ParentNode = ar_Node_;
}
IRenderCommandNode* RenderCommandNode::get_Parent() const
{
return m_ParentNode;
}
bool RenderCommandNode::IsDirty() const
{
return m_Dirty;
}
void RenderCommandNode::MarkDirty()
{
m_Dirty = true;
if (m_ParentNode != NULL)
m_ParentNode->MarkDirty();
}
void RenderCommandNode::AddCommandUser(ICommandUser& argCommandUser)
{
argCommandUser.AssignToRenderCommandNode(*const_cast<RenderCommandNode*>(this));
m_CommandUsers.push_back(&argCommandUser);
IRenderCommandNode* node = dynamic_cast<IRenderCommandNode*>(&argCommandUser);
if (node != NULL)
node->set_Parent(this);
this->MarkDirty();
}
void RenderCommandNode::RemoveCommandUser(ICommandUser& argCommandUser)
{
std::vector<ICommandUser*>::iterator found = std::find(m_CommandUsers.begin(), m_CommandUsers.end(), &argCommandUser);
if (found != m_CommandUsers.end())
{
IRenderCommandNode* node = dynamic_cast<IRenderCommandNode*>(&argCommandUser);
if (node != NULL)
node->set_Parent(NULL);
m_CommandUsers.erase(found);
argCommandUser.UnassignFromRenderCommandNode(*const_cast<RenderCommandNode*>(this));
this->MarkDirty();
}
}
void RenderCommandNode::ReplaceCommandUser(ICommandUser& argWhatCommandUser, ICommandUser& argWhithCommandUser)
{
std::vector<ICommandUser*>::iterator found = std::find(m_CommandUsers.begin(), m_CommandUsers.end(), &argWhatCommandUser);
if (found != m_CommandUsers.end())
{
IRenderCommandNode* node = dynamic_cast<IRenderCommandNode*>(&argWhatCommandUser);
if (node != NULL)
node->set_Parent(NULL);
m_CommandUsers.insert(found, &argWhithCommandUser);
m_CommandUsers.erase(found);
argWhatCommandUser.UnassignFromRenderCommandNode(*const_cast<RenderCommandNode*>(this));
argWhithCommandUser.AssignToRenderCommandNode(*const_cast<RenderCommandNode*>(this));
node = dynamic_cast<IRenderCommandNode*>(&argWhithCommandUser);
if (node != NULL)
node->set_Parent(this);
this->MarkDirty();
}
}
void RenderCommandNode::ProcessCommands()
{
if (m_Dirty)
{
this->GenerateDeviceCommands();
m_CommandBuffer->Optimize();
}
m_CommandBuffer->Perform(true);
}
void RenderCommandNode::Update(bool argForceUpdate)
{
// Walk through all updatables and update them.
for (uint32 i = 0; i < m_CommandUsers.size(); ++i)
{
IUpdatable* updatable = dynamic_cast<IUpdatable*>(m_CommandUsers[i]);
if (updatable != NULL && (updatable->get_WantsUpdate() || argForceUpdate))
updatable->Update(argForceUpdate);
}
}
bool RenderCommandNode::get_WantsUpdate() const
{
return true;
}
CommandExecuteResult::Enumeration RenderCommandNode::ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon)
{
return CommandExecuteResult::None;
}
void RenderCommandNode::GenerateDeviceCommands()
{
if (!m_Dirty)
return;
m_Dirty = false;
if (m_CommandBuffer == NULL)
m_CommandBuffer = new CommandBuffer(false, false);
else
m_CommandBuffer->Clear();
for (uint32 i = 0; i < m_CommandUsers.size(); ++i)
{
// Add the commands all commands
const std::vector<Command*>& commands = m_CommandUsers[i]->GetCommands();
for (uint32 k = 0; k < commands.size(); ++k)
m_CommandBuffer->AddCommand(*commands[k]);
// Walk further if we found another RenderCommandNode
IRenderCommandNodeInternal* nodeInternal = dynamic_cast<IRenderCommandNodeInternal*>(m_CommandUsers[i]);
if (nodeInternal != NULL)
{
nodeInternal->GenerateDeviceCommands();
// Merge the buffers
ICommandBuffer* otherBuffer = nodeInternal->get_CommandBuffer();
if (otherBuffer != NULL)
m_CommandBuffer->AddChildCommandBuffer(*otherBuffer);
}
}
}
ICommandBuffer* RenderCommandNode::get_CommandBuffer() const
{
return m_CommandBuffer;
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include "IRenderCommandNode.h"
#include "IUpdatable.h"
#include "IEngine.h"
#include "../Commands/CommandUserBase.h"
class RenderCommandNode
: public IRenderCommandNode
, public IRenderCommandNodeInternal
, public CommandUserBase
, public IUpdatable
{
public:
RenderCommandNode(IEngine& argEngine);
virtual ~RenderCommandNode();
// IRenderCommandNode
virtual const std::vector<ICommandUser*> get_CommandUsers() const;
virtual void set_Parent(IRenderCommandNode* ar_Node_);
virtual IRenderCommandNode* get_Parent() const;
virtual bool IsDirty() const;
virtual void MarkDirty();
virtual void AddCommandUser(ICommandUser& argCommandUser);
virtual void RemoveCommandUser(ICommandUser& argCommandUser);
virtual void ReplaceCommandUser(ICommandUser& argWhatCommandUser, ICommandUser& argWhithCommandUser);
virtual void ProcessCommands();
// IUpdatable
virtual void Update(bool argForceUpdate);
virtual bool get_WantsUpdate() const;
// ICommandUser
virtual CommandExecuteResult::Enumeration ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon);
virtual string8 get_UserName() const { return "RenderCommandNode"; }
protected:
// IRenderCommandNodeInternal
virtual void GenerateDeviceCommands();
virtual ICommandBuffer* get_CommandBuffer() const;
private:
IEngine& m_Engine;
bool m_Dirty;
IRenderCommandNode* m_ParentNode;
std::vector<ICommandUser*> m_CommandUsers;
ICommandBuffer* m_CommandBuffer;
};

View File

@@ -0,0 +1,308 @@
#include "stdafx.h"
#include <algorithm>
#include "RenderTargetBase.h"
RenderTargetBase::RenderTargetBase(IEngine& argEngine)
: m_Engine(argEngine)
, m_ClearDepthStencilBufferDepth(1.0f) // 1.0f is the maximum depth
, m_ClearDepthStencilBufferStencil(0) // clear the full stencil buffer
, m_ClearColorBufferColor(D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)) // good old black opaque
, m_RenderTargetView(NULL)
, m_DepthStencilView(NULL)
, m_DepthStencilTexture(NULL)
, m_RenderingTargets(NULL)
, m_D3D10Viewports(NULL)
, m_MultiSampleCount(1)
, m_MultiSampleQuality(0)
, m_LastBindFlags(BindFlags::Default)
, m_ActiveRenderTargetCount(0)
{
this->set_ViewPort(this->get_ViewPort()); // force d3d10 viewport generation
m_Commands.push_back(new Command(this, CommandFlags::EndChain | CommandFlags::FlushChain, SetRenderTargetCommandType, 0));
memset(m_EmptyShaderResView, 0, sizeof(m_EmptyShaderResView));
}
RenderTargetBase::~RenderTargetBase()
{
this->Uninitialize();
}
void RenderTargetBase::Uninitialize()
{
if (m_Engine.get_EngineStates().LastRenderTarget == this)
m_Engine.get_EngineStates().LastRenderTarget = NULL;
// clear used states
ID3D10Device& device = m_Engine.get_DX10Device();
ID3D10RenderTargetView* activeRenderTargetViews[D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT];
ID3D10DepthStencilView* activeDepthStencilView;
device.OMGetRenderTargets(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT, activeRenderTargetViews, &activeDepthStencilView);
bool clearRenderTargets = false;
if (activeDepthStencilView == m_DepthStencilView && m_DepthStencilView != NULL)
clearRenderTargets = true;
for (uint32 i = 0; i < D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)
{
if (activeRenderTargetViews[i] != NULL)
{
for (uint32 k = 0; k < m_AdditionalRenderingTargets.size() + 1; ++k)
{
if (m_RenderingTargets != NULL && m_RenderingTargets[k] == activeRenderTargetViews[i])
clearRenderTargets = true;
}
activeRenderTargetViews[i]->Release();
}
else
break;
}
if (clearRenderTargets)
device.OMSetRenderTargets(0, NULL, NULL);
if (m_RenderTargetView != NULL)
m_RenderTargetView->Release();
m_RenderTargetView = NULL;
if (m_DepthStencilView != NULL)
m_DepthStencilView->Release();
m_DepthStencilView = NULL;
if (m_DepthStencilTexture != NULL)
m_DepthStencilTexture->Release();
m_DepthStencilTexture = NULL;
this->KillViewPortAndRenderTargetData();
}
void RenderTargetBase::set_ViewPort(const ViewPort& argViewPort)
{
m_ViewPort = argViewPort;
this->KillViewPortAndRenderTargetData();
this->BuildViewPortAndRenderTargetData();
}
void RenderTargetBase::set_HasDepthStencilBuffer(bool argState)
{
if (m_RenderTargetView == NULL)
{
throw L"RenderTarget: Access forbidden until resource has been initialized.";
}
m_Engine.get_DX10Device().ClearState();
if (m_DepthStencilView != NULL)
m_DepthStencilView->Release();
m_DepthStencilView = NULL;
if (m_DepthStencilTexture != NULL)
m_DepthStencilTexture->Release();
m_DepthStencilTexture = NULL;
if (!argState)
return; // no further effort needed here
ID3D10Device& device = m_Engine.get_DX10Device();
D3D10_TEXTURE2D_DESC depthTextureDesc;
D3D10_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthTextureDesc,sizeof(depthTextureDesc));
depthTextureDesc.Width = this->get_RenderTargetWidth();
depthTextureDesc.Height = this->get_RenderTargetHeight();
depthTextureDesc.MipLevels = 1;
depthTextureDesc.ArraySize = 1;
depthTextureDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthTextureDesc.SampleDesc.Count = m_MultiSampleCount == 0 ? 1 : m_MultiSampleCount;
depthTextureDesc.SampleDesc.Quality = m_MultiSampleQuality;
depthTextureDesc.Usage = D3D10_USAGE_DEFAULT;
depthTextureDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthTextureDesc.CPUAccessFlags = 0;
depthTextureDesc.MiscFlags = 0;
if(device.CreateTexture2D(&depthTextureDesc, 0, &m_DepthStencilTexture) != S_OK)
{
throw L"RenderTarget: Unable to create depthstencil texture.";
}
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = depthTextureDesc.Format;
depthStencilViewDesc.ViewDimension = m_MultiSampleCount >= 1 ? D3D10_DSV_DIMENSION_TEXTURE2DMS : D3D10_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
if(device.CreateDepthStencilView(m_DepthStencilTexture, &depthStencilViewDesc, &m_DepthStencilView) != S_OK)
{
if (m_DepthStencilTexture != NULL)
m_DepthStencilTexture->Release();
m_DepthStencilTexture = NULL;
throw L"RenderTarget: Unable to create depth stencil view.";
}
}
void RenderTargetBase::AddAdditionalRenderTarget(IRenderTargetBase& argTarget)
{
this->RemoveAdditionalRenderTarget(argTarget);
if (m_AdditionalRenderingTargets.size() + 1 == D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT)
{
throw L"RenderTarget: Unable to add an additional rendering target, list is full.";
}
if (argTarget.get_DX10RenderTargetView() == NULL)
{
throw L"RenderingResource: Unable to add an additional rendering target, new target is not initialized.";
}
m_AdditionalRenderingTargets.push_back(&argTarget);
this->KillViewPortAndRenderTargetData();
this->BuildViewPortAndRenderTargetData();
}
void RenderTargetBase::RemoveAdditionalRenderTarget(IRenderTargetBase& argTarget)
{
std::vector<IRenderTargetBase*>::iterator found = std::find(m_AdditionalRenderingTargets.begin(), m_AdditionalRenderingTargets.end(), &argTarget);
if (found != m_AdditionalRenderingTargets.end())
m_AdditionalRenderingTargets.erase(found);
this->KillViewPortAndRenderTargetData();
this->BuildViewPortAndRenderTargetData();
}
void RenderTargetBase::BuildViewPortAndRenderTargetData()
{
// Rebuild rendertargets
if (m_RenderingTargets == NULL)
{
if ((m_LastBindFlags & BindFlags::BindAllTextures) == BindFlags::BindAllTextures)
{
m_ActiveRenderTargetCount = 1 + m_AdditionalRenderingTargets.size();
m_RenderingTargets = new ID3D10RenderTargetView*[m_ActiveRenderTargetCount];
m_RenderingTargets[0] = m_RenderTargetView;
for (uint32 i = 0; i < m_AdditionalRenderingTargets.size(); ++i)
m_RenderingTargets[i + 1] = m_AdditionalRenderingTargets[i]->get_DX10RenderTargetView();
}
else if (m_LastBindFlags & BindFlags::BindAllTextures)
{
m_ActiveRenderTargetCount = 0;
if (m_LastBindFlags & BindFlags::BindBaseTexture) ++m_ActiveRenderTargetCount;
if (m_LastBindFlags & BindFlags::BindAdditionalTexture0) ++m_ActiveRenderTargetCount;
if (m_LastBindFlags & BindFlags::BindAdditionalTexture1) ++m_ActiveRenderTargetCount;
if (m_LastBindFlags & BindFlags::BindAdditionalTexture2) ++m_ActiveRenderTargetCount;
if (m_LastBindFlags & BindFlags::BindAdditionalTexture3) ++m_ActiveRenderTargetCount;
m_RenderingTargets = new ID3D10RenderTargetView*[m_ActiveRenderTargetCount];
int bufferIndex = 0;
if (m_LastBindFlags & BindFlags::BindBaseTexture) m_RenderingTargets[bufferIndex++] = m_RenderTargetView;
if (m_LastBindFlags & BindFlags::BindAdditionalTexture0) m_RenderingTargets[bufferIndex++] = m_AdditionalRenderingTargets[0]->get_DX10RenderTargetView();
if (m_LastBindFlags & BindFlags::BindAdditionalTexture1) m_RenderingTargets[bufferIndex++] = m_AdditionalRenderingTargets[1]->get_DX10RenderTargetView();
if (m_LastBindFlags & BindFlags::BindAdditionalTexture2) m_RenderingTargets[bufferIndex++] = m_AdditionalRenderingTargets[2]->get_DX10RenderTargetView();
if (m_LastBindFlags & BindFlags::BindAdditionalTexture3) m_RenderingTargets[bufferIndex++] = m_AdditionalRenderingTargets[3]->get_DX10RenderTargetView();
}
else
throw L"No render target bindable.";
}
// Rebuild viewports
if (m_D3D10Viewports == NULL)
{
m_D3D10Viewports = new D3D10_VIEWPORT[m_ActiveRenderTargetCount];
m_D3D10Viewports[0].Height = m_ViewPort.m_Height;
m_D3D10Viewports[0].Width = m_ViewPort.m_Width;
m_D3D10Viewports[0].MinDepth = m_ViewPort.m_MinDepth;
m_D3D10Viewports[0].MaxDepth = m_ViewPort.m_MaxDepth;
m_D3D10Viewports[0].TopLeftX = m_ViewPort.m_TopLeftX;
m_D3D10Viewports[0].TopLeftY = m_ViewPort.m_TopLeftY;
for (uint32 i = 0; i < m_ActiveRenderTargetCount - 1; ++i)
{
ViewPort viewPort = m_AdditionalRenderingTargets[i]->get_ViewPort();
m_D3D10Viewports[i + 1].Height = viewPort.m_Height;
m_D3D10Viewports[i + 1].Width = viewPort.m_Width;
m_D3D10Viewports[i + 1].MinDepth = viewPort.m_MinDepth;
m_D3D10Viewports[i + 1].MaxDepth = viewPort.m_MaxDepth;
m_D3D10Viewports[i + 1].TopLeftX = viewPort.m_TopLeftX;
m_D3D10Viewports[i + 1].TopLeftY = viewPort.m_TopLeftY;
}
}
}
void RenderTargetBase::KillViewPortAndRenderTargetData()
{
if (m_RenderingTargets != NULL)
delete [] m_RenderingTargets;
m_RenderingTargets = NULL;
if (m_D3D10Viewports != NULL)
delete [] m_D3D10Viewports;
m_D3D10Viewports = NULL;
m_ActiveRenderTargetCount = 0;
}
void RenderTargetBase::Bind(DWORD argBindFlags)
{
if ((argBindFlags & ~ BindFlags::ClearAll) != (m_LastBindFlags & ~ BindFlags::ClearAll))
{
m_LastBindFlags = argBindFlags;
this->KillViewPortAndRenderTargetData();
this->BuildViewPortAndRenderTargetData();
}
m_LastBindFlags = argBindFlags;
ID3D10Device& device = m_Engine.get_DX10Device();
this->Clear(argBindFlags);
device.PSSetShaderResources(0, 128, m_EmptyShaderResView);
device.OMSetRenderTargets(m_ActiveRenderTargetCount, m_RenderingTargets, m_DepthStencilView);
device.RSSetViewports(m_ActiveRenderTargetCount, m_D3D10Viewports);
m_Engine.get_EngineStates().LastRenderTarget = this;
}
void RenderTargetBase::UnbindAllRenderTargets()
{
m_Engine.get_DX10Device().OMSetRenderTargets(0, NULL, NULL);
m_Engine.get_DX10Device().RSSetViewports(0, NULL);
m_Engine.get_EngineStates().LastRenderTarget = NULL;
}
CommandExecuteResult::Enumeration RenderTargetBase::ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon)
{
if (argCommandType == RenderTargetBase::SetRenderTargetCommandType)
{
if (m_Engine.get_EngineStates().LastRenderTarget != this)
{
this->UnbindAllRenderTargets();
this->Bind();
}
else
this->Clear();
}
return CommandExecuteResult::None;
}
void RenderTargetBase::Clear(DWORD argBindFlags)
{
ID3D10Device& device = m_Engine.get_DX10Device();
if (argBindFlags & BindFlags::ClearColor)
device.ClearRenderTargetView(m_RenderTargetView, (FLOAT*)m_ClearColorBufferColor);
if ((argBindFlags & BindFlags::ClearDepthStencil) && m_DepthStencilView != NULL)
device.ClearDepthStencilView(m_DepthStencilView, 0x01L | 0x02L, m_ClearDepthStencilBufferDepth, m_ClearDepthStencilBufferStencil);
}

View File

@@ -0,0 +1,81 @@
#pragma once
#include "IEngine.h"
#include "IRenderTargetBase.h"
#include "../Commands/CommandUserBase.h"
class RenderTargetBase
: public IRenderTargetBase
, public CommandUserBase
{
protected:
static const unsigned char SetRenderTargetCommandType = 0;
public:
RenderTargetBase(IEngine& argEngine);
virtual ~RenderTargetBase();
virtual void set_ClearDepth(float argValue) { m_ClearDepthStencilBufferDepth = argValue; }
virtual float get_ClearDepth() const { return m_ClearDepthStencilBufferDepth; }
virtual void set_ClearStencil(unsigned char argValue) { m_ClearDepthStencilBufferStencil = argValue; }
virtual unsigned char get_ClearStencil() const { return m_ClearDepthStencilBufferStencil; }
virtual void set_ClearColor(const D3DXCOLOR& argValue) { m_ClearColorBufferColor = argValue; }
virtual D3DXCOLOR get_ClearColor() const { return m_ClearColorBufferColor; }
virtual void set_ViewPort(const ViewPort& argValue);
virtual ViewPort get_ViewPort() const { return m_ViewPort; }
virtual uint32 get_RenderTargetWidth() const = 0;
virtual uint32 get_RenderTargetHeight() const = 0;
virtual DataFormat::Enumeration get_RenderTargetFormat() const = 0;
virtual void set_HasDepthStencilBuffer(bool argState);
virtual bool get_HasDepthStencilBuffer() const { return m_DepthStencilView != NULL; }
virtual void AddAdditionalRenderTarget(IRenderTargetBase& argTarget);
virtual void RemoveAdditionalRenderTarget(IRenderTargetBase& argTarget);
virtual ID3D10RenderTargetView* get_DX10RenderTargetView() const { return m_RenderTargetView; }
virtual ID3D10DepthStencilView* get_DX10DepthStencilView() const { return m_DepthStencilView; }
virtual void Bind(DWORD argBindFlags = BindFlags::Default);
virtual void UnbindAllRenderTargets();
// ICommandUser
virtual CommandExecuteResult::Enumeration ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon);
protected:
void Clear(DWORD argBindFlags = BindFlags::Default);
virtual void Uninitialize();
void BuildViewPortAndRenderTargetData();
void KillViewPortAndRenderTargetData();
protected:
IEngine& m_Engine;
float m_ClearDepthStencilBufferDepth;
unsigned char m_ClearDepthStencilBufferStencil;
D3DXCOLOR m_ClearColorBufferColor;
ViewPort m_ViewPort;
ID3D10RenderTargetView* m_RenderTargetView;
ID3D10DepthStencilView* m_DepthStencilView;
ID3D10Texture2D* m_DepthStencilTexture;
uint32 m_MultiSampleCount;
uint32 m_MultiSampleQuality;
std::vector<IRenderTargetBase*> m_AdditionalRenderingTargets;
ID3D10RenderTargetView** m_RenderingTargets;
D3D10_VIEWPORT* m_D3D10Viewports;
uint32 m_ActiveRenderTargetCount;
DWORD m_LastBindFlags;
ID3D10ShaderResourceView* m_EmptyShaderResView[128];
};

View File

@@ -0,0 +1,105 @@
#include "stdafx.h"
#include "ITexture.h"
#include "IEngine.h"
#include "IResourcefactory.h"
#include "RenderTargetTexture.h"
RenderTargetTexture::RenderTargetTexture(IEngine& argEngine)
: RenderTargetBase(argEngine)
, m_Texture(NULL)
{
}
RenderTargetTexture::~RenderTargetTexture()
{
delete m_Texture;
m_Engine.get_ResourceFactory().DeleteRenderTargetTexture(*const_cast<RenderTargetTexture*>(this), true);
}
void RenderTargetTexture::SetTextureParameters(uint32 argWidth, uint32 argHeight, DataFormat::Enumeration argFormat, uint32 argMultiSampleCount, uint32 argMultiSampleQuality)
{
if (argWidth == this->get_RenderTargetWidth() &&
argHeight == this->get_RenderTargetHeight() &&
argFormat == this->get_RenderTargetFormat() &&
m_MultiSampleCount == argMultiSampleCount &&
m_MultiSampleQuality == argMultiSampleQuality)
{
return;
}
RenderTargetBase::Uninitialize();
// get missing information
if (argMultiSampleCount == 0)
argMultiSampleCount = 1;
if (argWidth == 0)
argWidth = 1;
if (argHeight == 0)
argHeight = 1;
if (argFormat == DataFormat::Unknown)
argFormat = DataFormat::R8G8B8A8_UnsignedNormalized;
if (m_Texture == NULL)
m_Texture = &m_Engine.get_ResourceFactory().CreateOrFindTexture();
m_MultiSampleCount = argMultiSampleCount;
m_MultiSampleQuality = argMultiSampleQuality;
m_Texture->CreateRenderTargetTexture(argWidth, argHeight, argFormat, argMultiSampleCount, argMultiSampleQuality);
HRESULT lh_Result = m_Engine.get_DX10Device().CreateRenderTargetView(m_Texture->get_Texture2D(), NULL, &m_RenderTargetView);
if(FAILED(lh_Result))
throw L"RenderTargetTexture: Unable to retrieve rendertarget view.";
ViewPort viewPort;
viewPort.m_Width = argWidth;
viewPort.m_Height = argHeight;
this->set_ViewPort(viewPort);
}
void RenderTargetTexture::Resize(int argWidth, int argHeight)
{
if (m_Texture == NULL)
throw L"SwapChainResource: Access forbidden until resource has been initialized.";
if (this->get_RenderTargetWidth() == argWidth
|| this->get_RenderTargetHeight() == argHeight)
return;
bool lb_DepthStencilBuffer = this->get_HasDepthStencilBuffer();
this->SetTextureParameters(argWidth, argHeight, this->get_RenderTargetFormat(), m_MultiSampleCount, m_MultiSampleQuality);
this->set_HasDepthStencilBuffer(lb_DepthStencilBuffer);
}
void RenderTargetTexture::Uninitialize()
{
if (m_Texture != NULL)
m_Engine.get_ResourceFactory().DeleteTexture(*m_Texture);
RenderTargetBase::Uninitialize();
}
uint32 RenderTargetTexture::get_RenderTargetWidth() const
{
return m_Texture == NULL ? 0 : m_Texture->get_TextureWidth();
}
uint32 RenderTargetTexture::get_RenderTargetHeight() const
{
return m_Texture == NULL ? 0 : m_Texture->get_TextureHeight();
}
DataFormat::Enumeration RenderTargetTexture::get_RenderTargetFormat() const
{
return m_Texture == NULL ? DataFormat::Unknown : m_Texture->get_TextureFormat();
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include "IRenderTargetTexture.h"
#include "../RenderTargetBase.h"
struct ITexture;
class RenderTargetTexture
: public RenderTargetBase
, public IRenderTargetTexture
{
public:
RenderTargetTexture(IEngine& argEngine);
virtual ~RenderTargetTexture();
virtual void SetTextureParameters(uint32 argWidth, uint32 argHeight, DataFormat::Enumeration argFormat, uint32 argMultiSampleCount = 1, uint32 argMultiSampleQuality = 0);
virtual void Resize(int argWidth, int argHeight);
virtual ITexture& get_TextureResource() const { return *m_Texture; }
virtual IRenderTargetBase& get_Base() { return *dynamic_cast<IRenderTargetBase*>(this); }
virtual void Uninitialize();
// IRenderTargetBase
virtual uint32 get_RenderTargetWidth() const;
virtual uint32 get_RenderTargetHeight() const;
virtual DataFormat::Enumeration get_RenderTargetFormat() const;
protected:
//ICommandUser
virtual string8 get_UserName() const { return "RenderTargetTexture"; }
private:
ITexture* m_Texture;
};

View File

@@ -0,0 +1,187 @@
#include "stdafx.h"
#include "SwapChain.h"
#include "IResourceFactory.h"
#include <exception>
SwapChain::SwapChain(IEngine& argEngine)
: RenderTargetBase(argEngine)
, m_SwapChain(NULL)
, m_VSync(true)
{
}
SwapChain::~SwapChain()
{
this->Uninitialize();
m_Engine.get_ResourceFactory().DeleteSwapChain(*const_cast<SwapChain*>(this), true);
}
void SwapChain::SetWindowParameters(HWND argWindowHandle, uint32 argWidth, uint32 argHeight, uint32 argRefreshRate, DataFormat::Enumeration argFormat, uint32 argMultiSampleCount, uint32 argMultiSampleQuality)
{
if (m_SwapChain != NULL)
this->Uninitialize();
// get missing information
if (argMultiSampleCount == 0)
argMultiSampleCount = 1;
if (argWidth == 0 || argHeight == 0)
{
RECT windowRect;
::GetClientRect(argWindowHandle, &windowRect);
if (argWidth == 0)
argWidth = windowRect.right - windowRect.left;
if (argHeight == 0)
argHeight = windowRect.bottom - windowRect.top;
}
if (argRefreshRate == 0)
argRefreshRate = 60; // 60Hz
if (argFormat == DataFormat::Unknown)
argFormat = DataFormat::R8G8B8A8_UnsignedNormalized; // RGBA(X) 8Bit per channel -> 32Bit
// build the description
ZeroMemory(&m_SwapChainDescription, sizeof(m_SwapChainDescription));
m_SwapChainDescription.BufferCount = 1;
m_SwapChainDescription.BufferDesc.Width = argWidth;
m_SwapChainDescription.BufferDesc.Height = argHeight;
m_SwapChainDescription.BufferDesc.Format = (DXGI_FORMAT)argFormat;
m_SwapChainDescription.BufferDesc.RefreshRate.Numerator = argRefreshRate;
m_SwapChainDescription.BufferDesc.RefreshRate.Denominator = 1;
m_SwapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
m_SwapChainDescription.OutputWindow = argWindowHandle;
m_MultiSampleCount = m_SwapChainDescription.SampleDesc.Count = argMultiSampleCount;
m_MultiSampleQuality = m_SwapChainDescription.SampleDesc.Quality = argMultiSampleQuality;
m_SwapChainDescription.Windowed = TRUE;
if (S_OK != m_Engine.get_DXGIFactory().CreateSwapChain(&m_Engine.get_DX10Device(), &m_SwapChainDescription, &m_SwapChain))
{
throw L"SwapChain: Unable to create swapchain.";
}
this->RetriveData();
ViewPort vp;
vp.m_Width = argWidth;
vp.m_Height = argHeight;
this->set_ViewPort(vp);
}
void SwapChain::Uninitialize()
{
RenderTargetBase::Uninitialize();
/*if (m_SwapChain != NULL)
m_SwapChain->Release();*/
m_SwapChain = NULL;
}
void SwapChain::Resize(int argWidth, int argHeight)
{
if (m_SwapChain == NULL || m_RenderTargetView == NULL)
{
throw L"SwapChain: Access forbidden until resource has been initialized.";
}
if (argWidth == 0 || argHeight == 0)
{
RECT windowRect;
::GetClientRect(m_SwapChainDescription.OutputWindow, &windowRect);
if (argWidth == 0)
argWidth = windowRect.right - windowRect.left;
if (argHeight == 0)
argHeight = windowRect.bottom - windowRect.top;
}
if (m_SwapChainDescription.BufferDesc.Width == argWidth ||
m_SwapChainDescription.BufferDesc.Height == argHeight)
return;
bool depthStencilBuffer = this->get_HasDepthStencilBuffer();
m_Engine.get_DX10Device().ClearState();
this->set_HasDepthStencilBuffer(false);
if (m_RenderTargetView != NULL)
m_RenderTargetView->Release();
m_RenderTargetView = NULL;
m_SwapChain->ResizeBuffers(m_SwapChainDescription.BufferCount, argWidth, argHeight, m_SwapChainDescription.BufferDesc.Format, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
m_SwapChainDescription.BufferDesc.Width = argWidth;
m_SwapChainDescription.BufferDesc.Height = argHeight;
this->RetriveData();
this->set_HasDepthStencilBuffer(depthStencilBuffer);
ViewPort vp = this->get_ViewPort();
vp.m_Width = argWidth;
vp.m_Height = argHeight;
this->set_ViewPort(vp);
}
void SwapChain::set_Fullscreen(bool argState)
{
if (m_SwapChainDescription.Windowed == FALSE && argState)
{
throw L"SwapChain: Fullscreen mode already set.";
}
if (m_SwapChainDescription.Windowed == TRUE && !argState)
{
throw L"SwapChain: Windowed mode already set.";
}
m_SwapChainDescription.Windowed = argState ? FALSE : TRUE;
bool depthStencilBuffer = this->get_HasDepthStencilBuffer();
m_Engine.get_DX10Device().ClearState();
this->set_HasDepthStencilBuffer(false);
if (m_RenderTargetView != NULL)
m_RenderTargetView->Release();
m_RenderTargetView = NULL;
m_SwapChain->SetFullscreenState(m_SwapChainDescription.Windowed == FALSE, NULL);
this->RetriveData();
this->set_HasDepthStencilBuffer(depthStencilBuffer);
}
void SwapChain::Present()
{
if (m_SwapChain == NULL || m_RenderTargetView == NULL)
{
throw L"SwapChain: Access forbidden until resource has been initialized.";
}
m_SwapChain->Present(m_VSync ? 1 : 0, 0); // VSync, Flags
}
void SwapChain::RetriveData()
{
if (m_SwapChain == NULL || m_RenderTargetView != NULL)
{
throw L"SwapChain: Access forbidden until resource has been initialized.";
}
ID3D10Texture2D* backBuffer;
HRESULT result = m_SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (void**)&backBuffer);
if(FAILED(result))
{
throw L"SwapChain: Unable to retrive backbuffer.";
}
result = m_Engine.get_DX10Device().CreateRenderTargetView(backBuffer, NULL, &m_RenderTargetView);
backBuffer->Release();
if(FAILED(result))
{
throw L"SwapChain: Unable to retrive rendertarget view.";
}
}

View File

@@ -0,0 +1,44 @@
#pragma once
#include "IEngine.h"
#include "ISwapChain.h"
#include "../RenderTargetBase.h"
class SwapChain
: public RenderTargetBase
, public ISwapChain
{
public:
SwapChain(IEngine& argEngine);
virtual ~SwapChain();
virtual void SetWindowParameters(HWND argWindowHandle, uint32 argWidth = 0, uint32 argHeight = 0, uint32 argRefreshRate = 0, DataFormat::Enumeration argFormat = DataFormat::Unknown, uint32 argMultiSampleCount = 0, uint32 argMultiSampleQuality = 0);
virtual void Resize(int argWidth, int argHeight);
virtual void Present();
virtual bool get_Fullscreen() const { return !m_SwapChainDescription.Windowed; }
virtual void set_Fullscreen(bool argState);
virtual bool get_VSync() const { return m_VSync; }
virtual void set_VSync(bool argState) {m_VSync = argState; }
virtual IRenderTargetBase& get_Base() { return *dynamic_cast<IRenderTargetBase*>(this); }
// IRenderTargetBase
virtual uint32 get_RenderTargetWidth() const { return m_SwapChainDescription.BufferDesc.Width; }
virtual uint32 get_RenderTargetHeight() const { return m_SwapChainDescription.BufferDesc.Height; }
virtual DataFormat::Enumeration get_RenderTargetFormat() const { return (DataFormat::Enumeration)m_SwapChainDescription.BufferDesc.Format; }
protected:
//ICommandUser
virtual string8 get_UserName() const { return "SwapChain"; }
protected:
void RetriveData();
virtual void Uninitialize();
private:
DXGI_SWAP_CHAIN_DESC m_SwapChainDescription;
IDXGISwapChain* m_SwapChain;
bool m_VSync;
};

View File

@@ -0,0 +1,61 @@
#include "stdafx.h"
#include "InternalShader.h"
#include "IResourceFactory.h"
InternalShader::InternalShader(const IEngine& argEngine)
: m_Engine(argEngine)
, m_Effect(NULL)
{
}
InternalShader::~InternalShader()
{
m_Engine.get_ResourceFactory().DeleteInternalShader(*const_cast<InternalShader*>(this), true);
}
void InternalShader::LoadFromFile(const string16& argFileName)
{
if (m_Effect != NULL)
this->Uninitialize();
HRESULT result = E_FAIL;
IFile* shaderFile = m_Engine.get_FileSystem().Open(argFileName);
if (shaderFile == NULL)
throw L"Unable to open shader file.";
ID3D10Blob* error = NULL;
result = ::D3DX10CreateEffectFromMemory(shaderFile->get_Buffer(), shaderFile->get_BufferLength(), std::to_string8(argFileName).c_str(), NULL, NULL, "fx_4_0",
#ifdef _DEBUG
D3D10_SHADER_ENABLE_STRICTNESS | D3D10_SHADER_DEBUG,
#else
D3D10_SHADER_ENABLE_STRICTNESS,
#endif
0, &m_Engine.get_DX10Device(), NULL, NULL, &m_Effect, &error, NULL);
if (result != S_OK)
std::cout << std::red << "Unable to load shader file from memory. " << std::white << std::endl;
if (error != NULL)
{
string8 errorText = static_cast<char*>(error->GetBufferPointer());
std::cout << std::red << errorText << std::white << std::endl;
::OutputDebugString(errorText.c_str());
throw errorText;
}
if (error != NULL)
error->Release();
delete shaderFile;
}
void InternalShader::Uninitialize()
{
if (m_Effect != NULL)
m_Effect->Release();
m_Effect = NULL;
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "IEngine.h"
#include "IShader.h"
#include "IFileSystem.h"
class InternalShader
: public IInternalShader
{
public:
InternalShader(const IEngine& argEngine);
virtual ~InternalShader();
virtual void Uninitialize();
virtual void LoadFromFile(const string16& argValue);
virtual ID3D10Effect* get_DX10Effect() const { return m_Effect; }
protected:
const IEngine& m_Engine;
ID3D10Effect* m_Effect;
};

View File

@@ -0,0 +1,140 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "Shader.h"
Shader::Shader(IEngine& argEngine)
: m_Engine(argEngine)
, m_Technique(NULL)
, m_InternalShader(NULL)
, m_CurrentPass(-1)
, m_Priority(0)
{
m_Commands.push_back(new Command(this, CommandFlags::StartChain | CommandFlags::SubChainStart, ApplyFirstPassCommandType, 2, m_Priority));
m_Commands.push_back(new Command(this, CommandFlags::SubChainEnd, NextPassCommandType, -1, m_Priority));
}
Shader::~Shader()
{
this->Uninitialize();
m_Engine.get_ResourceFactory().DeleteShader(*const_cast<Shader*>(this), true);
}
void Shader::set_TechniqueName(const string8& argValue)
{
m_TechniqueName = argValue;
if (this->get_InternalShader() == NULL)
return;
if (m_TechniqueName.empty())
{
m_Technique = this->get_InternalShader()->get_DX10Effect()->GetTechniqueByIndex(0);
if (m_Technique != NULL)
{
D3D10_TECHNIQUE_DESC desc;
m_Technique->GetDesc(&desc);
m_TechniqueName = desc.Name;
}
}
m_Technique = this->get_InternalShader()->get_DX10Effect()->GetTechniqueByName(m_TechniqueName.c_str());
if (m_Technique == NULL)
{
throw L"Shader: Technique not found.";
}
m_Technique->GetDesc(&m_TechDesc);
}
void Shader::LoadFromFile(const string16& argValue)
{
this->CreateOrFindShader(argValue);
this->set_TechniqueName(m_TechniqueName);
}
void Shader::Uninitialize()
{
if (m_Engine.get_EngineStates().LastShader == this)
m_Engine.get_EngineStates().LastShader = NULL;
m_Technique = NULL;
m_CurrentPass = -1;
m_TechniqueName = "";
}
bool Shader::TryApplyNextPass()
{
if (m_InternalShader == NULL || m_Technique == NULL)
throw L"Shader: Access forbidden until resource has been initialized.";
if (m_CurrentPass >= m_TechDesc.Passes)
m_CurrentPass = m_TechDesc.Passes;
else
m_CurrentPass++;
if (m_CurrentPass >= m_TechDesc.Passes)
return false;
return true;
}
void Shader::ApplyFirstPass()
{
if (m_InternalShader == NULL || m_Technique == NULL)
throw L"Shader: Access forbidden until resource has been initialized.";
m_CurrentPass = 0;
m_Engine.get_EngineStates().LastShader = this;
}
void Shader::set_Priority(unsigned char argValue)
{
m_Priority = argValue;
for (uint32 i = 0; i < m_Commands.size(); ++i)
if (m_Commands[i]->Type == ApplyFirstPassCommandType)
m_Commands[i]->SubPriority = m_Priority;
}
unsigned char Shader::get_Priority() const
{
return m_Priority;
}
IInternalShader* Shader::CreateOrFindShader(const string16& argFileName)
{
IResourceFactory& resFac = m_Engine.get_ResourceFactory();
m_InternalShader = &resFac.CreateOrFindInternalShader(std::to_string8(argFileName));
if (m_InternalShader != NULL)
{
if (resFac.HasCreatedResource())
m_InternalShader->LoadFromFile(argFileName);
return m_InternalShader;
}
throw L"Shader: Unable to create ShaderInternal for specified Shader.";
}
CommandExecuteResult::Enumeration Shader::ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon)
{
if (argCommandType == Shader::ApplyFirstPassCommandType)
{
this->ApplyFirstPass();
}
else if (argCommandType == Shader::NextPassCommandType)
{
if (this->TryApplyNextPass())
return CommandExecuteResult::RetrySubChainSkipHead;
}
return CommandExecuteResult::None;
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include "IFileSystem.h"
#include "IEngine.h"
#include "IShader.h"
#include "../Commands/CommandUserBase.h"
class Shader
: public IShader
, public CommandUserBase
{
protected:
static const unsigned char ApplyFirstPassCommandType = 0;
static const unsigned char NextPassCommandType = 1;
public:
Shader(IEngine& argEngine);
virtual ~Shader();
virtual void LoadFromFile(const string16& argValue);
virtual string8 get_TechniqueName() const { return m_TechniqueName; }
virtual void set_TechniqueName(const string8& argValue);
virtual ID3D10EffectTechnique* get_DX10Technique() const { return m_Technique; }
virtual IInternalShader* get_InternalShader() const { return m_InternalShader; }
virtual bool TryApplyNextPass();
virtual void ApplyFirstPass();
virtual uint32 get_CurrentRenderPass() const { return m_CurrentPass; }
virtual void set_Priority(unsigned char argValue);
virtual unsigned char get_Priority() const;
protected:
virtual void Uninitialize();
IInternalShader* CreateOrFindShader(const string16& argFileName);
// ICommandUser
virtual CommandExecuteResult::Enumeration ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon);
virtual string8 get_UserName() const { return "Shader"; }
private:
IEngine& m_Engine;
string8 m_TechniqueName;
uint32 m_CurrentPass;
ID3D10EffectTechnique* m_Technique;
IInternalShader* m_InternalShader;
D3D10_TECHNIQUE_DESC m_TechDesc;
unsigned char m_Priority;
};

View File

@@ -0,0 +1,79 @@
#include "stdafx.h"
#include "IShader.h"
#include "BasisParameterCollection.h"
BasisShaderParameterCollection::BasisShaderParameterCollection()
{
}
BasisShaderParameterCollection::~BasisShaderParameterCollection()
{
this->RemoveAllShaderParameters();
}
void BasisShaderParameterCollection::UseParametersOnShader(IShader& argShader)
{
if (m_ParameterMap.empty())
return;
tk_ShaderToShaderParameter::iterator shaderPos = m_ShaderToParameterMap.find(argShader.get_InternalShader());
if (shaderPos == m_ShaderToParameterMap.end())
{
tk_ShaderParameterPairs newSet;
// (re)create shader entries
tk_StringToShaderParameter::iterator iter = m_ParameterMap.begin();
for (; iter != m_ParameterMap.end(); ++iter)
{
if (iter->second != NULL)
{
ID3D10EffectVariable* var = NULL;
if (iter->second->get_ParameterNameType() == ParameterBindType::BindByVariable)
var = argShader.get_InternalShader()->get_DX10Effect()->GetVariableByName(iter->first.c_str());
else if (iter->second->get_ParameterNameType() == ParameterBindType::BindBySemantic)
var = argShader.get_InternalShader()->get_DX10Effect()->GetVariableBySemantic(iter->first.c_str());
if (var != NULL)
newSet.push_back(ShaderParameterPair(*iter->second, var));
}
}
if (newSet.empty())
return;
m_ShaderToParameterMap[argShader.get_InternalShader()] = newSet;
}
tk_ShaderParameterPairs& set = m_ShaderToParameterMap[argShader.get_InternalShader()];
for (uint32 i = 0; i < set.size(); ++i)
set[i].Parameter->ApplyValue(*set[i].TargetVariable);
}
void BasisShaderParameterCollection::SetShaderParameter(const string8& argParameterName, IShaderParameter& argParameter)
{
this->RemoveShaderParameter(argParameterName);
m_ParameterMap[argParameterName] = &argParameter;
}
void BasisShaderParameterCollection::RemoveShaderParameter(const string8& argParameterName)
{
m_ShaderToParameterMap.clear();
tk_StringToShaderParameter::iterator found = m_ParameterMap.find(argParameterName);
if (found != m_ParameterMap.end())
m_ParameterMap.erase(found);
}
void BasisShaderParameterCollection::RemoveAllShaderParameters()
{
while (!m_ParameterMap.empty())
{
delete m_ParameterMap.begin()->second;
m_ParameterMap.erase(m_ParameterMap.begin());
}
}

View File

@@ -0,0 +1,38 @@
#include "IShaderParameterCollection.h"
struct ID3D10EffectVariable;
struct IShader;
struct IInternalShader;
struct IShaderParameter;
class BasisShaderParameterCollection
{
public:
BasisShaderParameterCollection();
~BasisShaderParameterCollection();
void UseParametersOnShader(IShader& argShader);
void SetShaderParameter(const string8& argParameterName, IShaderParameter& argParameter);
void RemoveShaderParameter(const string8& argParameterName);
void RemoveAllShaderParameters();
protected:
struct ShaderParameterPair
{
ShaderParameterPair(IShaderParameter& argShaderParameter, ID3D10EffectVariable* ar_EffectVariable_)
: Parameter(&argShaderParameter)
, TargetVariable(ar_EffectVariable_)
{}
IShaderParameter* Parameter;
ID3D10EffectVariable* TargetVariable;
};
typedef std::map<string8, IShaderParameter*> tk_StringToShaderParameter;
typedef std::vector<ShaderParameterPair> tk_ShaderParameterPairs;
typedef std::map<IInternalShader*, tk_ShaderParameterPairs> tk_ShaderToShaderParameter;
tk_ShaderToShaderParameter m_ShaderToParameterMap;
tk_StringToShaderParameter m_ParameterMap;
};

View File

@@ -0,0 +1,162 @@
#include "stdafx.h"
#include "ShaderParameterCollection.h"
#include "IEngine.h"
#include "IResourceFactory.h"
#include "Types/Types.h"
ShaderParameterCollection::ShaderParameterCollection(IEngine& argEngine, unsigned char argBasePriority)
: m_Engine(argEngine)
, m_BasePriority(argBasePriority)
{
this->set_IsPreconditionForFollowingShaders(false);
}
ShaderParameterCollection::~ShaderParameterCollection()
{
this->Uninitialize();
m_Engine.get_ResourceFactory().DeleteShaderParameterCollection(*const_cast<ShaderParameterCollection*>(this), true);
}
void ShaderParameterCollection::Uninitialize()
{
m_BasisShaderParameterCollection.RemoveAllShaderParameters();
}
bool ShaderParameterCollection::get_IsPreconditionForFollowingShaders() const
{
return m_IsPreconditionForFollowingShaders;
}
void ShaderParameterCollection::set_IsPreconditionForFollowingShaders(bool argValue)
{
if (!m_Commands.empty() && argValue == m_IsPreconditionForFollowingShaders)
return;
m_IsPreconditionForFollowingShaders = argValue;
while (!m_Commands.empty())
{
delete m_Commands[0];
m_Commands.erase(m_Commands.begin());
}
if (m_IsPreconditionForFollowingShaders)
m_Commands.push_back(new Command(this, CommandFlags::EndChain | CommandFlags::FlushChain, SetFollowingShaderParameterCollectionCommandType, 0));
else
m_Commands.push_back(new Command(this, CommandFlags::None, SetShaderParameterCollectionCommandType, m_BasePriority));
this->MarkCommandsAsDirty();
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, IShaderParameter* ar_Parameter_)
{
m_BasisShaderParameterCollection.SetShaderParameter(argParameterName, *ar_Parameter_);
//this->MarkCommandsAsDirty();
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, ITexture& argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new TextureShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, float* af_Parameter_, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new FloatShaderParameter(af_Parameter_, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, float argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new FloatShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, D3DXVECTOR4* ar_Parameter_, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Float4ShaderParameter(ar_Parameter_, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, const D3DXVECTOR4& argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Float4ShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, D3DXVECTOR3* ar_Parameter_, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Float3ShaderParameter(ar_Parameter_, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, const D3DXVECTOR3& argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Float3ShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, D3DXMATRIX* argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Matrix4x4ShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, const D3DXMATRIX& argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new Matrix4x4ShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, bool* ab_Parameter_, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new BoolShaderParameter(ab_Parameter_, argParamNameType));
}
void ShaderParameterCollection::SetParameter(const string8& argParameterName, bool argParameter, ParameterBindType::Enumeration argParamNameType)
{
this->SetParameter(argParameterName, new BoolShaderParameter(argParameter, argParamNameType));
}
void ShaderParameterCollection::RemoveParameter(const string8& argParameterName)
{
m_BasisShaderParameterCollection.RemoveShaderParameter(argParameterName);
}
CommandExecuteResult::Enumeration ShaderParameterCollection::ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon)
{
if (argCommandType == SetShaderParameterCollectionCommandType)
{
if (m_Engine.get_EngineStates().LastShader == NULL)
std::wcout << std::red << "ShaderParameterCollection could not been used without a valid shader." << std::white << std::endl;
this->UseOnShader(*m_Engine.get_EngineStates().LastShader);
}
else if (argCommandType == SetFollowingShaderParameterCollectionCommandType)
{
const std::vector<Command*>& commands = argCurrentBuffer.get_BufferedCommands();
for (uint32 i = argCurrentPositon + 1; i < commands.size(); ++i)
{
IShader* shader = dynamic_cast<IShader*>(commands[i]->Owner);
if (shader != NULL && commands[i]->Type == 0)
this->UseOnShader(*shader);
}
}
return CommandExecuteResult::None;
}
void ShaderParameterCollection::UseOnShader(IShader& argShader)
{
m_BasisShaderParameterCollection.UseParametersOnShader(argShader);
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include "BasisParameterCollection.h"
#include "IShaderParameterCollection.h"
#include "../Commands/CommandUserBase.h"
struct IEngine;
class ShaderParameterCollection
: public IShaderParameterCollection
, public CommandUserBase
{
static const unsigned char SetShaderParameterCollectionCommandType = 0;
static const unsigned char SetFollowingShaderParameterCollectionCommandType = 1;
public:
ShaderParameterCollection(IEngine& argEngine, unsigned char argBasePriority = 4);
virtual ~ShaderParameterCollection();
virtual void Uninitialize();
// IShaderParameterCollection
virtual bool get_IsPreconditionForFollowingShaders() const;
virtual void set_IsPreconditionForFollowingShaders(bool argValue);
virtual void SetParameter(const string8& argParameterName,IShaderParameter* ar_Parameter_);
virtual void SetParameter(const string8& argParameterName, ITexture& argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, float* af_Parameter_, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, float argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, D3DXVECTOR4* ar_Parameter_, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, const D3DXVECTOR4& argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, D3DXVECTOR3* ar_Parameter_, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, const D3DXVECTOR3& argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, D3DXMATRIX* argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, const D3DXMATRIX& argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, bool* ab_Parameter_, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void SetParameter(const string8& argParameterName, bool argParameter, ParameterBindType::Enumeration argParamNameType = ParameterBindType::BindBySemantic);
virtual void RemoveParameter(const string8& argParameterName);
protected:
// ICommandUser
virtual CommandExecuteResult::Enumeration ExecuteCommand(unsigned char argCommandType, ICommandBuffer& argCurrentBuffer, uint32 argCurrentPositon);
virtual bool get_IsPreconditionForNextCommands() const { return m_IsPreconditionForFollowingShaders; }
virtual string8 get_UserName() const { return "ShaderParameterCollection"; }
void UseOnShader(IShader& argShader);
protected:
IEngine& m_Engine;
BasisShaderParameterCollection m_BasisShaderParameterCollection;
bool m_IsPreconditionForFollowingShaders;
unsigned char m_BasePriority;
};

View File

@@ -0,0 +1,35 @@
#pragma once
#include "IShader.h"
#include "IShaderParameterCollection.h"
//////////////////////////////////////////////////////////////////////////
struct ParameterMetaType
{
enum Enumeration
{
Fixed,
FixedArray,
Dynamic,
DynamicArray
};
};
class BaseShaderParameter
: public IShaderParameter
{
public:
BaseShaderParameter(ParameterBindType::Enumeration argType, ParameterMetaType::Enumeration argMetaType = ParameterMetaType::Fixed)
: m_NameType(argType)
, m_Type(argMetaType)
{
}
virtual ParameterBindType::Enumeration get_ParameterNameType() const { return m_NameType; }
virtual void set_ParameterNameType(ParameterBindType::Enumeration argBindType) { m_NameType = argBindType; }
protected:
ParameterBindType::Enumeration m_NameType;
ParameterMetaType::Enumeration m_Type;
};

View File

@@ -0,0 +1,60 @@
#pragma once
#include "BaseType.h"
class BoolShaderParameter
: public BaseShaderParameter
{
public:
BoolShaderParameter(bool argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new bool(argValue);
}
BoolShaderParameter(const bool* af_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new bool[m_Count];
::CopyMemory(m_Value, af_Value_, sizeof(bool) * m_Count);
}
BoolShaderParameter(bool* af_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(af_Value_)
{
}
BoolShaderParameter(bool** af_Value__, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*af_Value__)
, m_Count(argCount)
{}
virtual ~BoolShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectScalarVariable* scalar = argTargetVariable.AsScalar();
if (scalar != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
scalar->SetBool((BOOL)*m_Value);
break;
default:
scalar->SetBoolArray((BOOL*)m_Value, 0, m_Count);
}
}
}
private:
bool* m_Value;
uint32 m_Count;
};

View File

@@ -0,0 +1,178 @@
#pragma once
#include "baseType.h"
class FloatShaderParameter
: public BaseShaderParameter
{
public:
FloatShaderParameter(float argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new float(argValue);
}
FloatShaderParameter(const float* af_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new float[m_Count];
::CopyMemory(m_Value, af_Value_, sizeof(float) * m_Count);
}
FloatShaderParameter(float* af_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(af_Value_)
{
}
FloatShaderParameter(float** af_Value__, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*af_Value__)
, m_Count(argCount)
{}
virtual ~FloatShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectScalarVariable* scalar = argTargetVariable.AsScalar();
if (scalar != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
scalar->SetFloat(*m_Value);
break;
default:
scalar->SetFloatArray(m_Value, 0, m_Count);
}
}
}
private:
float* m_Value;
uint32 m_Count;
};
class Float3ShaderParameter
: public BaseShaderParameter
{
public:
Float3ShaderParameter(const D3DXVECTOR3& argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new D3DXVECTOR3(argValue);
}
Float3ShaderParameter(const D3DXVECTOR3* ar_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new D3DXVECTOR3[m_Count];
::CopyMemory(m_Value, ar_Value_, sizeof(D3DXVECTOR3) * m_Count);
}
Float3ShaderParameter(D3DXVECTOR3* ar_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(ar_Value_)
{
}
Float3ShaderParameter(D3DXVECTOR3** ar_Value__, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*ar_Value__)
, m_Count(argCount)
{}
virtual ~Float3ShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectVectorVariable* vector = argTargetVariable.AsVector();
if (vector != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
vector->SetRawValue((void*)m_Value, 0, sizeof(D3DXVECTOR3));
break;
default:
vector->SetRawValue((void*)m_Value, 0, sizeof(D3DXVECTOR3) * m_Count);
}
}
}
private:
D3DXVECTOR3* m_Value;
uint32 m_Count;
};
class Float4ShaderParameter
: public BaseShaderParameter
{
public:
Float4ShaderParameter(const D3DXVECTOR4& argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new D3DXVECTOR4(argValue);
}
Float4ShaderParameter(const D3DXVECTOR4* ar_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new D3DXVECTOR4[m_Count];
::CopyMemory(m_Value, ar_Value_, sizeof(D3DXVECTOR4) * m_Count);
}
Float4ShaderParameter(D3DXVECTOR4* ar_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(ar_Value_)
{
}
Float4ShaderParameter(D3DXVECTOR4** ar_Value__, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*ar_Value__)
, m_Count(argCount)
{}
virtual ~Float4ShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectVectorVariable* vector = argTargetVariable.AsVector();
if (vector != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
vector->SetFloatVector((float*)m_Value);
break;
default:
vector->SetFloatVectorArray((float*)m_Value, 0, m_Count);
}
}
}
private:
D3DXVECTOR4* m_Value;
uint32 m_Count;
};

View File

@@ -0,0 +1,61 @@
#pragma once
#include "BaseType.h"
class IntShaderParameter
: public BaseShaderParameter
{
public:
IntShaderParameter(int argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new int(argValue);
}
IntShaderParameter(const int* af_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new int[m_Count];
::CopyMemory(m_Value, af_Value_, sizeof(int) * m_Count);
}
IntShaderParameter(int* af_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(af_Value_)
{
}
IntShaderParameter(int** af_Value__, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*af_Value__)
, m_Count(argCount)
{}
virtual ~IntShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectScalarVariable* scalar = argTargetVariable.AsScalar();
if (scalar != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
scalar->SetInt(*m_Value);
break;
default:
scalar->SetIntArray(m_Value, 0, m_Count);
}
}
}
private:
int* m_Value;
uint32 m_Count;
};

View File

@@ -0,0 +1,61 @@
#pragma once
#include "BaseType.h"
class Matrix4x4ShaderParameter
: public BaseShaderParameter
{
public:
Matrix4x4ShaderParameter(const D3DXMATRIX& argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Fixed)
{
m_Value = new D3DXMATRIX(argValue);
}
Matrix4x4ShaderParameter(const D3DXMATRIX* ar_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::FixedArray)
, m_Count(argCount)
{
m_Value = new D3DXMATRIX[m_Count];
::CopyMemory(m_Value, ar_Value_, sizeof(D3DXMATRIX) * m_Count);
}
Matrix4x4ShaderParameter(D3DXMATRIX* ar_Value_, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::Dynamic)
, m_Value(ar_Value_)
{
}
Matrix4x4ShaderParameter(D3DXMATRIX** ar_Value_, uint32 argCount, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType, ParameterMetaType::DynamicArray)
, m_Value(*ar_Value_)
, m_Count(argCount)
{}
virtual ~Matrix4x4ShaderParameter()
{
if (m_Type == ParameterMetaType::FixedArray)
delete [] m_Value;
if (m_Type == ParameterMetaType::Fixed)
delete m_Value;
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectMatrixVariable* matrix = argTargetVariable.AsMatrix();
if (matrix != NULL)
{
switch (m_Type)
{
case ParameterMetaType::Fixed:
case ParameterMetaType::Dynamic:
matrix->SetMatrix((float*)m_Value);
break;
default:
matrix->SetMatrixArray((float*)m_Value, 0, m_Count);
}
}
}
private:
D3DXMATRIX* m_Value;
uint32 m_Count;
};

View File

@@ -0,0 +1,28 @@
#pragma once
#include "BaseType.h"
#include "ITexture.h"
class TextureShaderParameter
: public BaseShaderParameter
{
public:
TextureShaderParameter(ITexture& argValue, ParameterBindType::Enumeration argType = ParameterBindType::BindByVariable)
: BaseShaderParameter(argType)
, m_Value(&argValue)
{
}
virtual ~TextureShaderParameter()
{
}
virtual void ApplyValue(ID3D10EffectVariable& argTargetVariable)
{
ID3D10EffectShaderResourceVariable* var = argTargetVariable.AsShaderResource();
if (var != NULL)
var->SetResource(m_Value->get_ResourceViewFromTexture());
}
private:
ITexture* m_Value;
};

View File

@@ -0,0 +1,7 @@
#pragma once
#include "Integer.h"
#include "Boolean.h"
#include "Matrix.h"
#include "Float.h"
#include "Texture.h"

View File

@@ -0,0 +1,328 @@
#include "stdafx.h"
#include "IEngine.h"
#include "IResourceFactory.h"
#include "Texture.h"
#include <algorithm>
Texture::Texture(IEngine& argEngine)
: ShaderParameterCollection(argEngine, 3)
, m_ShaderResourceView(NULL)
, m_TextureResource(NULL)
, m_TextureBuffer(NULL)
, m_Texture(NULL)
, m_TextureWidth(0)
, m_TextureHeight(0)
, m_TextureFormat(DataFormat::Unknown)
, m_LockedTexture(false)
, m_LockedArrayElement(0)
, m_TextureBindingName("Diffuse")
, m_TextureArraySize(0)
{
}
Texture::~Texture()
{
this->Uninitialize();
m_Engine.get_ResourceFactory().DeleteTexture(*const_cast<Texture*>(this), true);
}
void Texture::Uninitialize()
{
m_FileName = L"";
m_LockedTexture = false;
m_TextureWidth = 0;
m_TextureHeight = 0;
m_TextureArraySize = 0;
m_TextureFormat = DataFormat::Unknown;
if (m_ShaderResourceView != NULL)
m_ShaderResourceView->Release();
m_ShaderResourceView = NULL;
if (m_Texture != NULL)
m_Texture->Release();
m_Texture = NULL;
m_TextureResource = NULL;
if (m_TextureBuffer != NULL)
m_TextureBuffer->Release();
m_TextureBuffer = NULL;
}
void Texture::set_BindingName(const string8& argName)
{
if (!m_TextureBindingName.empty())
this->RemoveParameter(m_TextureBindingName);
m_TextureBindingName = argName;
if (m_ShaderResourceView == NULL)
return;
if (!m_TextureBindingName.empty() && m_ShaderResourceView != NULL)
this->SetParameter(argName, *const_cast<Texture*>(this));
}
void Texture::LoadFromFile(const string16& argFileName, DataAccessType::Enumeration argAccessType)
{
this->Uninitialize();
D3DX10_IMAGE_LOAD_INFO loadInfo;
loadInfo.Width = D3DX10_DEFAULT;
loadInfo.Height = D3DX10_DEFAULT;
loadInfo.Depth = D3DX10_DEFAULT;
loadInfo.FirstMipLevel = D3DX10_DEFAULT;
loadInfo.MipLevels = D3DX10_DEFAULT;
loadInfo.BindFlags = D3DX10_DEFAULT;
loadInfo.MiscFlags = D3DX10_DEFAULT;
loadInfo.Format = DXGI_FORMAT(D3DX10_DEFAULT);
loadInfo.Filter = D3DX10_DEFAULT;
loadInfo.MipFilter = D3DX10_DEFAULT;
loadInfo.pSrcInfo = NULL;
if (argAccessType == DataAccessType::Static)
{
loadInfo.Usage = D3D10_USAGE(D3DX10_DEFAULT);
loadInfo.CpuAccessFlags = D3DX10_DEFAULT;
}
else if (argAccessType == DataAccessType::CpuReadOnly)
{
loadInfo.BindFlags = 0;
loadInfo.Usage = D3D10_USAGE_STAGING;
loadInfo.CpuAccessFlags = D3D10_CPU_ACCESS_WRITE | D3D10_CPU_ACCESS_READ;
}
else if (argAccessType == DataAccessType::Dynamic)
{
loadInfo.Usage = D3D10_USAGE_DYNAMIC;
loadInfo.CpuAccessFlags = D3D10_CPU_ACCESS_WRITE;
}
IFile* textureFile = m_Engine.get_FileSystem().Open(argFileName);
if (textureFile == NULL)
throw L"Unable to open texture file.";
ID3D10Device& device = m_Engine.get_DX10Device();
if (S_OK != ::D3DX10CreateTextureFromMemory(&device,
textureFile->get_Buffer(),
textureFile->get_BufferLength(),
&loadInfo,
NULL,
&m_TextureResource,
NULL
))
{
this->Uninitialize();
throw L"Texture: Unable to load texture.";
}
D3DX10_IMAGE_INFO loadedImageInfo;
D3DX10GetImageInfoFromMemory(textureFile->get_Buffer(), textureFile->get_BufferLength(), NULL, &loadedImageInfo, NULL);
delete textureFile;
if (argAccessType != DataAccessType::CpuReadOnly)
{
D3D10_SHADER_RESOURCE_VIEW_DESC texViewDesc;
ZeroMemory(&texViewDesc, sizeof(texViewDesc));
texViewDesc.Format = loadedImageInfo.Format;
texViewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
texViewDesc.Texture2DArray.MostDetailedMip = 0;
texViewDesc.Texture2DArray.MipLevels = loadedImageInfo.MipLevels;
if (S_OK != device.CreateShaderResourceView(m_TextureResource, &texViewDesc, &m_ShaderResourceView))
{
this->Uninitialize();
throw L"Texture: Unable to create resource view.";
}
}
m_Texture = (ID3D10Texture2D*)m_TextureResource;
m_FileName = argFileName;
m_TextureWidth = loadedImageInfo.Width;
m_TextureHeight = loadedImageInfo.Height;
m_TextureFormat = (DataFormat::Enumeration)loadedImageInfo.Format;
m_TextureArraySize = 0;
this->set_BindingName(this->get_BindingName());
}
void Texture::CreateTexture(const CreateTextureDescriptor& argDesc)
{
this->CreateTexture(argDesc.Width, argDesc.Height, argDesc.MipLevels, argDesc.Elements, argDesc.Format, argDesc.InitialData_, argDesc.AccessType, false, 1, 0);
}
LockedTextureData Texture::LockTextureBuffer(DataAccessMode::Enumeration argAccessMode, uint32 argArrayElement)
{
LockedTextureData outData = { NULL, 0 };
if (m_Texture == NULL)
throw L"Texture: Access forbidden until resource has been initialized.";
if (m_LockedTexture)
throw L"Texture: Only one access to the texture buffer at one time possible.";
if (argArrayElement >= m_TextureArraySize && m_TextureArraySize != 0 && argArrayElement != 0)
throw L"Texture: Access element index out of range.";
D3D10_MAPPED_TEXTURE2D mapped;
if (S_OK != m_Texture->Map(D3D10CalcSubresource(0, argArrayElement, 1), (D3D10_MAP)argAccessMode, 0, &mapped))
throw L"TextureResource: Access to the texture buffer was not successfull.";
outData.Data = mapped.pData;
outData.RowPitch = mapped.RowPitch;
m_LockedTexture = true;
m_LockedArrayElement = argArrayElement;
return outData;
}
void Texture::UnlockTextureBuffer()
{
if (m_Texture == NULL)
throw L"Texture: Access forbidden until resource has been initialized.";
if (!m_LockedTexture)
throw L"Texture: No access was done ending the access to the texture buffer is not possible.";
m_Texture->Unmap(D3D10CalcSubresource(0, m_LockedArrayElement, 1));
m_LockedTexture = false;
}
void Texture::CreateRenderTargetTexture(uint32 argWidth, uint32 argHeight, DataFormat::Enumeration argFormat, uint32 argMultiSampleCount, uint32 argMultiSampleQuality)
{
this->CreateTexture(argWidth, argHeight, 1, 0, argFormat, NULL, DataAccessType::Static, true, argMultiSampleCount, argMultiSampleQuality);
}
void Texture::CreateTexture(uint32 argWidth, uint32 argHeight, uint32 argMipLevels, uint32 argElements, DataFormat::Enumeration argFormat, TextureInitialData* ar_InitialData_, DataAccessType::Enumeration argAccessType, bool argRenderTarget, uint32 argMultiSampleCount, uint32 argMultiSampleQuality)
{
this->Uninitialize();
ID3D10Device& device = m_Engine.get_DX10Device();
if (argElements > 512)
throw L"Texture: TextureArrays must not exceed the maximum element count of 512.";
if (argElements > 0 && ar_InitialData_ == NULL)
throw L"TextureResource: TextureArrays must provide a initial data set.";
// calculate the needed amount of mipmap levels
uint32 mipMapLevels = 1;
for (uint32 tmp = std::min<uint32>(argHeight, argWidth); tmp > 1; tmp /= 2, ++mipMapLevels);
mipMapLevels = std::max<uint32>(1, std::min<uint32>(mipMapLevels, argMipLevels == 0 ? mipMapLevels : argMipLevels));
// initialize the initiate data (this includes fake mipmap levels)
D3D10_SUBRESOURCE_DATA* subResourceData = NULL;
if (ar_InitialData_ != NULL)
{
subResourceData = new D3D10_SUBRESOURCE_DATA[std::max<uint32>(1, argElements) * mipMapLevels];
uint32 maxI = std::max<uint32>(1, argElements);
for (uint32 i = 0; i < maxI; ++i)
for (uint32 k = 0; k < mipMapLevels; ++k)
{
subResourceData[i * mipMapLevels + k].pSysMem = ar_InitialData_[i].Data;
subResourceData[i * mipMapLevels + k].SysMemSlicePitch = 0;
// use fake mipmap sizes when we are processing mipmaps, so we do not allocate unnecessary memory
// due the post calculation step for the mipmaps
if (k > 0)
subResourceData[i * mipMapLevels + k].SysMemPitch = 1;
else
subResourceData[i * mipMapLevels + k].SysMemPitch = ar_InitialData_[i].Pitch;
}
}
// create the resource
D3D10_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(texDesc));
texDesc.Width = argWidth;
texDesc.Height = argHeight;
texDesc.MipLevels = mipMapLevels;
texDesc.ArraySize = std::max<uint32>(1, argElements);
texDesc.Format = (DXGI_FORMAT)argFormat;
texDesc.SampleDesc.Count = argMultiSampleCount;
texDesc.SampleDesc.Quality = argMultiSampleQuality;
if (argAccessType == DataAccessType::Static)
{
texDesc.Usage = D3D10_USAGE_DEFAULT;
texDesc.CPUAccessFlags = 0;
}
else if (argAccessType == DataAccessType::Dynamic)
{
texDesc.Usage = D3D10_USAGE_DYNAMIC;
texDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
}
if (argAccessType != DataAccessType::CpuReadOnly)
texDesc.BindFlags = ((mipMapLevels > 1 || argRenderTarget) ? D3D10_BIND_RENDER_TARGET : 0) | D3D10_BIND_SHADER_RESOURCE;
if (mipMapLevels > 1)
texDesc.MiscFlags = D3D10_RESOURCE_MISC_GENERATE_MIPS;
if (S_OK != device.CreateTexture2D(&texDesc, subResourceData, &m_Texture))
{
if (subResourceData != NULL)
delete subResourceData;
this->Uninitialize();
throw L"Texture: Unable to create plain texture.";
return;
}
if (subResourceData != NULL)
delete [] subResourceData;
m_TextureResource = m_Texture;
// update the mipmapchain
if (mipMapLevels > 1)
{
if (S_OK != ::D3DX10FilterTexture(m_Texture, 0, D3DX10_DEFAULT))
throw L"Texture: Unable to filter texture (mipmaps).";
else
m_Texture->GetDesc(&texDesc);
}
// create the resource view when possible
if (argAccessType != DataAccessType::CpuReadOnly)
{
D3D10_SHADER_RESOURCE_VIEW_DESC texViewDesc;
ZeroMemory(&texViewDesc, sizeof(texViewDesc));
texViewDesc.Format = texDesc.Format;
texViewDesc.Buffer.ElementWidth = argElements;
if (argElements > 0)
texViewDesc.ViewDimension = argMultiSampleCount == 1 ? D3D10_SRV_DIMENSION_TEXTURE2DARRAY : D3D10_SRV_DIMENSION_TEXTURE2DMSARRAY;
else
texViewDesc.ViewDimension = argMultiSampleCount == 1 ? D3D10_SRV_DIMENSION_TEXTURE2D : D3D10_SRV_DIMENSION_TEXTURE2DMS;
texViewDesc.Texture2DArray.MostDetailedMip = 0;
texViewDesc.Texture2DArray.MipLevels = texDesc.MipLevels;
texViewDesc.Texture2DArray.ArraySize = argElements;
if (S_OK != device.CreateShaderResourceView(m_TextureResource, &texViewDesc, &m_ShaderResourceView))
{
this->Uninitialize();
throw L"TextureResource: Unable to create resource view.";
}
}
// finalize
m_TextureWidth = argWidth;
m_TextureHeight = argHeight;
m_TextureFormat = argFormat;
m_TextureArraySize = argElements;
this->set_BindingName(this->get_BindingName());
}

View File

@@ -0,0 +1,57 @@
#pragma once
#include "ITexture.h"
#include "IFileSystem.h"
#include "../ShaderParameterCollection/ShaderParameterCollection.h"
#include "../../Commands/CommandUserBase.h"
class Texture
: public ITexture
, public ShaderParameterCollection
{
public:
Texture(IEngine& argEngine);
virtual ~Texture();
virtual void Uninitialize();
// ITexture
virtual string8 get_BindingName() const { return m_TextureBindingName; }
virtual void set_BindingName(const string8& argName);
virtual string16 get_FileName() const { return m_FileName; }
virtual uint32 get_TextureWidth() const{ return m_TextureWidth; }
virtual uint32 get_TextureHeight() const { return m_TextureHeight; }
virtual uint32 get_TextureArraySize() const { return m_TextureArraySize; }
virtual DataFormat::Enumeration get_TextureFormat() const{ return m_TextureFormat; }
virtual void LoadFromFile(const string16& argFileName, DataAccessType::Enumeration argAccessType = DataAccessType::Static);
virtual void CreateTexture(const CreateTextureDescriptor& argDesc);
virtual LockedTextureData LockTextureBuffer(DataAccessMode::Enumeration argAccessMode, uint32 argArrayElement = 0);
virtual void UnlockTextureBuffer();
virtual void CreateRenderTargetTexture(uint32 argWidth, uint32 argHeight, DataFormat::Enumeration argFormat, uint32 argMultiSampleCount, uint32 argMultiSampleQuality);
virtual ID3D10ShaderResourceView* get_ResourceViewFromTexture() const { return m_ShaderResourceView; }
virtual ID3D10Texture2D* get_Texture2D() const { return m_Texture; }
protected:
// ICommandUser
virtual string8 get_UserName() const { return "Texture"; }
protected:
void CreateTexture(uint32 argWidth, uint32 argHeight, uint32 argMipLevels, uint32 m_Elements, DataFormat::Enumeration argFormat, TextureInitialData* ar_InitialData_, DataAccessType::Enumeration argAccessType, bool argRenderTarget, uint32 argMultiSampleCount, uint32 argMultiSampleQuality);
private:
ID3D10Buffer* m_TextureBuffer;
ID3D10Resource* m_TextureResource;
ID3D10ShaderResourceView* m_ShaderResourceView;
ID3D10Texture2D* m_Texture;
string16 m_FileName;
uint32 m_TextureWidth;
uint32 m_TextureHeight;
uint32 m_TextureArraySize;
DataFormat::Enumeration m_TextureFormat;
bool m_LockedTexture;
uint32 m_LockedArrayElement;
string8 m_TextureBindingName;
};

View File

@@ -0,0 +1,345 @@
#include "stdafx.h"
#include "IEngine.h"
#include "IResourceFactory.h"
#include "Transformation.h"
#include <algorithm>
Transformation::Transformation(IEngine& argEngine)
: ShaderParameterCollection(argEngine, 5)
, m_LocalTranslation(0.0f, 0.0f, 0.0f)
, m_WorldTranslation(0.0f, 0.0f, 0.0f)
, m_LocalScale(1.0f, 1.0f, 1.0f)
, m_WorldScale(1.0f, 1.0f, 1.0f)
, m_LocalDirection(0.0f, 0.0f, 1.0f)
, m_LocalUpDirection(0.0f, 1.0f, 0.0f)
, m_WorldDirection(0.0f, 0.0f, 1.0f)
, m_WorldUpDirection(0.0f, 1.0f, 0.0f)
, m_LocalRotation(0.0f, 0.0f, 0.0f, 1.0f)
, m_LastKnownYawPitchRoll(0.0f, 0.0f, 0.0f)
, m_IsDirty(true)
, m_ParentTransformation(NULL)
{
}
Transformation::~Transformation()
{
m_Engine.get_ResourceFactory().DeleteTransformation(*const_cast<Transformation*>(this), true);
}
void Transformation::set_TransformationBindings(const TransformationBindings& argData)
{
m_TransformationBindings = argData;
m_BasisShaderParameterCollection.RemoveAllShaderParameters();
this->RecreateAllShaderParameters();
}
TransformationBindings Transformation::get_TransformationBindings() const
{
return m_TransformationBindings;
}
void Transformation::set_LocalPosition(const D3DXVECTOR3& argValue)
{
m_LocalTranslation = argValue;
m_IsDirty = true;
}
const D3DXVECTOR3& Transformation::get_LocalPosition() const
{
return m_LocalTranslation;
}
const D3DXVECTOR3& Transformation::get_WorldPosition() const
{
return m_WorldTranslation;
}
void Transformation::set_LocalRotationYPR(const D3DXVECTOR3& argValue)
{
m_LastKnownYawPitchRoll = argValue;
::D3DXQuaternionRotationYawPitchRoll(&m_LocalRotation, argValue.x, argValue.y, argValue.z);
m_IsDirty = true;
}
const D3DXVECTOR3& Transformation::get_LocalRotationYPR() const
{
return m_LastKnownYawPitchRoll;
}
void Transformation::set_LocalRotation(const D3DXQUATERNION& argValue)
{
m_LocalRotation = argValue;
m_IsDirty = true;
}
const D3DXQUATERNION& Transformation::get_LocalRotation() const
{
return m_LocalRotation;
}
const D3DXQUATERNION& Transformation::get_WorldRotation() const
{
return m_WorldRotation;
}
const D3DXVECTOR3& Transformation::get_LocalDirection() const
{
return m_LocalDirection;
}
const D3DXVECTOR3& Transformation::get_WorldDirection() const
{
return m_WorldDirection;
}
const D3DXVECTOR3& Transformation::get_LocalUpDirection() const
{
return m_LocalUpDirection;
}
const D3DXVECTOR3& Transformation::get_WorldUpDirection() const
{
return m_WorldUpDirection;
}
D3DXVECTOR3 Transformation::get_LocalRightDirection() const
{
return m_LocalRightDirection;
}
D3DXVECTOR3 Transformation::get_WorldRightDirection() const
{
return m_WorldRightDirection;
}
void Transformation::set_LocalScale(const D3DXVECTOR3& argValue)
{
m_LocalScale = argValue;
m_IsDirty = true;
}
const D3DXVECTOR3& Transformation::get_LocalScale() const
{
return m_LocalScale;
}
const D3DXVECTOR3& Transformation::get_WorldScale() const
{
return m_WorldScale;
}
const D3DXMATRIX& Transformation::get_WorldMatrix() const
{
return m_WorldMatrix;
}
const D3DXMATRIX& Transformation::get_LocalMatrix() const
{
return m_LocalMatrix;
}
void Transformation::set_TransformationParent(ITransformation* argValue)
{
ITransformation* oldTransformationParent = m_ParentTransformation;
m_ParentTransformation = argValue;
m_IsDirty = true;
if (oldTransformationParent != NULL)
oldTransformationParent->RemoveTransformation(*const_cast<Transformation*>(this));
}
ITransformation* Transformation::get_TransformationParent() const
{
return m_ParentTransformation;
}
void Transformation::AddTransformation(ITransformation& argTransformation)
{
if (std::find(m_TransformationChildren.begin(),m_TransformationChildren.end(), &argTransformation) == m_TransformationChildren.end())
{
argTransformation.set_TransformationParent(this);
m_TransformationChildren.push_back(&argTransformation);
m_IsDirty = true;
}
}
void Transformation::RemoveTransformation(ITransformation& argTransformation)
{
std::vector<ITransformation*>::iterator found = std::find(m_TransformationChildren.begin(),m_TransformationChildren.end(), &argTransformation);
if (found != m_TransformationChildren.end())
{
m_TransformationChildren.erase(found);
argTransformation.set_TransformationParent(NULL);
m_IsDirty = true;
}
}
const std::vector<ITransformation*>& Transformation::get_Transformations() const
{
return m_TransformationChildren;
}
void Transformation::Update(bool argForceUpdate)
{
if (!m_IsDirty && !argForceUpdate)
return;
m_IsDirty = false;
D3DXMATRIXA16 localMatrix;
D3DXMATRIXA16 worldMatrix;
::D3DXMatrixIdentity(&localMatrix);
// look if we use our own matrix
D3DXMATRIXA16 rotationMatrix;
D3DXMATRIXA16 scaleMatrix;
D3DXMATRIXA16 translationMatrix;
D3DXMATRIXA16 comboMatrix;
D3DXQUATERNION rotation = m_LocalRotation;
::D3DXMatrixRotationQuaternion(&rotationMatrix, &rotation);
::D3DXMatrixScaling(&scaleMatrix, m_LocalScale.x, m_LocalScale.y, m_LocalScale.z);
::D3DXMatrixTranslation(&translationMatrix, m_LocalTranslation.x, m_LocalTranslation.y, m_LocalTranslation.z);
::D3DXMatrixMultiply(&comboMatrix, &rotationMatrix, &translationMatrix);
::D3DXMatrixMultiply(&comboMatrix, &scaleMatrix, &comboMatrix);
::D3DXMatrixMultiply(&localMatrix, &localMatrix, &comboMatrix);
D3DXVECTOR3 localDirection = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVECTOR3 localUpDirection = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 localRightDirection = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
{
D3DXMATRIX tempMatrix;
::D3DXMatrixRotationQuaternion(&tempMatrix, &rotation);
D3DXVECTOR4 tempVector;
::D3DXVec3Transform(&tempVector, &localDirection, &tempMatrix);
localDirection.x = tempVector.x;
localDirection.y = tempVector.y;
localDirection.z = tempVector.z;
::D3DXVec3Normalize(&localDirection, &localDirection);
::D3DXVec3Transform(&tempVector, &localUpDirection, &tempMatrix);
localUpDirection.x = tempVector.x;
localUpDirection.y = tempVector.y;
localUpDirection.z = tempVector.z;
::D3DXVec3Normalize(&localUpDirection, &localUpDirection);
::D3DXVec3Transform(&tempVector, &localRightDirection, &tempMatrix);
localRightDirection.x = tempVector.x;
localRightDirection.y = tempVector.y;
localRightDirection.z = tempVector.z;
::D3DXVec3Normalize(&localRightDirection, &localRightDirection);
}
worldMatrix = localMatrix;
// stack parent matrix
if (m_ParentTransformation != NULL)
::D3DXMatrixMultiply(&worldMatrix, &localMatrix, &m_ParentTransformation->get_WorldMatrix());
// extract world related data
D3DXVECTOR3 worldScale, worldPosition;
D3DXQUATERNION worldRotation;
::D3DXMatrixDecompose(&worldScale, &worldRotation, &worldPosition, &worldMatrix);
D3DXVECTOR3 worldDirection = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVECTOR3 worldUpDirection = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 worldRightDirection = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
{
D3DXMATRIX tempMatrix;
::D3DXMatrixRotationQuaternion(&tempMatrix, &worldRotation);
D3DXVECTOR4 tempVector;
::D3DXVec3Transform(&tempVector, &worldDirection, &tempMatrix);
worldDirection.x = tempVector.x;
worldDirection.y = tempVector.y;
worldDirection.z = tempVector.z;
::D3DXVec3Normalize(&worldDirection, &worldDirection);
::D3DXVec3Transform(&tempVector, &worldUpDirection, &tempMatrix);
worldUpDirection.x = tempVector.x;
worldUpDirection.y = tempVector.y;
worldUpDirection.z = tempVector.z;
::D3DXVec3Normalize(&worldUpDirection, &worldUpDirection);
::D3DXVec3Transform(&tempVector, &worldRightDirection, &tempMatrix);
worldRightDirection.x = tempVector.x;
worldRightDirection.y = tempVector.y;
worldRightDirection.z = tempVector.z;
::D3DXVec3Normalize(&worldRightDirection, &worldRightDirection);
}
m_LocalRightDirection = localRightDirection;
m_LocalUpDirection = localRightDirection;
m_LocalDirection = localDirection;
m_WorldRightDirection = worldRightDirection;
m_WorldUpDirection = worldUpDirection;
m_WorldDirection = worldDirection;
m_WorldScale = worldScale;
m_WorldRotation = worldRotation;
m_WorldTranslation = worldPosition;
m_LocalMatrix = localMatrix;
m_WorldMatrix = worldMatrix;
// update following transformations
for (uint32 i = 0; i < m_TransformationChildren.size(); ++i)
{
IUpdatable* updatable = dynamic_cast<IUpdatable*>(m_TransformationChildren[i]);
if (updatable != NULL && (updatable->get_WantsUpdate() || argForceUpdate))
updatable->Update(argForceUpdate);
}
}
void Transformation::RecreateAllShaderParameters()
{
if (!m_TransformationBindings.WorldMatrixSemanticName.empty())
this->SetParameter(m_TransformationBindings.WorldMatrixSemanticName, &m_WorldMatrix, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.LocalMatrixSemanticName.empty())
this->SetParameter(m_TransformationBindings.LocalMatrixSemanticName, &m_LocalMatrix, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.WorldPositionSemanticName.empty())
this->SetParameter(m_TransformationBindings.WorldPositionSemanticName, &m_WorldTranslation, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.LocalPositionSemanticName.empty())
this->SetParameter(m_TransformationBindings.LocalPositionSemanticName, &m_LocalTranslation, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.WorldDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.WorldDirectionSemanticName, &m_WorldDirection, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.LocalDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.LocalDirectionSemanticName, &m_LocalDirection, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.WorldUpDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.WorldUpDirectionSemanticName, &m_WorldUpDirection, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.LocalUpDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.LocalUpDirectionSemanticName, &m_LocalUpDirection, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.WorldRightDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.WorldRightDirectionSemanticName, &m_WorldRightDirection, ParameterBindType::BindBySemantic);
if (!m_TransformationBindings.LocalRightDirectionSemanticName.empty())
this->SetParameter(m_TransformationBindings.LocalRightDirectionSemanticName, &m_LocalRightDirection, ParameterBindType::BindBySemantic);
}

View File

@@ -0,0 +1,94 @@
#pragma once
#include "IUpdatable.h"
#include "ITransformation.h"
#include "../ShaderParameterCollection/ShaderParameterCollection.h"
class Transformation
: public ShaderParameterCollection
, public ITransformation
, public IUpdatable
{
public:
// ITransformation
Transformation(IEngine& argEngine);
virtual ~Transformation();
virtual void set_TransformationBindings(const TransformationBindings& argData);
virtual TransformationBindings get_TransformationBindings() const;
virtual void set_LocalPosition(const D3DXVECTOR3& argValue);
virtual const D3DXVECTOR3& get_LocalPosition() const;
virtual const D3DXVECTOR3& get_WorldPosition() const;
virtual void set_LocalRotationYPR(const D3DXVECTOR3& argValue);
virtual const D3DXVECTOR3& get_LocalRotationYPR() const;
virtual void set_LocalRotation(const D3DXQUATERNION& argValue);
virtual const D3DXQUATERNION& get_LocalRotation() const;
virtual const D3DXQUATERNION& get_WorldRotation() const;
virtual const D3DXVECTOR3& get_LocalDirection() const;
virtual const D3DXVECTOR3& get_WorldDirection() const;
virtual const D3DXVECTOR3& get_LocalUpDirection() const;
virtual const D3DXVECTOR3& get_WorldUpDirection() const;
virtual D3DXVECTOR3 get_LocalRightDirection() const;
virtual D3DXVECTOR3 get_WorldRightDirection() const;
virtual void set_LocalScale(const D3DXVECTOR3& argValue);
virtual const D3DXVECTOR3& get_LocalScale() const;
virtual const D3DXVECTOR3& get_WorldScale() const;
virtual const D3DXMATRIX& get_WorldMatrix() const;
virtual const D3DXMATRIX& get_LocalMatrix() const;
virtual void set_TransformationParent(ITransformation* argValue);
virtual ITransformation* get_TransformationParent() const;
virtual void AddTransformation(ITransformation& argTransformation);
virtual void RemoveTransformation(ITransformation& argTransformation);
virtual const std::vector<ITransformation*>& get_Transformations() const;
// IUpdatable
virtual void Update(bool argForceUpdate);
virtual bool get_WantsUpdate() const { return m_IsDirty; }
virtual void RecreateAllShaderParameters();
protected:
// ICommandUser
virtual string8 get_UserName() const { return "Transformation"; }
protected:
D3DXMATRIX m_LocalMatrix;
D3DXMATRIX m_WorldMatrix;
D3DXVECTOR3 m_LocalTranslation;
D3DXVECTOR3 m_WorldTranslation;
D3DXQUATERNION m_LocalRotation;
D3DXQUATERNION m_WorldRotation;
D3DXVECTOR3 m_LocalScale;
D3DXVECTOR3 m_WorldScale;
D3DXVECTOR3 m_LocalDirection;
D3DXVECTOR3 m_WorldDirection;
D3DXVECTOR3 m_LocalUpDirection;
D3DXVECTOR3 m_WorldUpDirection;
D3DXVECTOR3 m_LocalRightDirection;
D3DXVECTOR3 m_WorldRightDirection;
D3DXVECTOR3 m_LastKnownYawPitchRoll;
bool m_IsDirty;
TransformationBindings m_TransformationBindings;
ITransformation* m_ParentTransformation;
std::vector<ITransformation*> m_TransformationChildren;
};

View File

@@ -0,0 +1,138 @@
#include "stdafx.h"
#include "IResourceFactory.h"
#include "TransformationAnimation.h"
TransformationAnimation::TransformationAnimation(IEngine& argEngine)
: m_Engine(argEngine)
{
}
TransformationAnimation::~TransformationAnimation()
{
//m_Engine.get_ResourceFactory().DeleteTransformationAnimation(*const_cast<TransformationAnimation*>(this), true);
}
string16 TransformationAnimation::get_Name() const
{
return m_Name;
}
void TransformationAnimation::AddKeyFrame(const KeyFrame& argKeyFrame)
{
m_Keys[argKeyFrame.m_Target][argKeyFrame.m_Time] = argKeyFrame;
}
float TransformationAnimation::get_Duration() const
{
float duration = 0.0f;
if (!m_Keys[0].empty())
duration = max(duration, m_Keys[0].rbegin()->first);
if (!m_Keys[1].empty())
duration = max(duration, m_Keys[1].rbegin()->first);
if (!m_Keys[2].empty())
duration = max(duration, m_Keys[2].rbegin()->first);
return duration;
}
D3DXMATRIX TransformationAnimation::GetTransformationAtTime(float argTime)
{
float lf_Time = /*this->get_Duration() * */argTime;
D3DXMATRIX lk_Rot;
D3DXMATRIX lk_Trans;
D3DXMATRIX lk_Scale;
// Rotation
std::map<float, KeyFrame>::const_iterator lk_LowerR = this->FindLowerBoundKey(lf_Time, KeyFrameTarget::Rotation);
std::map<float, KeyFrame>::const_iterator lk_UpperR = this->FindUpperBoundKey(lf_Time, KeyFrameTarget::Rotation);
if (lk_LowerR != m_Keys[KeyFrameTarget::Rotation].end())
{
float t = 0.0f;
if ((lk_UpperR->first - lk_LowerR->first) > 0)
t = (lf_Time - lk_LowerR->first) / (lk_UpperR->first - lk_LowerR->first);
D3DXVECTOR3 lk_A = lk_LowerR->second.m_Value;
D3DXVECTOR3 lk_B = lk_UpperR->second.m_Value;
D3DXVECTOR3 lk_TmpResult = lk_A * (1.0f - t) + lk_B * t;
D3DXMATRIX tempMat;
D3DXMatrixRotationX(&lk_Rot, (float)D3DXToRadian(lk_TmpResult.x));
D3DXMatrixRotationY(&tempMat, (float)D3DXToRadian(lk_TmpResult.y));
D3DXMatrixMultiply(&lk_Rot, &lk_Rot, &tempMat);
D3DXMatrixRotationZ(&tempMat, (float)D3DXToRadian(lk_TmpResult.z));
D3DXMatrixMultiply(&lk_Rot, &lk_Rot, &tempMat);
}
// Translation
std::map<float, KeyFrame>::const_iterator lk_LowerT = this->FindLowerBoundKey(lf_Time, KeyFrameTarget::Position);
std::map<float, KeyFrame>::const_iterator lk_UpperT = this->FindUpperBoundKey(lf_Time, KeyFrameTarget::Position);
if (lk_LowerT != m_Keys[KeyFrameTarget::Position].end())
{
float t = 0.0f;
if ((lk_UpperT->first - lk_LowerT->first) > 0)
t = (lf_Time - lk_LowerT->first) / (lk_UpperT->first - lk_LowerT->first);
D3DXVECTOR3 lk_A = lk_LowerT->second.m_Value;
D3DXVECTOR3 lk_B = lk_UpperT->second.m_Value;
D3DXVECTOR3 lk_TmpResult = lk_A * (1.0f - t) + lk_B * t;
D3DXMatrixTranslation(&lk_Trans, lk_TmpResult.x, lk_TmpResult.y, lk_TmpResult.z);
}
// Scale
std::map<float, KeyFrame>::const_iterator lk_LowerS = this->FindLowerBoundKey(lf_Time, KeyFrameTarget::Scale);
std::map<float, KeyFrame>::const_iterator lk_UpperS = this->FindUpperBoundKey(lf_Time, KeyFrameTarget::Scale);
if (lk_LowerS != m_Keys[KeyFrameTarget::Scale].end())
{
float t = 0.0f;
if ((lk_UpperS->first - lk_LowerS->first) > 0)
t = (lf_Time - lk_LowerS->first) / (lk_UpperS->first - lk_LowerS->first);
D3DXVECTOR3 lk_A = lk_LowerS->second.m_Value;
D3DXVECTOR3 lk_B = lk_UpperS->second.m_Value;
D3DXVECTOR3 lk_TmpResult = lk_A * (1.0f - t) + lk_B * t;
D3DXMatrixScaling(&lk_Scale, lk_TmpResult.x, lk_TmpResult.y, lk_TmpResult.z);
}
D3DXMATRIXA16 comboMatrix;
::D3DXMatrixMultiply(&comboMatrix, &lk_Rot, &lk_Trans);
::D3DXMatrixMultiply(&comboMatrix, &lk_Scale, &comboMatrix);
return comboMatrix;
}
std::map<float, KeyFrame>::const_iterator TransformationAnimation::FindLowerBoundKey(float argTimeime, KeyFrameTarget::Enumeration ae_Target) const
{
std::map<float, KeyFrame>::const_iterator lk_Iter = m_Keys[ae_Target].begin();
for (; lk_Iter != m_Keys[ae_Target].end(); ++lk_Iter)
if (lk_Iter->first >= argTimeime)
break;
if (lk_Iter != m_Keys[ae_Target].begin())
--lk_Iter;
return lk_Iter;
}
std::map<float, KeyFrame>::const_iterator TransformationAnimation::FindUpperBoundKey(float argTimeime, KeyFrameTarget::Enumeration ae_Target) const
{
std::map<float, KeyFrame>::const_iterator lk_Iter = m_Keys[ae_Target].begin();
for (; lk_Iter != m_Keys[ae_Target].end(); ++lk_Iter)
if (lk_Iter->first >= argTimeime)
break;
if (lk_Iter == m_Keys[ae_Target].end() && m_Keys[ae_Target].size() > 0)
--lk_Iter;
return lk_Iter;
}

View File

@@ -0,0 +1,28 @@
#pragma once
#include "ITransformationAnimation.h"
#include "IEngine.h"
class TransformationAnimation
: public ITransformationAnimation
{
public:
TransformationAnimation(IEngine& argEngine);
virtual ~TransformationAnimation();
virtual std::wstring get_Name() const;
virtual void AddKeyFrame(const KeyFrame& argKeyFrame);
virtual float get_Duration() const;
virtual D3DXMATRIX GetTransformationAtTime(float argTime);
protected:
std::map<float, KeyFrame>::const_iterator FindLowerBoundKey(float argTime, KeyFrameTarget::Enumeration argTarget) const;
std::map<float, KeyFrame>::const_iterator FindUpperBoundKey(float argTime, KeyFrameTarget::Enumeration argTarget) const;
private:
IEngine& m_Engine;
string16 m_Name;
std::map<float, KeyFrame> m_Keys[3];
};