Ejemplo n.º 1
0
	TextureResourceId TextureResourceManager::createTextureResourceByAssetId(AssetId assetId, Renderer::ITexture& texture)
	{
		// Texture resource is not allowed to exist, yet
		assert(isUninitialized(loadTextureResourceByAssetId(assetId)));

		// Create the texture resource instance
		TextureResource& textureResource = mTextureResources.addElement();
		textureResource.setResourceManager(this);
		textureResource.setAssetId(assetId);
		textureResource.mTexture = &texture;

		// Done
		setResourceLoadingState(textureResource, IResource::LoadingState::LOADED);
		return textureResource.getId();
	}
Ejemplo n.º 2
0
bool WebGLFramebuffer::initializeRenderbuffers(GraphicsContext3D* g3d)
{
    ASSERT(object());
    bool initColor = false, initDepth = false, initStencil = false;
    GC3Dbitfield mask = 0;
    if (isUninitialized(m_colorAttachment.get())) {
        initColor = true;
        mask |= GraphicsContext3D::COLOR_BUFFER_BIT;
    }
    if (isUninitialized(m_depthAttachment.get())) {
        initDepth = true;
        mask |= GraphicsContext3D::DEPTH_BUFFER_BIT;
    }
    if (isUninitialized(m_stencilAttachment.get())) {
        initStencil = true;
        mask |= GraphicsContext3D::STENCIL_BUFFER_BIT;
    }
    if (isUninitialized(m_depthStencilAttachment.get())) {
        initDepth = true;
        initStencil = true;
        mask |= (GraphicsContext3D::DEPTH_BUFFER_BIT | GraphicsContext3D::STENCIL_BUFFER_BIT);
    }
    if (!initColor && !initDepth && !initStencil)
        return true;

    // We only clear un-initialized renderbuffers when they are ready to be
    // read, i.e., when the framebuffer is complete.
    if (g3d->checkFramebufferStatus(GraphicsContext3D::FRAMEBUFFER) != GraphicsContext3D::FRAMEBUFFER_COMPLETE)
        return false;

    GC3Dfloat colorClearValue[] = {0, 0, 0, 0}, depthClearValue = 0;
    GC3Dint stencilClearValue = 0;
    GC3Dboolean colorMask[] = {0, 0, 0, 0}, depthMask = 0;
    GC3Duint stencilMask = 0xffffffff;
    GC3Dboolean isScissorEnabled = 0;
    GC3Dboolean isDitherEnabled = 0;
    if (initColor) {
        g3d->getFloatv(GraphicsContext3D::COLOR_CLEAR_VALUE, colorClearValue);
        g3d->getBooleanv(GraphicsContext3D::COLOR_WRITEMASK, colorMask);
        g3d->clearColor(0, 0, 0, 0);
        g3d->colorMask(true, true, true, true);
    }
    if (initDepth) {
        g3d->getFloatv(GraphicsContext3D::DEPTH_CLEAR_VALUE, &depthClearValue);
        g3d->getBooleanv(GraphicsContext3D::DEPTH_WRITEMASK, &depthMask);
        g3d->clearDepth(0);
        g3d->depthMask(true);
    }
    if (initStencil) {
        g3d->getIntegerv(GraphicsContext3D::STENCIL_CLEAR_VALUE, &stencilClearValue);
        g3d->getIntegerv(GraphicsContext3D::STENCIL_WRITEMASK, reinterpret_cast<GC3Dint*>(&stencilMask));
        g3d->clearStencil(0);
        g3d->stencilMask(0xffffffff);
    }
    isScissorEnabled = g3d->isEnabled(GraphicsContext3D::SCISSOR_TEST);
    g3d->disable(GraphicsContext3D::SCISSOR_TEST);
    isDitherEnabled = g3d->isEnabled(GraphicsContext3D::DITHER);
    g3d->disable(GraphicsContext3D::DITHER);

    g3d->clear(mask);

    if (initColor) {
        g3d->clearColor(colorClearValue[0], colorClearValue[1], colorClearValue[2], colorClearValue[3]);
        g3d->colorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
    }
    if (initDepth) {
        g3d->clearDepth(depthClearValue);
        g3d->depthMask(depthMask);
    }
    if (initStencil) {
        g3d->clearStencil(stencilClearValue);
        g3d->stencilMask(stencilMask);
    }
    if (isScissorEnabled)
        g3d->enable(GraphicsContext3D::SCISSOR_TEST);
    else
        g3d->disable(GraphicsContext3D::SCISSOR_TEST);
    if (isDitherEnabled)
        g3d->enable(GraphicsContext3D::DITHER);
    else
        g3d->disable(GraphicsContext3D::DITHER);

    if (initColor)
        setInitialized(m_colorAttachment.get());
    if (initDepth && initStencil && m_depthStencilAttachment)
        setInitialized(m_depthStencilAttachment.get());
    else {
        if (initDepth)
            setInitialized(m_depthAttachment.get());
        if (initStencil)
            setInitialized(m_stencilAttachment.get());
    }
    return true;
}
void WebGLFramebuffer::initializeRenderbuffers()
{
    if (!object())
        return;
    bool initColor = false, initDepth = false, initStencil = false;
    unsigned long mask = 0;
    if (isUninitialized(m_colorAttachment)) {
        initColor = true;
        mask |= GraphicsContext3D::COLOR_BUFFER_BIT;
    }
    if (isUninitialized(m_depthAttachment)) {
        initDepth = true;
        mask |= GraphicsContext3D::DEPTH_BUFFER_BIT;
    }
    if (isUninitialized(m_stencilAttachment)) {
        initStencil = true;
        mask |= GraphicsContext3D::STENCIL_BUFFER_BIT;
    }
    if (isUninitialized(m_depthStencilAttachment)) {
        initDepth = true;
        initStencil = true;
        mask |= (GraphicsContext3D::DEPTH_BUFFER_BIT | GraphicsContext3D::STENCIL_BUFFER_BIT);
    }
    if (!initColor && !initDepth && !initStencil)
        return;

    // We only clear un-initialized renderbuffers when they are ready to be
    // read, i.e., when the framebuffer is complete.
    GraphicsContext3D* g3d = context()->graphicsContext3D();
    if (g3d->checkFramebufferStatus(GraphicsContext3D::FRAMEBUFFER) != GraphicsContext3D::FRAMEBUFFER_COMPLETE)
        return;

    float colorClearValue[] = {0, 0, 0, 0}, depthClearValue = 0;
    int stencilClearValue = 0;
    unsigned char colorMask[] = {1, 1, 1, 1}, depthMask = 1, stencilMask = 1;
    bool isScissorEnabled = false;
    bool isDitherEnabled = false;
    if (initColor) {
        g3d->getFloatv(GraphicsContext3D::COLOR_CLEAR_VALUE, colorClearValue);
        g3d->getBooleanv(GraphicsContext3D::COLOR_WRITEMASK, colorMask);
        g3d->clearColor(0, 0, 0, 0);
        g3d->colorMask(true, true, true, true);
    }
    if (initDepth) {
        g3d->getFloatv(GraphicsContext3D::DEPTH_CLEAR_VALUE, &depthClearValue);
        g3d->getBooleanv(GraphicsContext3D::DEPTH_WRITEMASK, &depthMask);
        g3d->clearDepth(0);
        g3d->depthMask(true);
    }
    if (initStencil) {
        g3d->getIntegerv(GraphicsContext3D::STENCIL_CLEAR_VALUE, &stencilClearValue);
        g3d->getBooleanv(GraphicsContext3D::STENCIL_WRITEMASK, &stencilMask);
        g3d->clearStencil(0);
        g3d->stencilMask(true);
    }
    isScissorEnabled = g3d->isEnabled(GraphicsContext3D::SCISSOR_TEST);
    g3d->disable(GraphicsContext3D::SCISSOR_TEST);
    isDitherEnabled = g3d->isEnabled(GraphicsContext3D::DITHER);
    g3d->disable(GraphicsContext3D::DITHER);

    g3d->clear(mask);

    if (initColor) {
        g3d->clearColor(colorClearValue[0], colorClearValue[1], colorClearValue[2], colorClearValue[3]);
        g3d->colorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
    }
    if (initDepth) {
        g3d->clearDepth(depthClearValue);
        g3d->depthMask(depthMask);
    }
    if (initStencil) {
        g3d->clearStencil(stencilClearValue);
        g3d->stencilMask(stencilMask);
    }
    if (isScissorEnabled)
        g3d->enable(GraphicsContext3D::SCISSOR_TEST);
    else
        g3d->disable(GraphicsContext3D::SCISSOR_TEST);
    if (isDitherEnabled)
        g3d->enable(GraphicsContext3D::DITHER);
    else
        g3d->disable(GraphicsContext3D::DITHER);

    if (initColor)
        setInitialized(m_colorAttachment);
    if (initDepth && initStencil && m_depthStencilAttachment)
        setInitialized(m_depthStencilAttachment);
    else {
        if (initDepth)
            setInitialized(m_depthAttachment);
        if (initStencil)
            setInitialized(m_stencilAttachment);
    }
}
Ejemplo n.º 4
0
	Renderer::ITexture* RenderTargetTextureManager::getTextureByAssetId(AssetId assetId, const Renderer::IRenderTarget& renderTarget, uint8_t numberOfMultisamples, float resolutionScale)
	{
		Renderer::ITexture* texture = nullptr;

		// Map asset ID to render target texture signature ID
		// TODO(co) The render target texture and framebuffer handling is still under construction regarding recycling renderer resources etc. - so for now, just add render target textures to have something to start with
		/*
		AssetIdToRenderTargetTextureSignatureId::const_iterator iterator = mAssetIdToRenderTargetTextureSignatureId.find(assetId);
		if (mAssetIdToRenderTargetTextureSignatureId.cend() != iterator)
		{
			// TODO(co) Is there need for a more efficient search?
			const RenderTargetTextureSignatureId renderTargetTextureSignatureId = iterator->second;
			for (RenderTargetTextureElement& renderTargetTextureElement : mSortedRenderTargetTextureVector)
			*/
		AssetIdToIndex::const_iterator iterator = mAssetIdToIndex.find(assetId);
		if (mAssetIdToIndex.cend() != iterator)
		{
			RenderTargetTextureElement& renderTargetTextureElement = mSortedRenderTargetTextureVector[iterator->second];
			{
				const RenderTargetTextureSignature& renderTargetTextureSignature = renderTargetTextureElement.renderTargetTextureSignature;
				// if (renderTargetTextureSignature.getRenderTargetTextureSignatureId() == renderTargetTextureSignatureId)	// TODO(co) The render target texture and framebuffer handling is still under construction regarding recycling renderer resources etc. - so for now, just add render target textures to have something to start with
				{
					// Do we need to create the renderer texture instance right now?
					if (nullptr == renderTargetTextureElement.texture)
					{
						// Get the texture width and height and apply resolution scale in case the main compositor workspace render target is used
						uint32_t width = renderTargetTextureSignature.getWidth();
						uint32_t height = renderTargetTextureSignature.getHeight();
						if (isUninitialized(width) || isUninitialized(height))
						{
							uint32_t renderTargetWidth = 0;
							uint32_t renderTargetHeight = 0;
							renderTarget.getWidthAndHeight(renderTargetWidth, renderTargetHeight);
							if (!renderTargetTextureSignature.getAllowResolutionScale())
							{
								resolutionScale = 1.0f;
							}
							if (isUninitialized(width))
							{
								width = static_cast<uint32_t>(static_cast<float>(renderTargetWidth) * resolutionScale * renderTargetTextureSignature.getWidthScale());
							}
							if (isUninitialized(height))
							{
								height = static_cast<uint32_t>(static_cast<float>(renderTargetHeight) * resolutionScale * renderTargetTextureSignature.getHeightScale());
							}
						}

						// Create the texture instance, but without providing texture data (we use the texture as render target)
						// -> Use the "Renderer::TextureFlag::RENDER_TARGET"-flag to mark this texture as a render target
						// -> Required for Direct3D 9, Direct3D 10, Direct3D 11 and Direct3D 12
						// -> Not required for OpenGL and OpenGL ES 2
						// -> The optimized texture clear value is a Direct3D 12 related option
						renderTargetTextureElement.texture = mRendererRuntime.getTextureManager().createTexture2D(width, height, renderTargetTextureSignature.getTextureFormat(), nullptr, Renderer::TextureFlag::RENDER_TARGET, Renderer::TextureUsage::DEFAULT, renderTargetTextureSignature.getAllowMultisample() ? numberOfMultisamples : 1u);
						renderTargetTextureElement.texture->addReference();

						{ // Tell the texture resource manager about our render target texture so it can be referenced inside e.g. compositor nodes
							TextureResourceManager& textureResourceManager = mRendererRuntime.getTextureResourceManager();
							TextureResource* textureResource = textureResourceManager.getTextureResourceByAssetId(assetId);
							if (nullptr == textureResource)
							{
								// Create texture resource
								textureResourceManager.createTextureResourceByAssetId(assetId, *renderTargetTextureElement.texture);
							}
							else
							{
								// Update texture resource
								textureResource->setTexture(*renderTargetTextureElement.texture);
							}
						}
					}
					texture = renderTargetTextureElement.texture;
					// break;	// TODO(co) The render target texture and framebuffer handling is still under construction regarding recycling renderer resources etc. - so for now, just add render target textures to have something to start with
				}
			}
			assert(nullptr != texture);
		}
		else
		{
			// Error! Unknown asset ID, this shouldn't have happened.
			assert(false);
		}

		// Done
		return texture;
	}