Skip to content

Commit

Permalink
Add hdr support in texture
Browse files Browse the repository at this point in the history
  • Loading branch information
saffronjam committed Nov 3, 2021
1 parent cd28843 commit 7051c01
Show file tree
Hide file tree
Showing 30 changed files with 2,891 additions and 23 deletions.
5 changes: 4 additions & 1 deletion Editor/Source/Layers/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void EditorLayer::OnAttach()
return false;
};

_texture = Texture::Create("Assets/Textures/Milkyway/Milkyway_small.hdr");


App::Instance().Window().GainedFocus += []
{
Expand Down Expand Up @@ -77,7 +79,7 @@ void EditorLayer::OnUi()
_scene.OnUi();

auto shaders = ShaderStore::GetAll();

ImGui::Begin("Shaders");
for (auto& shader : shaders)
{
Expand All @@ -91,6 +93,7 @@ void EditorLayer::OnUi()
ImGui::End();

ImGui::Begin("Editor");
Ui::Image(*_texture);
if (ImGui::RadioButton("Translate", _gizmoControl == GizmoControl::Translate))
{
_gizmoControl = GizmoControl::Translate;
Expand Down
2 changes: 2 additions & 0 deletions Editor/Source/Layers/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ class EditorLayer : public Layer
ViewportPanel _depthViewportPanel;

GizmoControl _gizmoControl = GizmoControl::Translate;

std::shared_ptr<Texture> _texture;
};
}
Binary file added Engine/Assets/Textures/Milkyway/Milky_thumb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions Engine/Assets/Textures/Milkyway/Milkyway.ibl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[Header]
ICOfile = "Milky_thumb.jpg"
PREVIEWfile = "Milkyway_preview.jpg"
Name = "Milkyway"
Author = "Blochi"
Location = "Space"
Comment = "purely artificial, derived from NASA map, spiced up with nebulas"
Link = "http://www.hdrlabs.com/sibl/archive.html"
Date = "2001:00:00"
Time = "00:00:00"

[Background]
BGfile = "Milkyway_BG.jpg"
BGmap = 1
BGu = 0
BGv = 0
BGheight = 4000

[Enviroment]
EVfile = "Milkyway_Light.hdr"
EVmap = 1
EVu = 0
EVv = 0
EVheight = 128
EVmulti = 1
EVgamma = 1.6

[Reflection]
REFfile = "Milkyway_small.hdr"
REFmap = 1
REFu = 0
REFv = 0
REFheight = 1024
REFmulti = 1
REFgamma = 1.6

Binary file added Engine/Assets/Textures/Milkyway/Milkyway_BG.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added Engine/Assets/Textures/skybox/back.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Engine/Assets/Textures/skybox/bottom.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Engine/Assets/Textures/skybox/front.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Engine/Assets/Textures/skybox/left.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Engine/Assets/Textures/skybox/right.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Engine/Assets/Textures/skybox/top.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 74 additions & 16 deletions Engine/Source/Saffron/Rendering/Bindables/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Saffron/ErrorHandling/ExceptionHelpers.h"
#include "Saffron/Rendering/Renderer.h"

#include "DirectXTex/DirectXTex.h"

namespace Se
{
Texture::Texture(const TextureSpec& spec, uint slot) :
Expand Down Expand Up @@ -105,24 +107,80 @@ Texture::Texture(const std::filesystem::path& path, const TextureSpec& spec, uin
? DirectX::WIC_LOADER_FORCE_RGBA32
: DirectX::WIC_LOADER_FORCE_SRGB;

// Load from disk
ComPtr<ID3D11Resource> texture;
const auto hr = DirectX::CreateWICTextureFromFileEx(
&package.Device,
inst->_path->c_str(),
0,
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0,
0,
loaderFlags,
&texture,
&inst->_nativeShaderResourceView
);
ThrowIfBad(hr);
texture->QueryInterface(__uuidof(ID3D11Texture2D), &inst->_nativeTexture);
const auto* path = inst->_path->c_str();


bool isHdr = inst->_path->extension() == ".hdr";

if (isHdr)
{
// Load from disk
DirectX::TexMetadata metaData;
DirectX::GetMetadataFromHDRFile(path, metaData);

DirectX::ScratchImage scratchImage;
DirectX::LoadFromHDRFile(path, &metaData, scratchImage);

inst->_width = metaData.width;
inst->_height = metaData.height;
inst->_format = Utils::ToSaffronFormat(metaData.format);

// Convert to texture
D3D11_TEXTURE2D_DESC td = {};
td.Width = inst->_width;
td.Height = inst->_height;
td.Format = metaData.format;
td.ArraySize = 1;
td.MipLevels = 1;
td.MiscFlags = 0;
td.SampleDesc.Count = 1;
td.Usage = D3D11_USAGE_DEFAULT;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = 0;

D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = scratchImage.GetPixels();
sd.SysMemPitch = inst->_width * Utils::ImageFormatToMemorySize(inst->_format);

auto hr = package.Device.CreateTexture2D(&td, &sd, &inst->_nativeTexture);
ThrowIfBad(hr);

D3D11_SHADER_RESOURCE_VIEW_DESC srvd = {};
srvd.Format = td.Format;
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvd.Texture2D.MipLevels = td.MipLevels;
srvd.Texture2D.MostDetailedMip = 0;

hr = package.Device.CreateShaderResourceView(
inst->_nativeTexture.Get(),
&srvd,
&inst->_nativeShaderResourceView
);
ThrowIfBad(hr);

// TODO: Store this for some time in case it is loaded from disk again
scratchImage.Release();
}
else
{
// Load from disk
ComPtr<ID3D11Resource> texture;
const auto hr = DirectX::CreateWICTextureFromFileEx(
&package.Device,
path,
0,
D3D11_USAGE_DEFAULT,
D3D11_BIND_SHADER_RESOURCE,
0,
0,
loaderFlags,
&texture,
&inst->_nativeShaderResourceView
);
ThrowIfBad(hr);
texture->QueryInterface(__uuidof(ID3D11Texture2D), &inst->_nativeTexture);
}

inst->_loaded = true;

// Fetch texture info
Expand Down
Loading

0 comments on commit 7051c01

Please sign in to comment.