Skip to content

Commit ecfe64c

Browse files
committed
1.support texture
2.resize backbuffer when window resize
1 parent 1899661 commit ecfe64c

File tree

6 files changed

+115
-20
lines changed

6 files changed

+115
-20
lines changed

res/shaders/hlsl/shaders.hlsl

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ VS_Output vs_main(VS_Input input)
3939

4040
float4 ps_main(VS_Output input) : SV_Target
4141
{
42-
//return mytexture.Sample(mysampler, input.uv);
43-
float NdotL = max(0, dot(input.worldNormal, _MainLightPosition));
42+
float3 albedo = mytexture.Sample(mysampler, input.uv).rgb;
4443

45-
float3 finalColor = NdotL;
44+
float nl = max(0, dot(input.worldNormal, _MainLightPosition));
45+
46+
float3 finalColor = nl * albedo * _MainLightColor.rgb;
4647

4748
return float4(finalColor, 1);
4849
}

src/common/Texture.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ glm::vec2 Texture::vec2_zero = glm::vec2(0, 0);
1010
glm::vec2 Texture::vec2_one = glm::vec2(1, 1);
1111

1212
std::shared_ptr<Texture> Texture::white_tex;
13+
std::shared_ptr<Texture> Texture::brick_tex;
1314
std::shared_ptr<Texture> Texture::normal_tex;
1415

1516
void Texture::Init()
1617
{
1718
white_tex = std::make_shared<Texture>("res/obj/white_texture.png");
1819
normal_tex = std::make_shared<Texture>("res/obj/normal_texture.png");
20+
brick_tex = std::make_shared<Texture>("res/obj/brick.png");
1921
}
2022

2123
void Texture::Uninit()
2224
{
2325
white_tex = nullptr;
2426
normal_tex = nullptr;
27+
brick_tex = nullptr;
2528
}
2629

