SlideShare a Scribd company logo
1 of 30
Download to read offline
이중 버퍼
Double Buffer
게임 프로그래밍 패턴
이데아 게임즈 손진화
의도
여러 순차 작업의 결과를 한 번에 보여주기 위해 사용
예를 들어, 게임 렌더링 시 여러 번에 걸쳐서 그린 내용
을 한번에 출력해서 보여줘야 한다
화면 찢김 현상 Tearing
프레임 버퍼에 그릴 내용을 입력하고 화면을 출력할 때
프레임 버퍼를 읽어서 보여준다 이 때, 프레임 버퍼에
내용을 미처 다 쓰기 전에 비디오 드라이버가 버퍼를
읽어서 화면을 출력하면 발생 할 수 있다
화면 찢김 현상 Tearing
요구사항
 비디오 드라이버는 작업이 완료된 버퍼를 읽을 수
있어야 한다
 그릴 데이터를 입력할 때는 하나씩 입력할 수 있어야
한다
1막 1장
요구 사항
1장에서 2장으로 넘어갈 때 무대가 바뀌어야 한다
끊김 없이 바꿀 수 없을까?
해결 방안
 무대를 2개 준비한다(A무대:1장, B무대:2장)
 1장이 끝나면 A무대 조명을 모두 끄고 B무대 조명
을 켠다 그리고 2장 시작
 2장을 하는 동안 A무대에 3장 무대를 준비한다
화면 찢김 현상 Tearing
해결방안
 프레임 버퍼를 두 개 준비해, 하나의 버퍼에는 현재
보여줘야 되는 값을 둬서 GPU가 언제든지
읽을 수 있도록 한다
 그동안 렌더링 코드는 다른 프레임 버퍼를 채운다
 렌더링 코드가 다 그린 후에는 버퍼를 교체한 뒤
비디오 하드웨어에게 두 번째 버퍼를 읽으라고
알려준다
 그리고 이전 버퍼에 다음 프레임에 출력할 화면을
그린다
패턴
 버퍼 클래스는 버퍼를 캡슐화 하여 현재 버퍼,
다음 버퍼 두 개의 버퍼를 가진다
 정보를 읽을 때는 현재의 버퍼를 알려준다
 정보를 쓸 때는 다음 버퍼에 수정한다
 정보를 다 쓰고 난 뒤에 현재 버퍼와 다음 버퍼를
교체 한다
 현재 버퍼는 새로운 다음 버퍼가 되어 재사용 된다
언제 쓸 것인가?
 순차적으로 변경해야 하는 상태가 있다
렌더링 코드가 하나씩 그림
 이 상태는 변경 도중에도 접근 가능해야 한다
GPU는 언제든 프레임 버퍼에 접근할 수 있어야
한다
언제 쓸 것인가?
 바깥 코드에서 작업 중인 상태에 접근 할 수 없어야
한다
렌더링 중인 버퍼에는 접근 하면 안 된다
 상태에 값을 쓰는 도중에도 기다리지 않고 바로
접근할 수 있어야 한다
GPU는 언제든 프레임 버퍼에 접근할 수 있어야
한다
주의사항
 교체 연산 자체에 시간이 걸린다
버퍼 교체 시 외부에서 두 버퍼에 접근 할 수 없어야
되기 때문에 버퍼 교체 연산이 빨라야 한다
 버퍼가 두 개 필요하다
그만큼 메모리를 더 요구한다
예제코드 1
예제코드 1
예제코드 1
예제코드 1 - 적용
예제코드 1 - 적용
예제코드 2
 서로를 계속 때리는 것을 구현하고자 함
 모든 배우가 한 번에 업데이트 되는 것처럼 보여야
한다
예제코드 2
예제코드 2
예제코드 2
예제코드 2
예제코드 2 – 입력순서 변경
예제코드 2 – 입력순서 변경
 다음 프레임에
적용될 값이 이번
프레임에 영향을
미치고 있다
 순서에 따라 결과가
