diff --git a/Engine/res/Shaders/TextureShader.h b/Engine/res/Shaders/TextureShader.h index ce2d1fc..0d7e8f9 100644 --- a/Engine/res/Shaders/TextureShader.h +++ b/Engine/res/Shaders/TextureShader.h @@ -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 myTexture : register(t0); + +float4 main(float2 InUV : UV) : SV_Target +{ + return myTexture.Sample(samplerState, InUV); +} + +-HLSL )" \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/Renderer.cpp b/Engine/src/Engine/Graphics/Renderer.cpp index fcf7faf..7853353 100644 --- a/Engine/src/Engine/Graphics/Renderer.cpp +++ b/Engine/src/Engine/Graphics/Renderer.cpp @@ -35,7 +35,7 @@ namespace Light { m_TextureRenderer.shader = ResourceManager::GetShader("TextureShader"); m_TextureRenderer.vertexBuffer = std::unique_ptr(VertexBuffer::Create(nullptr, sizeof(TextureRendererProgram::TextureVertexData), LT_MAX_QUAD * 4, m_SharedContext)); m_TextureRenderer.indexBuffer = std::unique_ptr(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 6, m_SharedContext)); - m_TextureRenderer.vertexLayout = std::unique_ptr(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::Create(m_TextureRenderer.vertexBuffer.get(), m_TextureRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "UV", VertexElementType::Float2 } }, m_SharedContext)); //** texture rendererc **// } diff --git a/Engine/src/Engine/Graphics/Texture.cpp b/Engine/src/Engine/Graphics/Texture.cpp index abefe3c..d22383e 100644 --- a/Engine/src/Engine/Graphics/Texture.cpp +++ b/Engine/src/Engine/Graphics/Texture.cpp @@ -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(sharedContext)); + ) default: LT_ENGINE_ASSERT(false, "Texture::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.cpp new file mode 100644 index 0000000..e4ce1e2 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.cpp @@ -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 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()); + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.h new file mode 100644 index 0000000..cd24972 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxTexture.h @@ -0,0 +1,27 @@ +#pragma once + +#include "Base.h" +#include "Graphics/Texture.h" + +#include +#include + +namespace Light { + + class dxSharedContext; + + class dxTexture : public Texture + { + private: + std::shared_ptr m_Context; + + Microsoft::WRL::ComPtr m_Texture; + Microsoft::WRL::ComPtr m_ResourceView; + Microsoft::WRL::ComPtr m_SamplerState; + public: + dxTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, std::shared_ptr sharedContext); + + void Bind(unsigned int slot /* = 0 */) override; + }; + +} \ No newline at end of file