Render Target Texture

    Part of creation was in article with texture.

    Now i need to add methods in GS like SetRenderTarget, and I want to draw scene into RTT, then draw RTT into window, so I will be able to change "screen resolution" but not changing window size.

    
    slTexture* slGSD3D11::SummonRenderTargetTexture(const slPoint& size, const slTextureInfo& ti)
    {
    	slImage img;
    	img.m_info.m_width = size.x;
    	img.m_info.m_height = size.y;
    
    	slTextureInfo inf;
    	inf = ti;
    	inf.m_type = slTextureType::RTT;
    
    	return SummonTexture(&img, inf);
    }
    

    Create MAIN render target. Add in slGSD3D11 new RTT.

    
    slGSD3D11Texture* m_mainTargetRTT = 0;
    

    In Shutdown

    
    if (m_mainTargetRTT) slDestroy(m_mainTargetRTT);
    

    Create it in updateMainTarget

    
    bool slGSD3D11::updateMainTarget()
    {
    	if (m_mainTargetRTT) slDestroy(m_mainTargetRTT);
    
    	slTextureInfo tinf;
    	m_mainTargetRTT = (slGSD3D11Texture*)SummonRenderTargetTexture(
    		slPoint((uint32_t)m_mainTargetSize.x, (uint32_t)m_mainTargetSize.y), 
    		tinf);
    ...
    

    Create main target in Init

    
    if (!updateMainTarget())
    {
    	slLog::PrintError("Can't create main target\n");
    	return false;
    }
    

    SetRenderTarget

    
    void slGSD3D11::SetRenderTarget(slTexture* t)
    {
    	if (t)
    	{
    		slGSD3D11Texture* gst = (slGSD3D11Texture*)t;
    		m_d3d11DevCon->OMSetRenderTargets(1, &gst->m_RTV, m_depthStencilView);
    		m_currentTargetView = gst->m_RTV;
    	}
    	else
    	{
    		m_d3d11DevCon->OMSetRenderTargets(1, &m_mainTargetRTT->m_RTV, m_depthStencilView);
    		m_currentTargetView = m_mainTargetRTT->m_RTV;
    	}
    }
    

    Shader for drawing main render target. I use geometry shader, I draw 1 point and create square.

    
    bool slD3D11ShaderMainTarget::init(){
    	const char* text =
    		"Texture2D tex2d_1;\n"
    		"SamplerState tex2D_sampler_1;\n"
    		"struct VSIn{\n"
    		"   float3 position : POSITION;\n"
    		"   float2 uv : TEXCOORD;\n"
    		"};\n"
    		"cbuffer cbPixel{\n"
    		"	float4 something;\n"
    		"};\n"
    		"struct VSOut{\n"
    		"   float4 pos : SV_POSITION;\n"
    		"   float2 uv : TEXCOORD;\n"
    		"};\n"
    		"struct PSOut{\n"
    		"    float4 color : SV_Target;\n"
    		"};\n"
    		"VSOut VSMain(VSIn input){\n"
    		"   VSOut output;\n"
    		"	output.pos.xyz   = input.position;\n"
    		"	output.pos.w   = 0.f;\n"
    		"	output.uv   = input.uv;\n"
    		"	return output;\n"
    		"}\n"
    		"PSOut PSMain(VSOut input){\n"
    		"   PSOut output;\n"
    		"   output.color = tex2d_1.Sample(tex2D_sampler_1, input.uv);\n"
    		"   return output;\n"
    		"}\n"
    		"[maxvertexcount(4)]\n"
    		"void GSMain(point VSOut input[1], inout TriangleStream<VSOut> TriStream ){\n"
    		"	VSOut Out;\n"
    		"	const float4 sizes[4] = {\n"
    		"		float4(-1.f, -1.f, 0.f, 1.f),\n"
    		"		float4(-1.f, 1.f, 0.f, 1.f),\n"
    		"		float4(1.f, -1.f, 0.f, 1.f),\n"
    		"		float4(1.f, 1.f, 0.f, 1.f)\n"
    		"	};\n"
    		"	const float2 uvs[4] = {\n"
    		"		float2(0.f, 1.f),\n"
    		"		float2(0.f, 0.f),\n"
    		"		float2(1.f, 1.f),\n"
    		"		float2(1.f, 0.f)\n"
    		"	};\n"
    		"	for (uint i = 0; i<4; ++i)\n"
    		"	{\n"
    		"		Out.pos = sizes[i];\n"
    		"		Out.uv = uvs[i];\n"
    		"		TriStream.Append(Out);\n"
    		"	}\n"
    
    		"	TriStream.RestartStrip();\n"
    		"}\n";
    	if (!m_gs->createShaders(
    		"vs_5_0",
    		"ps_5_0",
    		text,
    		text,
    		"VSMain",
    		"PSMain",
    		slMeshVertexType::Triangle,
    		&this->m_vShader,
    		&this->m_pShader,
    		&this->m_vLayout))
    		return false;
    	if (!m_gs->createGeometryShaders("gs_5_0", text, "GSMain", &m_gShader))
    		return false;
    
    	if (!m_gs->createConstantBuffer(sizeof(cbP), &m_cbP))
    		return false;
    
    	return true;
    }
    

    Modify BeginDraw

    
    void slGSD3D11::BeginDraw()
    {
    	m_currentTargetView = m_mainTargetRTT->m_RTV;
    	m_d3d11DevCon->OMSetRenderTargets(1, &m_currentTargetView, m_depthStencilView);
    	SetViewport(0, 0, m_mainTargetSize.x, m_mainTargetSize.y);
    }
    

    EndDraw

    
    void slGSD3D11::EndDraw()
    {
    	m_d3d11DevCon->OMSetRenderTargets(1, &m_MainTargetView, m_depthStencilView);
    	m_currentTargetView = m_MainTargetView;
    	SetViewport(0, 0, (uint32_t)m_activeWindowSize->x, (uint32_t)m_activeWindowSize->y);
    	SetScissorRect(slVec4f(0.f, 0.f, (float)m_activeWindowSize->x, (float)m_activeWindowSize->y), 0);
    	ClearColor();
    
    	SetActiveShader(m_shaderMainTarget);
    	m_shaderMainTarget->SetConstants(0);
    
    	m_d3d11DevCon->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_POINTLIST);
    	m_d3d11DevCon->Draw(1, 0);
    }
    

    Other things

    Added class slUserData, base class.

    Very often need to set\get user data.

    
    class slUserData
    {
    	void* m_data = 0;
    public:
    	slUserData() {}
    	virtual ~slUserData() {}
    
    	virtual void* GetUserData() { return m_data; }
    	virtual void SetUserData(void* d) { m_data = d; }
    };
    
    Download