달라진다
예제코드 2 - 적용
예제코드 2 - 적용
예제코드 2 - 적용
디자인 결정
버퍼를 어떻게 교체할 것인가?
포인터나 레퍼런스 교체
 빠르다
 버퍼 코드 밖에서는 메모리를 포인터로
가질 수 없다
항상 현재의 버퍼를 가리키는 메모리를 읽어야 한다
 버퍼에 남아있는 데이터는 직전 프레임이 아니라 2
프레임 전의 데이터이다
버퍼에 남은 데이터를 재사용 해야 할 때 주의
(ex. 모션 블러)
디자인 결정
버퍼를 어떻게 교체할 것인가?
데이터 복사
 다음 버퍼에는 딱 한 프레임 전의 데이터가
들어있다
이전 버퍼에서 좀 더 최신 데이터를 얻을 수 있다
 교체 시간이 더 걸린다
디자인 결정
얼마나 정밀하게 버퍼링 할 것인가?
버퍼가 한 덩어리라면
 간단히 교체할 수 있다
버퍼가 두 개 밖에 없기 때문에 포인터 연산으로
빠르게 교체 가능
디자인 결정
얼마나 정밀하게 버퍼링 할 것인가?
여러 객체가 각종 데이터를 들고 있다면
 교체가 느리다
전체 객체 컬렉션을 순회해야 하기 때문
프로젝트 적용 예

More Related Content

Similar to 게임 프로그래밍 패턴 8장

프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기
프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기
프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기Chang W. Doh
 
MGS 툴세미나
MGS 툴세미나MGS 툴세미나
MGS 툴세미나Bonex Gu
 
프론트엔드 개발자를 위한 Layer Model
프론트엔드 개발자를 위한 Layer Model프론트엔드 개발자를 위한 Layer Model
프론트엔드 개발자를 위한 Layer ModelHan Lee
 
[데브루키] Color space gamma correction
[데브루키] Color space gamma correction[데브루키] Color space gamma correction
[데브루키] Color space gamma correctionMinGeun Park
 
Optimizing the graphics_pipeline_
Optimizing the graphics_pipeline_Optimizing the graphics_pipeline_
Optimizing the graphics_pipeline_ozlael ozlael
 
20140514 team blender_v01 (Korean)
20140514 team blender_v01 (Korean)20140514 team blender_v01 (Korean)
20140514 team blender_v01 (Korean)Dongho Kim
 
다음 팟인코더 개선방안(최종)
다음 팟인코더 개선방안(최종)다음 팟인코더 개선방안(최종)
다음 팟인코더 개선방안(최종)hahahiho
 
모바일 게임 최적화
모바일 게임 최적화 모바일 게임 최적화
모바일 게임 최적화 tartist
 
Ndc2013 정리(upload버전)
Ndc2013 정리(upload버전)Ndc2013 정리(upload버전)
Ndc2013 정리(upload버전)Minsu Park
 
게임프로젝트에 적용하는 GPGPU
게임프로젝트에 적용하는 GPGPU게임프로젝트에 적용하는 GPGPU
게임프로젝트에 적용하는 GPGPUYEONG-CHEON YOU
 
Android mediacodec
Android mediacodecAndroid mediacodec
Android mediacodecTaehwan kwon
 
programming with GDB
programming with GDBprogramming with GDB
programming with GDBNakCheon Jung
 
증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임Junhee Han
 
멀티스레드 렌더링 (Multithreaded rendering)
멀티스레드 렌더링 (Multithreaded rendering)멀티스레드 렌더링 (Multithreaded rendering)
멀티스레드 렌더링 (Multithreaded rendering)Bongseok Cho
 
윈도우 매니저 스터디: 1. 윈도우 매니저 출력
윈도우 매니저 스터디: 1. 윈도우 매니저 출력윈도우 매니저 스터디: 1. 윈도우 매니저 출력
윈도우 매니저 스터디: 1. 윈도우 매니저 출력nemoux
 
증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임Junhee Han
 

Similar to 게임 프로그래밍 패턴 8장 (20)

프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기
프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기
프론트엔드 개발자를 위한 크롬 렌더링 성능 인자 이해하기
 
MGS 툴세미나
MGS 툴세미나MGS 툴세미나
MGS 툴세미나
 
