Skip to content

Commit 57f5cdb

Browse files
+ 第九章纹理,TexColumns项目
* zlib库是采用vs中的nuget安装的。这里也一并上传上去
1 parent 02ca9d1 commit 57f5cdb

36 files changed

+6536
-95256
lines changed

Chapter 9 Texturing/Chapter 9 Texturing.vcxproj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<Optimization>Disabled</Optimization>
5555
<SDLCheck>true</SDLCheck>
5656
<ConformanceMode>true</ConformanceMode>
57-
<AdditionalIncludeDirectories>Core;Core\Graphics;Core\Graphics\Command;Core\Graphics\DescriptorHeap;Core\Graphics\Pipeline;Core\Graphics\Resource;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
57+
<AdditionalIncludeDirectories>Core;Core\Graphics;Core\Graphics\Command;Core\Graphics\DescriptorHeap;Core\Graphics\Pipeline;Core\Graphics\Resource;Core\Graphics\Texture;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
5858
</ClCompile>
5959
<Link>
6060
<SubSystem>Windows</SubSystem>
@@ -78,7 +78,7 @@
7878
<IntrinsicFunctions>true</IntrinsicFunctions>
7979
<SDLCheck>true</SDLCheck>
8080
<ConformanceMode>true</ConformanceMode>
81-
<AdditionalIncludeDirectories>Core;Core\Graphics;Core\Graphics\Command;Core\Graphics\DescriptorHeap;Core\Graphics\Pipeline;Core\Graphics\Resource;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
81+
<AdditionalIncludeDirectories>Core;Core\Graphics;Core\Graphics\Command;Core\Graphics\DescriptorHeap;Core\Graphics\Pipeline;Core\Graphics\Resource;Core\Graphics\Texture;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
8282
</ClCompile>
8383
<Link>
8484
<SubSystem>Windows</SubSystem>
@@ -98,6 +98,7 @@
9898
<ItemGroup>
9999
<ClCompile Include="Core\CameraController.cpp" />
100100
<ClCompile Include="Core\EngineTuning.cpp" />
101+
<ClCompile Include="Core\FileUtility.cpp" />
101102
<ClCompile Include="Core\GameCore.cpp" />
102103
<ClCompile Include="Core\GameInput.cpp" />
103104
<ClCompile Include="Core\Graphics\Camera.cpp" />
@@ -120,6 +121,8 @@
120121
<ClCompile Include="Core\Graphics\Resource\LinearAllocator.cpp" />
121122
<ClCompile Include="Core\Graphics\Resource\PixelBuffer.cpp" />
122123
<ClCompile Include="Core\Graphics\Resource\ReadbackBuffer.cpp" />
124+
<ClCompile Include="Core\Graphics\Texture\DDSTextureLoader.cpp" />
125+
<ClCompile Include="Core\Graphics\Texture\TextureManager.cpp" />
123126
<ClCompile Include="Core\Math\Frustum.cpp" />
124127
<ClCompile Include="Core\Math\Random.cpp" />
125128
<ClCompile Include="Core\pch.cpp" />
@@ -133,6 +136,7 @@
133136
<ItemGroup>
134137
<ClInclude Include="Core\CameraController.h" />
135138
<ClInclude Include="Core\EngineTuning.h" />
139+
<ClInclude Include="Core\FileUtility.h" />
136140
<ClInclude Include="Core\GameCore.h" />
137141
<ClInclude Include="Core\GameInput.h" />
138142
<ClInclude Include="Core\Graphics\Camera.h" />
@@ -158,6 +162,9 @@
158162
<ClInclude Include="Core\Graphics\Resource\LinearAllocator.h" />
159163
<ClInclude Include="Core\Graphics\Resource\PixelBuffer.h" />
160164
<ClInclude Include="Core\Graphics\Resource\ReadbackBuffer.h" />
165+
<ClInclude Include="Core\Graphics\Texture\dds.h" />
166+
<ClInclude Include="Core\Graphics\Texture\DDSTextureLoader.h" />
167+
<ClInclude Include="Core\Graphics\Texture\TextureManager.h" />
161168
<ClInclude Include="Core\Hash.h" />
162169
<ClInclude Include="Core\Math\BoundingPlane.h" />
163170
<ClInclude Include="Core\Math\BoundingSphere.h" />
@@ -206,6 +213,7 @@
206213
<None Include="Core\Shaders\ShaderUtility.hlsli" />
207214
<None Include="Core\Shaders\SSAORS.hlsli" />
208215
<None Include="Core\Shaders\TextRS.hlsli" />
216+
<None Include="packages.config" />
209217
</ItemGroup>
210218
<ItemGroup>
211219
<FxCompile Include="Core\Shaders\AdaptExposureCS.hlsl" />
@@ -429,5 +437,12 @@
429437
</ItemGroup>
430438
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
431439
<ImportGroup Label="ExtensionTargets">
440+
<Import Project="..\packages\zlib-vc140-static-64.1.2.11\build\native\zlib-vc140-static-64.targets" Condition="Exists('..\packages\zlib-vc140-static-64.1.2.11\build\native\zlib-vc140-static-64.targets')" />
432441
</ImportGroup>
442+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
443+
<PropertyGroup>
444+
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
445+
</PropertyGroup>
446+
<Error Condition="!Exists('..\packages\zlib-vc140-static-64.1.2.11\build\native\zlib-vc140-static-64.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\zlib-vc140-static-64.1.2.11\build\native\zlib-vc140-static-64.targets'))" />
447+
</Target>
433448
</Project>

