포스트

DirecX12 | Lecture 13. Material (인프런)

DirectX12 강의 13. Material를 정리한 글입니다.

DirecX12 | Lecture 13. Material (인프런)

강의 정보 🎓


shader, mesh, texture와 같은 resource는 모든 동일한 오브젝트끼리 공유가 가능 할 수 있다. Material은 shader, texture 등을 하나로 묶어서 관리한다.


1. Input 클래스 개선

  • Input::Update()에서 GetKeyboardState() 사용:
1
2
3
4
BYTE asciiKeys[KEY_TYPE_COUNT] = {};
if (::GetKeyboardState(asciiKeys) == false) {
    return;
}

2. ConstantBuffer 개선

  • ConstantBufferType Enum class를 작성한다.
  • CbvRegisterNumber 멤버 변수 추가 → PushData 이후 SetCBV()까지 하나의 작업으로 통합한다.
  • 리턴 타입을 void로 변경한다.

목적

  • 레지스터 번호 실수 방지
  • 사용 시 명확한 의미 전달

3. Engine에서 ConstantBuffer 여러 개 관리

  • 기존 1개 → std::vector<std::shared_ptr<ConstantBuffer>> 사용한다.
  • Init에서 기존 ConstantBuffer 초기화 코드 제거한다.
  • 대신 CreateConstantBuffer() 함수를 도입한다.
1
2
CreateConstantBuffer(CbvRegisterNumber::b0, sizeof(Transform), 256);
CreateConstantBuffer(CbvRegisterNumber::b1, sizeof(MaterialParams), 256);

4. Material 클래스 생성

MaterialParams 구조체

1
2
3
4
5
6
struct MaterialParams {
    void SetInt(uint8 index, int32 value) { intParams[index] = value; }
    void SetFloat(uint8 index, float value) { floatParams[index] = value; }
    std::array<int32, MATERIAL_INT_COUNT> intParams;
    std::array<float, MATERIAL_FLOAT_COUNT> floatParams;
};

Material 클래스

  • Shader, MaterialParams, Texture 배열을 멤버 변수로 가진다.
  • Update() 함수:
    • PushData(),SetSRV(),Shader의 Update() 를 호출한다.

5. Default.hlsli 수정

  • cbuffer Material_Params 추가한다.
  • Texture2D tex_0 ~ tex_4 추가한다.

6. CommandList → RenderBegin 수정

1
2
constantBuffers[static_cast<uint8>(ConstantBufferType::TRANSFORM)]->Clear();
constantBuffers[static_cast<uint8>(ConstantBufferType::MATERIAL)]->Clear();

7. Mesh → Render 수정

1
2
3
4
constantBuffers[static_cast<uint8>(ConstantBufferType::TRANSFORM)]->PushData(&mTransform, sizeof(Transform));

mMaterial->Update(device, commandList, tableDescriptorHeap,
    constantBuffers[static_cast<uint8>(ConstantBufferType::TRANSFORM)]);
  • 기존 SetSRV() 호출을 제거한다.
  • Texture 관련 코드를 삭제하고, Material 멤버를 추가한다.

8. Shader 코드 테스트

  • Shader 코드에서 float_0 ~ float_2 를 position에 더해 테스트한다.
1
2
3
output.pos.x += float_0;
output.pos.y += float_1;
output.pos.z += float_2;

9. Game에서 Material 사용

  • 기존 전역 Shader, Texture 사용 → Init()에서 설정한다.
  • Material 생성 후:
    • Shader, Texture, Float 값을 설정
    • Mesh에 Material를 설정

요약

항목설명
Input 개선GetKeyboardState() 사용
ConstantBuffer 개선Type 분리, 레지스터 자동 처리
Engine여러 ConstantBuffer 관리
Material 클래스Shader + Texture + Params 관리
Shader 수정Material_Params 및 Texture2D 확장
Mesh 구조Material 기반 구조로 전환
Game 활용Material 구성 후 Mesh에 적용

느낀점 📝

느낀점 💡 Material에 대해 다루었는데 이전에는 왜 Material에 Shader, Texture 등을 넣어 관리해야 되는지에 대해 자세히 알아보지 않았는데 이 강의를 통해 깨달을 수 있었다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.