dxTexture

- Added dxTexture
- Added hlsl to TextureShader.h
This commit is contained in:
Light 2021-06-29 14:21:05 +04:30
parent c9352407e1
commit 7872e42c76
5 changed files with 119 additions and 3 deletions

View file

@ -14,6 +14,22 @@ void main()
texCoords = a_TexCoords;
}
-GLSL
+HLSL
struct VertexOut
{
float2 uv : UV;
float4 position : SV_Position;
};
VertexOut main(float3 InPosition : POSITION, float2 InUV : UV)
{
VertexOut vso;
vso.position = float4(InPosition, 1.0);
vso.uv = InUV;
return vso;
}
-HLSL
)"
#define LT_ENGINE_RESOURCES_TEXTURE_SHADER_PS \
@ -32,4 +48,14 @@ void main()
FragmentColor = texture(u_Texture, texCoords);
}
-GLSL
+HLSL
sampler samplerState : register(s0);
Texture2D<float4> myTexture : register(t0);
float4 main(float2 InUV : UV) : SV_Target
{
return myTexture.Sample(samplerState, InUV);
}
-HLSL
)"

View file

@ -35,7 +35,7 @@ namespace Light {
m_TextureRenderer.shader = ResourceManager::GetShader("TextureShader");
m_TextureRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(TextureRendererProgram::TextureVertexData), LT_MAX_QUAD * 4, m_SharedContext));
m_TextureRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 6, m_SharedContext));
m_TextureRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_TextureRenderer.vertexBuffer.get(), m_TextureRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "TEXCOORDS", VertexElementType::Float2 } }, m_SharedContext));
m_TextureRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_TextureRenderer.vertexBuffer.get(), m_TextureRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "UV", VertexElementType::Float2 } }, m_SharedContext));
//** texture rendererc **//
}

View file

@ -5,7 +5,8 @@
#include "OpenGL/glTexture.h"
#ifdef LIGHT_PLATFORM_WINDOWS
// #todo:
#include "DirectX/dxTexture.h"
#include "DirectX/dxSharedContext.h"
#endif
namespace Light {
@ -17,7 +18,9 @@ namespace Light {
case GraphicsAPI::OpenGL:
return new glTexture(width, height, components, pixels);
case GraphicsAPI::DirectX: LT_WIN()
case GraphicsAPI::DirectX: LT_WIN(
return new dxTexture(width, height, components, pixels, std::static_pointer_cast<dxSharedContext>(sharedContext));
)
default:
LT_ENGINE_ASSERT(false, "Texture::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());

View file

@ -0,0 +1,60 @@
#include "ltpch.h"
#include "dxTexture.h"
#include "dxSharedContext.h"
namespace Light {
dxTexture::dxTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, std::shared_ptr<dxSharedContext> sharedContext)
: m_Context(sharedContext)
{
D3D11_TEXTURE2D_DESC textureDesc = { 0 };
textureDesc.Width = width;
textureDesc.Height = height;
textureDesc.MipLevels = 0;
textureDesc.ArraySize = 1;
textureDesc.Format = components == 4 ? DXGI_FORMAT_R8G8B8A8_UNORM :
components == 3 ? DXGI_FORMAT_R8G8B8A8_UNORM :
components == 2 ? DXGI_FORMAT_R8G8B8A8_UNORM :
components == 1 ? DXGI_FORMAT_R8G8B8A8_UNORM : DXGI_FORMAT_UNKNOWN;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = NULL;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
HRESULT hr;
DXC(m_Context->device->CreateTexture2D(&textureDesc, nullptr, &m_Texture));
m_Context->deviceContext->UpdateSubresource(m_Texture.Get(), 0u, nullptr, pixels, width * 4, 0u);
m_Texture->GetDesc(&textureDesc);
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc = { };
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0u;
shaderResourceViewDesc.Texture2D.MipLevels = -1;
m_Context->device->CreateShaderResourceView(m_Texture.Get(), &shaderResourceViewDesc, &m_ResourceView);
m_Context->deviceContext->GenerateMips(m_ResourceView.Get());
D3D11_SAMPLER_DESC samplerDesc = { };
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
m_Context->device->CreateSamplerState(&samplerDesc, &m_SamplerState);
}
void dxTexture::Bind(unsigned int slot /* = 0 */)
{
m_Context->deviceContext->PSSetSamplers(slot, 1u, m_SamplerState.GetAddressOf());
m_Context->deviceContext->PSSetShaderResources(slot, 1u, m_ResourceView.GetAddressOf());
}
}

View file

@ -0,0 +1,27 @@
#pragma once
#include "Base.h"
#include "Graphics/Texture.h"
#include <d3d11.h>
#include <wrl.h>
namespace Light {
class dxSharedContext;
class dxTexture : public Texture
{
private:
std::shared_ptr<dxSharedContext> m_Context;
Microsoft::WRL::ComPtr<ID3D11Texture2D> m_Texture;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_ResourceView;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_SamplerState;
public:
dxTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, std::shared_ptr<dxSharedContext> sharedContext);
void Bind(unsigned int slot /* = 0 */) override;
};
}