2013-11-06 22:52:00 +01:00
|
|
|
#include "Core.h"
|
2013-11-10 03:19:46 +01:00
|
|
|
|
2013-11-20 10:22:01 +01:00
|
|
|
using namespace Oyster::Graphics;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
Core::Buffer::Buffer()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
mBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
Core::Buffer::~Buffer()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
SAFE_RELEASE(mBuffer);
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
HRESULT Core::Buffer::Apply(UINT32 misc) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
|
|
|
|
switch(mType)
|
|
|
|
{
|
|
|
|
case VERTEX_BUFFER:
|
|
|
|
{
|
|
|
|
UINT32 vertexSize = mElementSize;
|
|
|
|
UINT32 offset = 0;
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->IASetVertexBuffers(misc, 1, &mBuffer, &vertexSize, &offset );
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INDEX_BUFFER:
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->IASetIndexBuffer(mBuffer, DXGI_FORMAT_R32_UINT, 0);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONSTANT_BUFFER_VS:
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->VSSetConstantBuffers(misc, 1, &mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONSTANT_BUFFER_GS:
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->GSSetConstantBuffers(misc, 1, &mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONSTANT_BUFFER_PS:
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->PSSetConstantBuffers(misc, 1, &mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONSTANT_BUFFER_CS:
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->CSSetConstantBuffers(misc,1,&mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hr = E_FAIL;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
HRESULT Core::Buffer::Init(const BUFFER_INIT_DESC& initDesc)
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
D3D11_BUFFER_DESC bufferDesc;
|
|
|
|
|
|
|
|
mType = initDesc.Type;
|
|
|
|
switch(mType)
|
|
|
|
{
|
|
|
|
case VERTEX_BUFFER:
|
|
|
|
{
|
|
|
|
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
|
|
|
|
if(initDesc.Usage == BUFFER_STREAM_OUT_TARGET)
|
|
|
|
bufferDesc.BindFlags |= D3D11_BIND_STREAM_OUTPUT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INDEX_BUFFER:
|
|
|
|
{
|
|
|
|
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STRUCTURED_BUFFER:
|
|
|
|
{
|
|
|
|
bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONSTANT_BUFFER_CS:
|
|
|
|
case CONSTANT_BUFFER_VS:
|
|
|
|
case CONSTANT_BUFFER_GS:
|
|
|
|
case CONSTANT_BUFFER_PS:
|
|
|
|
{
|
|
|
|
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return E_FAIL;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
mUsage = initDesc.Usage;
|
|
|
|
mElementSize = initDesc.ElementSize;
|
|
|
|
mElementCount = initDesc.NumElements;
|
|
|
|
|
|
|
|
bufferDesc.CPUAccessFlags = 0;
|
|
|
|
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
|
|
|
|
if(mUsage == BUFFER_CPU_READ)
|
|
|
|
{
|
|
|
|
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bufferDesc.CPUAccessFlags |= D3D11_CPU_ACCESS_READ;
|
|
|
|
}
|
|
|
|
else if(mUsage == BUFFER_CPU_WRITE)
|
|
|
|
{
|
|
|
|
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bufferDesc.CPUAccessFlags |= D3D11_CPU_ACCESS_WRITE;
|
|
|
|
}
|
|
|
|
else if(mUsage == BUFFER_CPU_WRITE_DISCARD)
|
|
|
|
{
|
|
|
|
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bufferDesc.CPUAccessFlags |= D3D11_CPU_ACCESS_WRITE;
|
|
|
|
}
|
2013-11-10 03:19:46 +01:00
|
|
|
else if(mUsage == BUFFER_USAGE_IMMUTABLE)
|
|
|
|
{
|
|
|
|
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
|
|
|
|
bufferDesc.CPUAccessFlags = 0;
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
//Desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
bufferDesc.MiscFlags = 0;
|
|
|
|
bufferDesc.ByteWidth = initDesc.NumElements * initDesc.ElementSize;
|
|
|
|
bufferDesc.StructureByteStride=0;
|
|
|
|
if(mType== STRUCTURED_BUFFER)
|
|
|
|
{
|
|
|
|
bufferDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
|
|
|
|
bufferDesc.StructureByteStride=initDesc.ElementSize;
|
|
|
|
}
|
|
|
|
//set at least 16 bytes
|
|
|
|
if(bufferDesc.ByteWidth < 16)
|
|
|
|
bufferDesc.ByteWidth = 16;
|
|
|
|
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
if(initDesc.InitData)
|
|
|
|
{
|
|
|
|
D3D11_SUBRESOURCE_DATA data;
|
|
|
|
data.pSysMem = initDesc.InitData;
|
|
|
|
data.SysMemPitch=0;
|
|
|
|
data.SysMemSlicePitch = 0;
|
2013-11-20 10:22:01 +01:00
|
|
|
hr = Core::device->CreateBuffer(&bufferDesc, &data, &mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
hr = Core::device->CreateBuffer(&bufferDesc, NULL, &mBuffer);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
2013-11-26 09:09:35 +01:00
|
|
|
//MessageBox(NULL, L"Unable to create buffer.", L"Slenda Error", MB_ICONERROR | MB_OK);
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2014-02-05 16:54:57 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Core::UsedMem += bufferDesc.ByteWidth;
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
void* Core::Buffer::Map()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
void* ret = NULL;
|
|
|
|
if(mUsage == BUFFER_CPU_WRITE || mUsage == BUFFER_CPU_READ || mUsage == BUFFER_CPU_WRITE_DISCARD)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE MappedResource;
|
|
|
|
UINT32 mapType = 0;
|
|
|
|
|
|
|
|
if(mUsage == BUFFER_CPU_READ) mapType = D3D11_MAP_READ;
|
|
|
|
else if(mUsage == BUFFER_CPU_WRITE) mapType = D3D11_MAP_WRITE;
|
|
|
|
else if(mUsage == BUFFER_CPU_WRITE_DISCARD) mapType = D3D11_MAP_WRITE_DISCARD;
|
|
|
|
|
|
|
|
HRESULT hr = S_OK;
|
2013-11-20 10:22:01 +01:00
|
|
|
if(FAILED(hr = Core::deviceContext->Map(
|
2013-11-06 22:52:00 +01:00
|
|
|
mBuffer,
|
|
|
|
0,
|
|
|
|
(D3D11_MAP)mapType,
|
|
|
|
0,
|
|
|
|
&MappedResource)))
|
|
|
|
{
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = MappedResource.pData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
void Core::Buffer::Unmap()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
Core::deviceContext->Unmap( mBuffer, 0 );
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
Core::Buffer::operator ID3D11Buffer *()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
return this->mBuffer;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
Core::Buffer::operator const ID3D11Buffer *() const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
return this->mBuffer;
|
2013-12-18 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 Core::Buffer::GetElementCount()
|
|
|
|
{
|
|
|
|
return this->mElementCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 Core::Buffer::GetVertexSize()
|
|
|
|
{
|
|
|
|
return this->mElementSize;
|
2014-01-16 09:30:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11Buffer* Core::Buffer::GetBufferPointer()
|
|
|
|
{
|
|
|
|
return this->mBuffer;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|