프론트엔드 개발자를 위한 Layer Model
프론트엔드 개발자를 위한 Layer Model프론트엔드 개발자를 위한 Layer Model
프론트엔드 개발자를 위한 Layer Model
 
[데브루키] Color space gamma correction
[데브루키] Color space gamma correction[데브루키] Color space gamma correction
[데브루키] Color space gamma correction
 
Optimizing the graphics_pipeline_
Optimizing the graphics_pipeline_Optimizing the graphics_pipeline_
Optimizing the graphics_pipeline_
 
20140514 team blender_v01 (Korean)
20140514 team blender_v01 (Korean)20140514 team blender_v01 (Korean)
20140514 team blender_v01 (Korean)
 
다음 팟인코더 개선방안(최종)
다음 팟인코더 개선방안(최종)다음 팟인코더 개선방안(최종)
다음 팟인코더 개선방안(최종)
 
Android camera basics
Android camera basicsAndroid camera basics
Android camera basics
 
모바일 게임 최적화
모바일 게임 최적화 모바일 게임 최적화
모바일 게임 최적화
 
Ndc2013 정리(upload버전)
Ndc2013 정리(upload버전)Ndc2013 정리(upload버전)
Ndc2013 정리(upload버전)
 
게임프로젝트에 적용하는 GPGPU
게임프로젝트에 적용하는 GPGPU게임프로젝트에 적용하는 GPGPU
게임프로젝트에 적용하는 GPGPU
 
Android mediacodec
Android mediacodecAndroid mediacodec
Android mediacodec
 
Devtree illu
Devtree illuDevtree illu
Devtree illu
 
programming with GDB
programming with GDBprogramming with GDB
programming with GDB
 
증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임
 
Gametech2015
Gametech2015Gametech2015
Gametech2015
 
멀티스레드 렌더링 (Multithreaded rendering)
멀티스레드 렌더링 (Multithreaded rendering)멀티스레드 렌더링 (Multithreaded rendering)
멀티스레드 렌더링 (Multithreaded rendering)
 
윈도우 매니저 스터디: 1. 윈도우 매니저 출력
윈도우 매니저 스터디: 1. 윈도우 매니저 출력윈도우 매니저 스터디: 1. 윈도우 매니저 출력
윈도우 매니저 스터디: 1. 윈도우 매니저 출력
 
Node.js in Flitto
Node.js in FlittoNode.js in Flitto
Node.js in Flitto
 
증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임증강현실을 통한 두더지잡기 게임
증강현실을 통한 두더지잡기 게임
 

More from 진화 손

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf진화 손
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf진화 손
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates진화 손
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...진화 손
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions진화 손
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf진화 손
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types진화 손
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements진화 손
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library진화 손
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine진화 손
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules진화 손
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules진화 손
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)진화 손
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains진화 손
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers진화 손
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]진화 손
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts진화 손
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>진화 손
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr진화 손
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields진화 손
 

More from 진화 손 (20)

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields
 