Chapter 9 Texturing/Chapter 9 Texturing.vcxproj.filters

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
<Filter Include="Shaders\default">
7777
<UniqueIdentifier>{6f915565-9b4a-40af-b0eb-818205eefe85}</UniqueIdentifier>
7878
</Filter>
79+
<Filter Include="Core\Graphics\Texture">
80+
<UniqueIdentifier>{5a7d369c-0aad-4e98-9906-82afcdfade82}</UniqueIdentifier>
81+
</Filter>
7982
</ItemGroup>
8083
<ItemGroup>
8184
<ClCompile Include="Core\Math\Frustum.cpp">
@@ -177,6 +180,15 @@
177180
<ClCompile Include="Core\Graphics\Resource\ReadbackBuffer.cpp">
178181
<Filter>Core\Graphics\Resource</Filter>
179182
</ClCompile>
183+
<ClCompile Include="Core\Graphics\Texture\TextureManager.cpp">
184+
<Filter>Core\Graphics\Texture</Filter>
185+
</ClCompile>
186+
<ClCompile Include="Core\Graphics\Texture\DDSTextureLoader.cpp">
187+
<Filter>Core\Graphics\Texture</Filter>
188+
</ClCompile>
189+
<ClCompile Include="Core\FileUtility.cpp">
190+
<Filter>Core</Filter>
191+
</ClCompile>
180192
</ItemGroup>
181193
<ItemGroup>
182194
<ClInclude Include="Core\Math\BoundingPlane.h">
@@ -320,6 +332,18 @@
320332
<ClInclude Include="Core\Graphics\Resource\ReadbackBuffer.h">
321333
<Filter>Core\Graphics\Resource</Filter>
322334
</ClInclude>
335+
<ClInclude Include="Core\Graphics\Texture\TextureManager.h">
336+
<Filter>Core\Graphics\Texture</Filter>
337+
</ClInclude>
338+
<ClInclude Include="Core\Graphics\Texture\dds.h">
339+
<Filter>Core\Graphics\Texture</Filter>
340+
</ClInclude>
341+
<ClInclude Include="Core\Graphics\Texture\DDSTextureLoader.h">
342+
<Filter>Core\Graphics\Texture</Filter>
343+
</ClInclude>
344+
<ClInclude Include="Core\FileUtility.h">
345+
<Filter>Core</Filter>
346+
</ClInclude>
323347
</ItemGroup>
324348
<ItemGroup>
325349
<None Include="Core\Math\Functions.inl">
@@ -403,6 +427,7 @@
403427
<None Include="Core\Shaders\LightingUtil.hlsl">
404428
<Filter>Shaders\default</Filter>
405429
</None>
430+
<None Include="packages.config" />
406431
</ItemGroup>
407432
<ItemGroup>
408433
<FxCompile Include="Core\Shaders\Bitonic32InnerSortCS.hlsl">
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5+
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6+
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7+
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8+
//
9+
// Developed by Minigraph
10+
//
11+
// Author: James Stanard
12+
//
13+
14+
#include "pch.h"
15+
#include "FileUtility.h"
16+
#include <fstream>
17+
#include <mutex>
18+
#include <zlib.h> // From NuGet package
19+
20+
using namespace std;
21+
using namespace Utility;
22+
23+
namespace Utility
24+
{
25+
ByteArray NullFile = make_shared<vector<byte> > (vector<byte>() );
26+
}
27+
28+
ByteArray DecompressZippedFile( wstring& fileName );
29+
30+
ByteArray ReadFileHelper(const wstring& fileName)
31+
{
32+
struct _stat64 fileStat;
33+
int fileExists = _wstat64(fileName.c_str(), &fileStat);
34+
if (fileExists == -1)
35+
return NullFile;
36+
37+
ifstream file( fileName, ios::in | ios::binary );
38+
if (!file)
39+
return NullFile;
40+
41+
Utility::ByteArray byteArray = make_shared<vector<byte> >( file.seekg(0, ios::end).tellg() );
42+
file.seekg(0, ios::beg).read( (char*)byteArray->data(), byteArray->size() );
43+
file.close();
44+
45+
ASSERT(byteArray->size() == (size_t)fileStat.st_size);
46+
47+
return byteArray;
48+
}
49+
50+
ByteArray ReadFileHelperEx( shared_ptr<wstring> fileName)
51+
{
52+
std::wstring zippedFileName = *fileName + L".gz";
53+
ByteArray firstTry = DecompressZippedFile(zippedFileName);
54+
if (firstTry != NullFile)
55+
return firstTry;
56+
57+
return ReadFileHelper(*fileName);
58+
}
59+
60+
ByteArray Inflate(ByteArray CompressedSource, int& err, uint32_t ChunkSize = 0x100000 )
61+
{
62+
// Create a dynamic buffer to hold compressed blocks
63+
vector<unique_ptr<byte> > blocks;
64+
65+
z_stream strm = {};
66+
strm.data_type = Z_BINARY;
67+
strm.total_in = strm.avail_in = (uInt)CompressedSource->size();
68+
strm.next_in = CompressedSource->data();
69+
70+
err = inflateInit2(&strm, (15 + 32)); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib
71+
72+
while (err == Z_OK || err == Z_BUF_ERROR)
73+
{
74+
strm.avail_out = ChunkSize;
75+
strm.next_out = (byte*)malloc(ChunkSize);
76+
blocks.emplace_back(strm.next_out);
77+
err = inflate(&strm, Z_NO_FLUSH);
78+
}
79+
80+
if (err != Z_STREAM_END)
81+
{
82+
inflateEnd(&strm);
83+
return NullFile;
84+
}
85+
86+
ASSERT(strm.total_out > 0, "Nothing to decompress");
87+
88+
Utility::ByteArray byteArray = make_shared<vector<byte> >( strm.total_out );
89+
90+
// Allocate actual memory for this.
91+
// copy the bits into that RAM.
92+
// Free everything else up!!
93+
void* curDest = byteArray->data();
94+
size_t remaining = byteArray->size();
95+
96+
for (size_t i = 0; i < blocks.size(); ++i)
97+
{
98+
ASSERT(remaining > 0);
99+
100+
size_t CopySize = min(remaining, (size_t)ChunkSize);
101+
102+
memcpy(curDest, blocks[i].get(), CopySize);
103+
curDest = (byte*)curDest + CopySize;
104+
remaining -= CopySize;
105+
}
106+
107+
inflateEnd(&strm);
108+
109+
return byteArray;
110+
}
111+
112+
ByteArray DecompressZippedFile( wstring& fileName )
113+
{
114+
ByteArray CompressedFile = ReadFileHelper(fileName);
115+
if (CompressedFile == NullFile)
116+
return NullFile;
117+
118+
int error;
119+
ByteArray DecompressedFile = Inflate(CompressedFile, error);
120+
if (DecompressedFile->size() == 0)
121+
{
122+
Utility::Printf(L"Couldn't unzip file %s: Error = %d\n", fileName.c_str(), error);
123+
return NullFile;
124+
}
125+
126+
return DecompressedFile;
127+
}
128+
129+
ByteArray Utility::ReadFileSync( const wstring& fileName)
130+
{
131+
return ReadFileHelperEx(make_shared<wstring>(fileName));
132+
}
133+
134+
task<ByteArray> Utility::ReadFileAsync(const wstring& fileName)
135+
{
136+
shared_ptr<wstring> SharedPtr = make_shared<wstring>(fileName);
137+
return create_task( [=] { return ReadFileHelperEx(SharedPtr); } );
138+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5+
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6+
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7+
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8+
//
9+
// Developed by Minigraph
10+
//
11+
// Author: James Stanard
12+
//
13+
14+
#pragma once
15+
16+
#include "pch.h"
17+
#include <vector>
18+
#include <string>
19+
#include <ppl.h>
20+
21+
namespace Utility
22+
{
23+
using namespace std;
24+
using namespace concurrency;
25+
26+
typedef shared_ptr<vector<byte> > ByteArray;
27+
extern ByteArray NullFile;
28+
29+
// Reads the entire contents of a binary file. If the file with the same name except with an additional
30+
// ".gz" suffix exists, it will be loaded and decompressed instead.
31+
// This operation blocks until the entire file is read.
32+
ByteArray ReadFileSync(const wstring& fileName);
33+
34+
// Same as previous except that it does not block but instead returns a task.
35+
task<ByteArray> ReadFileAsync(const wstring& fileName);
36+
37+
} // namespace Utility

Chapter 9 Texturing/Core/Graphics/Command/CommandContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "PipelineState.h"
2020
#include "RootSignature.h"
2121
#include "GpuBuffer.h"
22-
//#include "TextureManager.h"
22+
#include "TextureManager.h"
2323
#include "PixelBuffer.h"
2424
#include "DynamicDescriptorHeap.h"
2525
#include "LinearAllocator.h"
@@ -29,6 +29,7 @@
2929

3030
class ColorBuffer;
3131
class DepthBuffer;
32+
class Texture;
3233
class GraphicsContext;
3334

3435
struct DWParam

Chapter 9 Texturing/Core/Graphics/GraphicsCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ void Graphics::Shutdown( void )
554554
// TextRenderer::Shutdown();
555555
// GraphRenderer::Shutdown();
556556
// ParticleEffects::Shutdown();
557-
// TextureManager::Shutdown();
557+
TextureManager::Shutdown();
558558

559559
for (UINT i = 0; i < SWAP_CHAIN_BUFFER_COUNT; ++i)
560560
g_DisplayPlane[i].Destroy();

0 commit comments

Comments
 (0)