Пример #1
0
Файл: 3d.cpp Проект: hyp/Arpheg
static void drawSubmesh(Pipeline::Constant& tc,const data::Material* material,const data::SubMesh* submesh) {
	
	
	auto renderer = services::rendering();
	if(material){
		uint32 slots[::data::Material::kMaxTextures] = {0};
		for(uint32 i = 0;i<material->textureCount();++i)
			renderer->bind(material->textures()[i],i);
		renderer->bind(tc,slots);
	}
	renderer->bind(submesh->mesh(),submesh->primitiveKind(),submesh->indexSize());
	renderer->drawIndexed(submesh->primitiveOffset(),submesh->primitiveCount());
}
Пример #2
0
void SubMesh::bindDrawIndexedRecursive(const std::string& nodeName, const ICommandBuffersSP& cmdBuffer, const SmartPointerVector<IGraphicsPipelineSP>& allGraphicsPipelines, const overwrite* renderOverwrite, const uint32_t bufferIndex) const
{
    const overwrite* currentOverwrite = renderOverwrite;
    while (currentOverwrite)
    {
    	if (!currentOverwrite->submeshBindDrawIndexedRecursive(*this, cmdBuffer, allGraphicsPipelines, bufferIndex))
    	{
    		return;
    	}

    	currentOverwrite = currentOverwrite->getNextOverwrite();
    }

    if (bsdfMaterial.get())
    {
    	// TODO: Add again.
    }
    else if (phongMaterial.get())
    {
		IGraphicsPipelineSP graphicsPipeline;

		for (size_t i = 0; i < allGraphicsPipelines.size(); i++)
		{
			if (allGraphicsPipelines[i]->getVertexBufferType() == vertexBufferType)
			{
				graphicsPipeline = allGraphicsPipelines[i];

				break;
			}
		}

		if (!graphicsPipeline.get())
		{
	        logPrint(VKTS_LOG_SEVERE, "SubMesh: Vertex buffer type not found %x", vertexBufferType);

	        return;
		}

		vkCmdBindPipeline(cmdBuffer->getCommandBuffer(), VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->getPipeline());

		phongMaterial->bindDrawIndexedRecursive(nodeName,cmdBuffer, graphicsPipeline, renderOverwrite, bufferIndex);
    }
    else
    {
        return;
    }

    bindIndexBuffer(cmdBuffer, bufferIndex);
    bindVertexBuffers(cmdBuffer, bufferIndex);
    drawIndexed(cmdBuffer, bufferIndex);
}
Пример #3
0
void ShadowsDemo::onDraw() {
	App::onDraw();

	const auto device = graphicsDevice();

	device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth);

	device->setRasterizerState(_rasterState);
	device->restoreDefaultBlendState();

	if( _spotlightShader->isValid() && _directionalShader->isValid() ) {
		const cc::Mat4f& cameraViewProj = _camera.getProj() * _camera.getView();

		bool firstLight = true;
		Light::Type boundLightType = Light::Type::Invalid;
		for( auto& light : _lights ) {
			// compute light matrices
			//const cc::Mat4f& lightView = light.view();
			//const cc::Mat4f& lightProj = light.proj();//cc::math::perspectiveRH(45.0f, 1.0f, 0.1f, light.range());//light.proj();
			//const cc::Mat4f lightViewProj = lightProj * lightView;
			if( light.type() == Light::Type::Directional ) {
				//light.computeViewProjFromFrustum(BoundingFrustum(cameraViewProj));
				light.computeViewProjFromFrustum(BoundingFrustum(_camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane(), _camera.getPosition(), _camera.getFpsFront(), _camera.getUp()));
				//light.computeViewProjOrtho(_camera.getView(), _camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane());
			}
			const cc::Mat4f lightViewProj = light.proj() * light.view();

			if( light.castShadows() ) {
				device->setDepthStencilState(device->getDefaultDepthStencilDefault());
				// set and clear render target
				ciri::IRenderTarget2D* depthTarget = _shadowTarget.get();
				device->setRenderTargets(&depthTarget, 1);
				device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
				device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth);
				// apply depth shader
				device->applyShader(_depthShader);
				// set viewport to depth size
				device->setViewport(ciri::Viewport(0, 0, _shadowTarget->getDepth()->getWidth(), _shadowTarget->getDepth()->getHeight()));
				// render all models
				for( auto& mdl : _models ) {
					_depthConstants.xform = lightViewProj * mdl->getXform().getWorld();
					_depthConstantsBuffer->setData(sizeof(DepthConstants), &_depthConstants);
					device->setVertexBuffer(mdl->getVertexBuffer());
					if( mdl->getIndexBuffer() != nullptr ) {
						device->setIndexBuffer(mdl->getIndexBuffer());
						device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
					} else {
						device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
					}
				}

				// reser viewport to screen
				device->setViewport(ciri::Viewport(0, 0, window()->getWidth(), window()->getHeight()));
				// restore default render targets
				device->restoreDefaultRenderTargets();
			}
			switch( light.type() ) {
				case Light::Type::Directional: {
					if( boundLightType != Light::Type::Directional || light.castShadows() ) {
						boundLightType = Light::Type::Directional;
						device->applyShader(_directionalShader);
						device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel);
						device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel);
					}
					_directionalConstants.LightDirection = light.direction();
					_directionalConstants.LightColor = light.diffuseColor();
					_directionalConstants.LightIntensity = light.diffuseIntensity();
					_directionalConstants.campos = _camera.getPosition();
					_directionalConstants.CastShadows = light.castShadows();
					_directionalConstants.lightViewProj = lightViewProj;
					for( auto& mdl : _models ) {
						if( !mdl->isValid() ) {
							continue;
						}
						_directionalConstants.world = mdl->getXform().getWorld();
						_directionalConstants.xform = cameraViewProj * _directionalConstants.world;
						_directionalConstantsBuffer->setData(sizeof(DirectionalConstants), &_directionalConstants);
						device->setVertexBuffer(mdl->getVertexBuffer());
						if( mdl->getIndexBuffer() != nullptr ) {
							device->setIndexBuffer(mdl->getIndexBuffer());
							device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
						} else {
							device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
						}
					}
					break;
				}
				case Light::Type::Spot: {
					if( boundLightType != Light::Type::Spot || light.castShadows() ) {
						boundLightType = Light::Type::Spot;
						device->applyShader(_spotlightShader);
						device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel);
						device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel);
					}
					_spotlightConstants.LightPosition = light.position();
					_spotlightConstants.LightDirection = light.direction();
					_spotlightConstants.LightColor = light.diffuseColor();
					_spotlightConstants.LightCosInner = light.cosConeInnerAngle(true);
					_spotlightConstants.LightCosOuter = light.cosConeOuterAngle(true);
					_spotlightConstants.LightIntensity = light.diffuseIntensity();
					_spotlightConstants.LightRange = light.range();
					_spotlightConstants.CastShadows = light.castShadows();
					_spotlightConstants.lightViewProj = lightViewProj;
					for( auto& mdl : _models ) {
						_spotlightConstants.world = mdl->getXform().getWorld();
						_spotlightConstants.xform = cameraViewProj * _spotlightConstants.world;
						_spotlightConstantsBuffer->setData(sizeof(SpotlightConstants), &_spotlightConstants);
						device->setVertexBuffer(mdl->getVertexBuffer());
						if( mdl->getIndexBuffer() != nullptr ) {
							device->setIndexBuffer(mdl->getIndexBuffer());
							device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
						} else {
							device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
						}
					}
					break;
				}
			}

			if( firstLight ) {
				firstLight = false;
				device->setBlendState(_additiveBlendState);
			}
		}
	}

	device->present();
}
Пример #4
0
void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
                                   const GrMatrix* matrix,
                                   StageBitfield stageEnableBitfield,
                                   const GrRect* srcRects[],
                                   const GrMatrix* srcMatrices[]) {

    GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad));
    GrAssert(!(fDraws.empty() && fCurrQuad));
    GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer));

    // if we have a quad IB then either append to the previous run of
    // rects or start a new run
    if (fMaxQuads) {

        bool appendToPreviousDraw = false;
        GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects);
        AutoReleaseGeometry geo(this, layout, 4, 0);
        AutoViewMatrixRestore avmr(this);
        GrMatrix combinedMatrix = this->getViewMatrix();
        this->setViewMatrix(GrMatrix::I());
        if (NULL != matrix) {
            combinedMatrix.preConcat(*matrix);
        }

        SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices());

        // we don't want to miss an opportunity to batch rects together
        // simply because the clip has changed if the clip doesn't affect
        // the rect.
        bool disabledClip = false;
        if (this->isClipState() && fClip.isRect()) {

            GrRect clipRect = fClip.getRect(0);
            // If the clip rect touches the edge of the viewport, extended it
            // out (close) to infinity to avoid bogus intersections.
            // We might consider a more exact clip to viewport if this
            // conservative test fails.
            const GrRenderTarget* target = this->getRenderTarget();
            if (0 >= clipRect.fLeft) {
                clipRect.fLeft = GR_ScalarMin;
            }
            if (target->width() <= clipRect.fRight) {
                clipRect.fRight = GR_ScalarMax;
            }
            if (0 >= clipRect.top()) {
                clipRect.fTop = GR_ScalarMin;
            }
            if (target->height() <= clipRect.fBottom) {
                clipRect.fBottom = GR_ScalarMax;
            }
            int stride = VertexSize(layout);
            bool insideClip = true;
            for (int v = 0; v < 4; ++v) {
                const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride);
                if (!clipRect.contains(p)) {
                    insideClip = false;
                    break;
                }
            }
            if (insideClip) {
                this->disableState(kClip_StateBit);
                disabledClip = true;
            }
        }
        if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 &&
            fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) {

            int vsize = VertexSize(layout);

            Draw& lastDraw = fDraws.back();

            GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer);
            GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType);
            GrAssert(0 == lastDraw.fVertexCount % 4);
            GrAssert(0 == lastDraw.fIndexCount % 6);
            GrAssert(0 == lastDraw.fStartIndex);

            GeometryPoolState& poolState = fGeoPoolStateStack.back();
            bool clearSinceLastDraw =
                            fClears.count() && 
                            fClears.back().fBeforeDrawIdx == fDraws.count();

            appendToPreviousDraw =  
                !clearSinceLastDraw &&
                lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer &&
                (fCurrQuad * 4 + lastDraw.fStartVertex) == poolState.fPoolStartVertex;

            if (appendToPreviousDraw) {
                lastDraw.fVertexCount += 4;
                lastDraw.fIndexCount += 6;
                fCurrQuad += 1;
                // we reserved above, so we should be the first
                // use of this vertex reserveation.
                GrAssert(0 == poolState.fUsedPoolVertexBytes);
                poolState.fUsedPoolVertexBytes = 4 * vsize;
            }
        }
        if (!appendToPreviousDraw) {
            this->setIndexSourceToBuffer(fQuadIndexBuffer);
            drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6);
            fCurrQuad = 1;
            fLastRectVertexLayout = layout;
        }
        if (disabledClip) {
            this->enableState(kClip_StateBit);
        }
    } else {
        INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices);
    }
}