| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsRenderTexture.h"
- #include "BsException.h"
- #include "BsTexture.h"
- #include "BsTextureManager.h"
- #include "BsResources.h"
- #include "BsCoreThread.h"
- #include "BsFrameAlloc.h"
- namespace bs
- {
- RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC& desc, bool requiresFlipping)
- {
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- HTexture texture = desc.colorSurfaces[i].texture;
- if (texture.isLoaded())
- {
- const TextureProperties& texProps = texture->getProperties();
- construct(&texProps, desc.colorSurfaces[i].numFaces, requiresFlipping);
- break;
- }
- }
- }
- RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC_CORE& desc, bool requiresFlipping)
- {
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- SPtr<TextureCore> texture = desc.colorSurfaces[i].texture;
- if (texture != nullptr)
- {
- const TextureProperties& texProps = texture->getProperties();
- construct(&texProps, desc.colorSurfaces[i].numFaces, requiresFlipping);
- break;
- }
- }
- }
- void RenderTextureProperties::construct(const TextureProperties* textureProps, UINT32 numSlices, bool requiresFlipping)
- {
- if (textureProps != nullptr)
- {
- mWidth = textureProps->getWidth();
- mHeight = textureProps->getHeight();
- mNumSlices = textureProps->getDepth() * numSlices;
- mColorDepth = bs::PixelUtil::getNumElemBits(textureProps->getFormat());
- mHwGamma = textureProps->isHardwareGammaEnabled();
- mMultisampleCount = textureProps->getNumSamples();
- }
- mActive = true;
- mIsWindow = false;
- mRequiresTextureFlipping = requiresFlipping;
- }
- RenderTextureCore::RenderTextureCore(const RENDER_TEXTURE_DESC_CORE& desc, UINT32 deviceIdx)
- :mDesc(desc)
- { }
- RenderTextureCore::~RenderTextureCore()
- {
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- if (mColorSurfaces[i] != nullptr)
- TextureCore::releaseView(mColorSurfaces[i]);
- }
- if (mDepthStencilSurface != nullptr)
- TextureCore::releaseView(mDepthStencilSurface);
- }
- void RenderTextureCore::initialize()
- {
- RenderTargetCore::initialize();
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- if (mDesc.colorSurfaces[i].texture != nullptr)
- {
- SPtr<TextureCore> texture = mDesc.colorSurfaces[i].texture;
- if ((texture->getProperties().getUsage() & TU_RENDERTARGET) == 0)
- BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
- mColorSurfaces[i] = TextureCore::requestView(texture, mDesc.colorSurfaces[i].mipLevel, 1,
- mDesc.colorSurfaces[i].face, mDesc.colorSurfaces[i].numFaces, GVU_RENDERTARGET);
- }
- }
- if (mDesc.depthStencilSurface.texture != nullptr)
- {
- SPtr<TextureCore> texture = mDesc.depthStencilSurface.texture;
- if ((texture->getProperties().getUsage() & TU_DEPTHSTENCIL) == 0)
- BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
- mDepthStencilSurface = TextureCore::requestView(texture, mDesc.depthStencilSurface.mipLevel, 1,
- mDesc.depthStencilSurface.face, 0, GVU_DEPTHSTENCIL);
- }
- throwIfBuffersDontMatch();
- }
- SPtr<RenderTextureCore> RenderTextureCore::create(const RENDER_TEXTURE_DESC_CORE& desc, UINT32 deviceIdx)
- {
- return TextureCoreManager::instance().createRenderTexture(desc, deviceIdx);
- }
- void RenderTextureCore::syncToCore(const CoreSyncData& data)
- {
- RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
- props = data.getData<RenderTextureProperties>();
- }
- const RenderTextureProperties& RenderTextureCore::getProperties() const
- {
- return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
- }
- void RenderTextureCore::throwIfBuffersDontMatch() const
- {
- SPtr<TextureView> firstSurfaceDesc = nullptr;
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- if (mColorSurfaces[i] == nullptr)
- continue;
- if (firstSurfaceDesc == nullptr)
- {
- firstSurfaceDesc = mColorSurfaces[i];
- continue;
- }
- const TextureProperties& curTexProps = mColorSurfaces[i]->getTexture()->getProperties();
- const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
- UINT32 curMsCount = curTexProps.getNumSamples();
- UINT32 firstMsCount = firstTexProps.getNumSamples();
- UINT32 curNumSlices = mColorSurfaces[i]->getNumArraySlices();
- UINT32 firstNumSlices = firstSurfaceDesc->getNumArraySlices();
- if (curMsCount == 0)
- curMsCount = 1;
- if (firstMsCount == 0)
- firstMsCount = 1;
- if (curTexProps.getWidth() != firstTexProps.getWidth() ||
- curTexProps.getHeight() != firstTexProps.getHeight() ||
- curTexProps.getDepth() != firstTexProps.getDepth() ||
- curMsCount != firstMsCount ||
- curNumSlices != firstNumSlices)
- {
- String errorInfo = "\nWidth: " + toString(curTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
- errorInfo += "\nHeight: " + toString(curTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
- errorInfo += "\nDepth: " + toString(curTexProps.getDepth()) + "/" + toString(firstTexProps.getDepth());
- errorInfo += "\nNum. slices: " + toString(curNumSlices) + "/" + toString(firstNumSlices);
- errorInfo += "\nMultisample Count: " + toString(curMsCount) + "/" + toString(firstMsCount);
- BS_EXCEPT(InvalidParametersException, "Provided color textures don't match!" + errorInfo);
- }
- }
- if (firstSurfaceDesc != nullptr)
- {
- const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
- UINT32 numSlices;
- if (firstTexProps.getTextureType() == TEX_TYPE_3D)
- numSlices = firstTexProps.getDepth();
- else
- numSlices = firstTexProps.getNumFaces();
- if ((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) > numSlices)
- {
- BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
- toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(numSlices));
- }
- if (firstSurfaceDesc->getMostDetailedMip() > firstTexProps.getNumMipmaps())
- {
- BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
- toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstTexProps.getNumMipmaps()));
- }
- if (mDepthStencilSurface == nullptr)
- return;
- const TextureProperties& depthTexProps = mDepthStencilSurface->getTexture()->getProperties();
- UINT32 depthMsCount = depthTexProps.getNumSamples();
- UINT32 colorMsCount = firstTexProps.getNumSamples();
- if (depthMsCount == 0)
- depthMsCount = 1;
- if (colorMsCount == 0)
- colorMsCount = 1;
- if (depthTexProps.getWidth() != firstTexProps.getWidth() ||
- depthTexProps.getHeight() != firstTexProps.getHeight() ||
- depthMsCount != colorMsCount)
- {
- String errorInfo = "\nWidth: " + toString(depthTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
- errorInfo += "\nHeight: " + toString(depthTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
- errorInfo += "\nMultisample Count: " + toString(depthMsCount) + "/" + toString(colorMsCount);
- BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
- }
- }
- }
- SPtr<RenderTexture> RenderTexture::create(const TEXTURE_DESC& desc,
- bool createDepth, PixelFormat depthStencilFormat)
- {
- return TextureManager::instance().createRenderTexture(desc, createDepth, depthStencilFormat);
- }
- SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
- {
- return TextureManager::instance().createRenderTexture(desc);
- }
- SPtr<RenderTextureCore> RenderTexture::getCore() const
- {
- return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
- }
- RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
- :mDesc(desc)
- {
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- if (desc.colorSurfaces[i].texture != nullptr)
- mBindableColorTex[i] = desc.colorSurfaces[i].texture;
- }
- if (desc.depthStencilSurface.texture != nullptr)
- mBindableDepthStencilTex = desc.depthStencilSurface.texture;
- }
- SPtr<CoreObjectCore> RenderTexture::createCore() const
- {
- RENDER_TEXTURE_DESC_CORE coreDesc;
- for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
- {
- RENDER_SURFACE_DESC_CORE surfaceDesc;
- if (mDesc.colorSurfaces[i].texture.isLoaded())
- surfaceDesc.texture = mDesc.colorSurfaces[i].texture->getCore();
- surfaceDesc.face = mDesc.colorSurfaces[i].face;
- surfaceDesc.numFaces = mDesc.colorSurfaces[i].numFaces;
- surfaceDesc.mipLevel = mDesc.colorSurfaces[i].mipLevel;
- coreDesc.colorSurfaces[i] = surfaceDesc;
- }
- if (mDesc.depthStencilSurface.texture.isLoaded())
- coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
- coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
- coreDesc.depthStencilSurface.numFaces = mDesc.depthStencilSurface.numFaces;
- coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
- return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
- }
- CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
- {
- UINT32 size = sizeof(RenderTextureProperties);
- UINT8* buffer = allocator->alloc(size);
- RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
- memcpy(buffer, (void*)&props, size);
- return CoreSyncData(buffer, size);
- }
- const RenderTextureProperties& RenderTexture::getProperties() const
- {
- return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
- }
- }
|