2730
void FlipYTexture(const unsigned int width, const unsigned int height, float* data) {

src/common/Texture.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Texture
3333
GLenum type = GL_UNSIGNED_BYTE;
3434

3535
static std::shared_ptr<Texture> white_tex;
36+
static std::shared_ptr<Texture> brick_tex;
3637
static std::shared_ptr<Texture> normal_tex;
3738

3839
private:

src/d3d11/D3D11Renderer.cpp

+86-11
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,62 @@ void D3D11Renderer::RegisterMesh(Mesh* mesh)
100100

101101
void D3D11Renderer::UnregisterMesh(Mesh* mesh)
102102
{
103+
std::shared_ptr<D3D11Mesh> d3d11mesh = meshDict[mesh];
104+
d3d11mesh->vertexBuffer->Release();
105+
d3d11mesh->indexBuffer->Release();
103106
meshDict.erase(mesh);
104107
}
105108

109+
void D3D11Renderer::RegisterTexture(Texture* tex)
110+
{
111+
if (tex->bytes == nullptr)
112+
return;
113+
ID3D11ShaderResourceView* textureView;
114+
{
115+
D3D11_TEXTURE2D_DESC textureDesc = {};
116+
textureDesc.Width = tex->width;
117+
textureDesc.Height = tex->height;
118+
textureDesc.MipLevels = 1;
119+
textureDesc.ArraySize = 1;
120+
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
121+
textureDesc.SampleDesc.Count = 1;
122+
textureDesc.Usage = D3D11_USAGE_IMMUTABLE;
123+
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
124+
125+
D3D11_SUBRESOURCE_DATA textureSubresourceData = {};
126+
textureSubresourceData.pSysMem = tex->bytes;
127+
textureSubresourceData.SysMemPitch = 4 * tex->width;
128+
129+
ID3D11Texture2D* texture;
130+
pd3dDevice->CreateTexture2D(&textureDesc, &textureSubresourceData, &texture);
131+
132+
pd3dDevice->CreateShaderResourceView(texture, nullptr, &textureView);
133+
texture->Release();
134+
}
135+
136+
std::shared_ptr<TextureData> texData = std::make_shared<TextureData>();
137+
texData->srv = textureView;
138+
texDict[tex] = texData;
139+
}
140+
141+
void D3D11Renderer::UnregisterTexture(Texture* texture)
142+
{
143+
if (texture->bytes == nullptr)
144+
return;
145+
std::shared_ptr<TextureData> texData = texDict[texture];
146+
texData->srv->Release();
147+
texDict.erase(texture);
148+
}
149+
150+
void D3D11Renderer::BindTexture(Texture& texture, GLuint slot)
151+
{
152+
if (texture.bytes == nullptr)
153+
return;
154+
std::shared_ptr<TextureData> texdata = texDict[&texture];
155+
pd3dDeviceContext->PSSetShaderResources(0, 1, &texdata->srv);
156+
pd3dDeviceContext->PSSetSamplers(0, 1, &samplerState);
157+
}
158+
106159
void D3D11Renderer::CreateBackBuffer()
107160
{
108161
ID3D11Texture2D* pBackBuffer;
@@ -111,6 +164,23 @@ void D3D11Renderer::CreateBackBuffer()
111164
pBackBuffer->Release();
112165
}
113166

167+
void D3D11Renderer::UpdateBackBuffer(int width, int height)
168+
{
169+
pd3dDeviceContext->OMSetRenderTargets(0, 0, 0);
170+
mainRenderTargetView->Release();
171+
172+
HRESULT res = pSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
173+
assert(SUCCEEDED(res));
174+
175+
ID3D11Texture2D* d3d11FrameBuffer;
176+
res = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&d3d11FrameBuffer);
177+
assert(SUCCEEDED(res));
178+
179+
res = pd3dDevice->CreateRenderTargetView(d3d11FrameBuffer, NULL, &mainRenderTargetView);
180+
assert(SUCCEEDED(res));
181+
d3d11FrameBuffer->Release();
182+
}
183+
114184
void D3D11Renderer::CreateRenderTarget()
115185
{
116186
D3D11_TEXTURE2D_DESC textureDesc;
@@ -293,6 +363,21 @@ bool D3D11Renderer::InitRender()
293363
pd3dDevice->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
294364
}
295365

366+
// Create Sampler State
367+
{
368+
D3D11_SAMPLER_DESC samplerDesc = {};
369+
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
370+
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
371+
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
372+
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
373+
samplerDesc.BorderColor[0] = 1.0f;
374+
samplerDesc.BorderColor[1] = 1.0f;
375+
samplerDesc.BorderColor[2] = 1.0f;
376+
samplerDesc.BorderColor[3] = 1.0f;
377+
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
378+
379+
pd3dDevice->CreateSamplerState(&samplerDesc, &samplerState);
380+
}
296381

297382
return true;
298383
}
@@ -301,15 +386,6 @@ D3D11Renderer::~D3D11Renderer()
301386
{
302387
}
303388

304-
void D3D11Renderer::RegisterTexture(Texture* texture)
305-
{
306-
}
307-
308-
void D3D11Renderer::BindTexture(Texture& texture, GLuint slot)
309-
{
310-
std::cout << "bind texture" << std::endl;
311-
}
312-
313389
int D3D11Renderer::GetObjectID(int x, int y)
314390
{
315391
return 0;
@@ -396,9 +472,8 @@ void D3D11Renderer::Render(Scene& scene)
396472
pd3dDeviceContext->Unmap(PSConstantBuffer, 0);
397473
}
398474

475+
BindTexture(*Texture::brick_tex, 0);
399476

400-
//pd3dDeviceContext->PSSetShaderResources(0, 1, &textureView);
401-
//pd3dDeviceContext->PSSetSamplers(0, 1, &samplerState);
402477

403478
DrawMesh(*mesh);
404479
}