게임 프로그래밍 패턴 8장

  • 1. 이중 버퍼 Double Buffer 게임 프로그래밍 패턴 이데아 게임즈 손진화
  • 2. 의도 여러 순차 작업의 결과를 한 번에 보여주기 위해 사용 예를 들어, 게임 렌더링 시 여러 번에 걸쳐서 그린 내용 을 한번에 출력해서 보여줘야 한다
  • 3. 화면 찢김 현상 Tearing 프레임 버퍼에 그릴 내용을 입력하고 화면을 출력할 때 프레임 버퍼를 읽어서 보여준다 이 때, 프레임 버퍼에 내용을 미처 다 쓰기 전에 비디오 드라이버가 버퍼를 읽어서 화면을 출력하면 발생 할 수 있다
  • 4. 화면 찢김 현상 Tearing 요구사항  비디오 드라이버는 작업이 완료된 버퍼를 읽을 수 있어야 한다  그릴 데이터를 입력할 때는 하나씩 입력할 수 있어야 한다
  • 5. 1막 1장 요구 사항 1장에서 2장으로 넘어갈 때 무대가 바뀌어야 한다 끊김 없이 바꿀 수 없을까? 해결 방안  무대를 2개 준비한다(A무대:1장, B무대:2장)  1장이 끝나면 A무대 조명을 모두 끄고 B무대 조명 을 켠다 그리고 2장 시작  2장을 하는 동안 A무대에 3장 무대를 준비한다
  • 6. 화면 찢김 현상 Tearing 해결방안  프레임 버퍼를 두 개 준비해, 하나의 버퍼에는 현재 보여줘야 되는 값을 둬서 GPU가 언제든지 읽을 수 있도록 한다  그동안 렌더링 코드는 다른 프레임 버퍼를 채운다  렌더링 코드가 다 그린 후에는 버퍼를 교체한 뒤 비디오 하드웨어에게 두 번째 버퍼를 읽으라고 알려준다  그리고 이전 버퍼에 다음 프레임에 출력할 화면을 그린다
  • 7. 패턴  버퍼 클래스는 버퍼를 캡슐화 하여 현재 버퍼, 다음 버퍼 두 개의 버퍼를 가진다  정보를 읽을 때는 현재의 버퍼를 알려준다  정보를 쓸 때는 다음 버퍼에 수정한다  정보를 다 쓰고 난 뒤에 현재 버퍼와 다음 버퍼를 교체 한다  현재 버퍼는 새로운 다음 버퍼가 되어 재사용 된다
  • 8. 언제 쓸 것인가?  순차적으로 변경해야 하는 상태가 있다 렌더링 코드가 하나씩 그림  이 상태는 변경 도중에도 접근 가능해야 한다 GPU는 언제든 프레임 버퍼에 접근할 수 있어야 한다
  • 9. 언제 쓸 것인가?  바깥 코드에서 작업 중인 상태에 접근 할 수 없어야 한다 렌더링 중인 버퍼에는 접근 하면 안 된다  상태에 값을 쓰는 도중에도 기다리지 않고 바로 접근할 수 있어야 한다 GPU는 언제든 프레임 버퍼에 접근할 수 있어야 한다
  • 10. 주의사항  교체 연산 자체에 시간이 걸린다 버퍼 교체 시 외부에서 두 버퍼에 접근 할 수 없어야 되기 때문에 버퍼 교체 연산이 빨라야 한다  버퍼가 두 개 필요하다 그만큼 메모리를 더 요구한다
  • 16. 예제코드 2  서로를 계속 때리는 것을 구현하고자 함  모든 배우가 한 번에 업데이트 되는 것처럼 보여야 한다
  • 21. 예제코드 2 – 입력순서 변경
  • 22. 예제코드 2 – 입력순서 변경  다음 프레임에 적용될 값이 이번 프레임에 영향을 미치고 있다  순서에 따라 결과가 달라진다
  • 26. 디자인 결정 버퍼를 어떻게 교체할 것인가? 포인터나 레퍼런스 교체  빠르다  버퍼 코드 밖에서는 메모리를 포인터로 가질 수 없다 항상 현재의 버퍼를 가리키는 메모리를 읽어야 한다  버퍼에 남아있는 데이터는 직전 프레임이 아니라 2 프레임 전의 데이터이다 버퍼에 남은 데이터를 재사용 해야 할 때 주의 (ex. 모션 블러)
  • 27. 디자인 결정 버퍼를 어떻게 교체할 것인가? 데이터 복사  다음 버퍼에는 딱 한 프레임 전의 데이터가 들어있다 이전 버퍼에서 좀 더 최신 데이터를 얻을 수 있다  교체 시간이 더 걸린다
  • 28. 디자인 결정 얼마나 정밀하게 버퍼링 할 것인가? 버퍼가 한 덩어리라면  간단히 교체할 수 있다 버퍼가 두 개 밖에 없기 때문에 포인터 연산으로 빠르게 교체 가능
  • 29. 디자인 결정 얼마나 정밀하게 버퍼링 할 것인가? 여러 객체가 각종 데이터를 들고 있다면  교체가 느리다 전체 객체 컬렉션을 순회해야 하기 때문