@@ -100,9 +100,62 @@ void D3D11Renderer::RegisterMesh(Mesh* mesh)
100
100
101
101
void D3D11Renderer::UnregisterMesh (Mesh* mesh)
102
102
{
103
+ std::shared_ptr<D3D11Mesh> d3d11mesh = meshDict[mesh];
104
+ d3d11mesh->vertexBuffer ->Release ();
105
+ d3d11mesh->indexBuffer ->Release ();
103
106
meshDict.erase (mesh);
104
107
}
105
108
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
+
106
159
void D3D11Renderer::CreateBackBuffer ()
107
160
{
108
161
ID3D11Texture2D* pBackBuffer;
@@ -111,6 +164,23 @@ void D3D11Renderer::CreateBackBuffer()
111
164
pBackBuffer->Release ();
112
165
}
113
166
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
+
114
184
void D3D11Renderer::CreateRenderTarget ()
115
185
{
116
186
D3D11_TEXTURE2D_DESC textureDesc;
@@ -293,6 +363,21 @@ bool D3D11Renderer::InitRender()
293
363
pd3dDevice->CreateDepthStencilState (&depthStencilDesc, &depthStencilState);
294
364
}
295
365
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
+ }
296
381
297
382
return true ;
298
383
}
@@ -301,15 +386,6 @@ D3D11Renderer::~D3D11Renderer()
301
386
{
302
387
}
303
388
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
-
313
389
int D3D11Renderer::GetObjectID (int x, int y)
314
390
{
315
391
return 0 ;
@@ -396,9 +472,8 @@ void D3D11Renderer::Render(Scene& scene)
396
472
pd3dDeviceContext->Unmap (PSConstantBuffer, 0 );
397
473
}
398
474
475
+ BindTexture (*Texture::brick_tex, 0 );
399
476
400
- // pd3dDeviceContext->PSSetShaderResources(0, 1, &textureView);
401
- // pd3dDeviceContext->PSSetSamplers(0, 1, &samplerState);
402
477
403
478
DrawMesh (*mesh);
404
479
}
0 commit comments