src/d3d11/D3D11Renderer.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class D3D11Mesh
3535
UINT offset;
3636
};
3737

38+
class TextureData
39+
{
40+
public:
41+
ID3D11ShaderResourceView* srv;
42+
};
43+
3844
class D3D11Renderer : public RealtimeRenderer
3945
{
4046
public:
@@ -51,6 +57,7 @@ class D3D11Renderer : public RealtimeRenderer
5157
void CleanupDeviceD3D();
5258

5359
void CreateBackBuffer();
60+
void UpdateBackBuffer(int width, int height);
5461

5562
void CreateRenderTarget();
5663
void CleanupRenderTarget();
@@ -64,7 +71,7 @@ class D3D11Renderer : public RealtimeRenderer
6471
virtual void RegisterMesh(Mesh* mesh);
6572
//virtual void RegisterSkybox(Skybox* skybox);
6673

67-
//virtual void UnregisterTexture(Texture* texture);
74+
virtual void UnregisterTexture(Texture* texture);
6875
virtual void UnregisterMesh(Mesh* mesh);
6976
//virtual void UnregisterSkybox(Skybox* skybox);
7077

@@ -110,6 +117,8 @@ class D3D11Renderer : public RealtimeRenderer
110117
ID3D11PixelShader* pixelShader;
111118

112119
std::unordered_map<Mesh*, std::shared_ptr<D3D11Mesh>> meshDict;
120+
std::unordered_map<Texture*, std::shared_ptr<TextureData>> texDict;
121+
ID3D11SamplerState* samplerState;
113122

114123
ID3D11InputLayout* inputLayout;
115124
ID3D11Buffer* VSConstantBuffer;

src/main_d3d11.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void MeshUnregister(Mesh* mesh) { pD3D11Renderer->UnregisterMesh(mesh); }
2424
void SkyboxUnregister(Skybox* skybox) { pD3D11Renderer->UnregisterSkybox(skybox); }
2525
void TextureBind(Texture& tex, GLuint slot) { pD3D11Renderer->BindTexture(tex, slot); }
2626

27+
void OnResizeFrame(GLFWwindow* window, int width, int height) {
28+
pD3D11Renderer->UpdateBackBuffer(width, height);
29+
}
30+
2731
// Main code
2832
int main(int, char**)
2933
{
@@ -35,7 +39,7 @@ int main(int, char**)
3539
std::cout << "Creat window error" << std::endl;
3640
return -1;
3741
}
38-
//glfwSetFramebufferSizeCallback(window, OnResizeFrame);
42+
glfwSetFramebufferSizeCallback(window, OnResizeFrame);
3943

4044
auto hwnd = glfwGetWin32Window(window);
4145

@@ -48,7 +52,7 @@ int main(int, char**)
4852
MeshUnregisterFunction = MeshUnregister;
4953
TextureBindFunction = TextureBind;
5054

51-
pD3D11Renderer = std::make_shared< D3D11Renderer>();
55+
pD3D11Renderer = std::make_shared<D3D11Renderer>();
5256

5357
D3D11Renderer& d3d11Renderer = *pD3D11Renderer;
5458

@@ -84,9 +88,9 @@ int main(int, char**)
8488

8589
Camera camera(width, height, glm::vec3(0.0f, 0.0f, 5.0f));
8690
Scene scene(camera);
87-
/*
88-
Shader::Init();
89-
Texture::Init();*/
91+
92+
//Shader::Init();
93+
Texture::Init();
9094

9195
RasterizerRenderer rasterizerRenderer;
9296
RayTracerRenderer raytracer;
@@ -132,6 +136,8 @@ int main(int, char**)
132136
glfwPollEvents();
133137
}
134138

139+
Texture::Uninit();
140+
135141
ImGui_ImplDX11_Shutdown();
136142
ImGui_ImplGlfw_Shutdown();
137143
ImGui::DestroyContext();

0 commit comments

Comments
 (0)