Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
niepp committed Dec 25, 2021
1 parent 84b9bce commit acb38a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
real time ASTC texture compression by computer shader

用d3d compute shader实时压缩astc纹理,支持ASTC4x4 6x6,带alpha通道,法线贴图

# 依赖

- d3d11

- [Stbimage](https://github.com/nothings/stb) - for image loading

# 使用格式

| 命令参数 | 可选值 | 作用 |
| -------- | ------ | ------------------------ |
| -4x4 | 1 or 0 | 是否使用ASTC4x4,否则6x6 |
| -alpha | 1 or 0 | 是否有alpha通道 |
| -norm | 1 or 0 | 是否法线贴图 |

例子

astc_cs_enc.exe ./textures/leaf.png -alpha 1 4x4 1

1 change: 1 addition & 0 deletions astc_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ID3D11Buffer* encode_astc(ID3D11Device *pd3dDevice, ID3D11DeviceContext *pDevice
ID3DBlob * csBlob = nullptr;
HRESULT hr = compile_shader(L"ASTC_Encode.hlsl", "MainCS", "cs_5_0", option, pd3dDevice, &csBlob);
if (FAILED(hr)) {
std::cout << "compile shader failed!" << std::endl;
return nullptr;
}

Expand Down
26 changes: 23 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define _WIN32_WINNT 0x600

#include <string>
#include <iostream>

#include <d3d11.h>
#include <d3dcompiler.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dcompiler.lib")
#pragma comment(lib, "dxguid.lib")

#define STB_IMAGE_IMPLEMENTATION

Expand Down Expand Up @@ -169,14 +169,21 @@ bool parse_cmd(int argc, char** argv, encode_option& option)
int main(int argc, char** argv)
{
if (argc < 2) {
std::cout << "wrong args count" << std::endl;
return -1;
}

encode_option option;
if (!parse_cmd(argc, argv, option)) {
std::cout << "wrong args options" << std::endl;
return -1;
}

std::cout << "encode option setting:\n"
<< "has_alpha\t" << std::boolalpha << option.has_alpha << std::endl
<< "is 4x4 block\t" << option.is4x4 << std::endl
<< "normal map\t" << option.is_normal_map << std::endl;

HWND hwnd = ::GetDesktopWindow();

// setting up device
Expand All @@ -185,20 +192,29 @@ int main(int argc, char** argv)
ID3D11DeviceContext* pDeviceContext = nullptr;
HRESULT hr = create_device_swapchain(hwnd, pSwapChain, pd3dDevice, pDeviceContext);
if (FAILED(hr)) {
std::cout << "init d3d failed!" << std::endl;
return hr;
}

std::string src_tex = argv[1];

// shader resource view
ID3D11Texture2D* pSrcTexture = load_tex(pd3dDevice, src_tex.c_str(), !option.is_normal_map);
if (pSrcTexture == nullptr) {
std::cout << "load source texture failed! [" << src_tex << "]" << std::endl;
return -1;
}

D3D11_TEXTURE2D_DESC TexDesc;
pSrcTexture->GetDesc(&TexDesc);
int TexWidth = TexDesc.Width;
int TexHeight = TexDesc.Height;

ID3D11Buffer* pOutBuf = encode_astc(pd3dDevice, pDeviceContext, pSrcTexture, option);
if (pOutBuf == nullptr) {
std::cout << "encode astc failed!" << std::endl;
return -1;
}

// save to file
D3D11_BUFFER_DESC sbDesc;
Expand All @@ -207,7 +223,11 @@ int main(int argc, char** argv)
uint32_t bufLen = sbDesc.ByteWidth;
uint8_t* pMemBuf = new uint8_t[bufLen];
ZeroMemory(pMemBuf, bufLen);
read_gpu(pd3dDevice, pDeviceContext, pOutBuf, pMemBuf, bufLen);
hr = read_gpu(pd3dDevice, pDeviceContext, pOutBuf, pMemBuf, bufLen);
if (FAILED(hr)) {
std::cout << "save astc failed!" << std::endl;
return -1;
}

std::string dst_tex(src_tex);
strip_file_extension(dst_tex);
Expand All @@ -219,7 +239,7 @@ int main(int argc, char** argv)
delete[] pMemBuf;
pMemBuf = nullptr;

system("pause");
std::cout << "save astc to:" << dst_tex << std::endl;

return 0;

Expand Down

0 comments on commit acb38a2

Please sign in to comment.