什么是Swap Chain【转自MSDN】

What Is a Swap Chain? (Direct3D 9)

A graphics adapter holds a pointer to a surface that represents the image being displayed on the monitor, called a front buffer. As the monitor is refreshed, the graphics card sends the contents of the front buffer to the monitor to be displayed. However, this leads to a problem when rendering real-time graphics. The heart of the problem is that monitor refresh rates are very slow in comparison to the rest of the computer. Common refresh rates range from 60 Hz (60 times per second) to 100 Hz. If your application is updating the front buffer while the monitor is in the middle of a refresh, the image that is displayed will be cut in half with the upper half of the display containing the old image and the lower half containing the new image. This problem is referred to as tearing.

Direct3D implements two options to avoid tearing:

  • An option to only allow updates of the monitor on the vertical retrace (or vertical sync) operation. A monitor typically refreshes its image by moving a light pin horizontally, zigzagging from the top left of the monitor and ending at the bottom right. When the light pin reaches the bottom, the monitor recalibrates the light pin by moving it back to the upper left so that the process can start again. This recalibration is called a vertical sync. During a vertical sync, the monitor is not drawing anything, so any update to the front buffer will not be seen until the monitor starts to draw again. The vertical sync is relatively slow; however, not slow enough to render a complex scene while waiting. What is needed to avoid tearing and be able to render complex scenes is a process called back buffering.
  • An option to use a technique called back buffering. Back buffering is the process of drawing a scene to an off-screen surface, called a back buffer. Note that any surface other than the front buffer is called an off-screen surface because it is never directly viewed by the monitor. By using a back buffer, an application has the freedom to render a scene whenever the system is idle (that is, no windows messages are waiting) without having to consider the monitor's refresh rate. Back buffering brings in an additional complication of how and when to move the back buffer to the front buffer.

The process of moving the back buffer to the front buffer is called surface flipping. Because the graphics card simply uses a pointer to a surface to represent the front buffer, a simple pointer change is all that is needed to set the back buffer to the front buffer. When an application asks Direct3D to present the back buffer to the front buffer, Direct3D simply "flips" the two surface pointers. The result is that the back buffer is now the new front buffer, and the old front buffer is the new back buffer. A surface flip is invoked whenever an application asks the Direct3D device to present the back buffer; however, Direct3D can be set up to queue the requests until a vertical sync occurs. This option is referred to as the Direct3D device's presentation interval. Note that the data in the new back buffer may not be reusable, depending on how an application specifies how Direct3D should handle surface flipping. Surface flipping is key in multimedia, animation, and game software; it is equivalent to the way you can do animation with a pad of paper. On each page, the artist changes the figures slightly, so that when you flip rapidly between sheets, the drawing appears animated.

Related Topics

Flipping Surfaces (Direct3D 9)
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(481) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
<think>嗯,用户问的是“swap是什么意思”,首先我需要确定他们在C++上下文中的具体需求。swap在编程中是一个常见术语,通常指交换两个变量的值。但根据用户的上下文,可能需要更详细的解释,尤其是C++标准库中的swap函数。 首先,我应该解释基本的swap概念,比如通过临时变量交换两个变量的值。这是最基础的手动实现方法,适用于所有支持赋值操作的类型。不过,用户可能更关心C++标准库中的std::swap,因为它更高效且通用。 接下来,需要说明std::swap的原理,即它如何通过移动语义(C++11之后)来高效交换值,避免不必要的拷贝。这对于大型对象尤其重要,比如包含大量数据的类或容器,使用移动语义可以显著提升性能。 然后,可能需要提到如何为自定义类型特化std::swap。比如,如果用户有一个自定义类,他们可能需要提供自己的swap实现,以便在交换时更高效,例如交换内部指针而不是逐个复制成员。这时候应该给出一个例子,展示如何通过友元函数或特化std::swap来实现。 另外,需要区分成员函数swap和std::swap的不同使用场景。比如,容器类如std::vector有自己的swap成员函数,可能比通用std::swap更高效,或者在使用ADL(参数依赖查找)时更合适。 还要注意C++11之后的移动语义对swap的影响,以及何时需要用户自定义swap函数。例如,当类管理资源时,自定义swap可以避免深拷贝,提升性能。 最后,总结swap的作用和应用场景,强调其在算法、容器操作和资源管理中的重要性。然后根据这些内容,生成相关问题,确保覆盖基本概念、标准库实现、自定义方法以及相关的最佳实践。</think><< 在C++中,`swap`指交换两个变量值的操作,常见实现方式如下: 1. **基础变量交换**(手动实现): ```cpp int a = 5, b = 10; int temp = a; a = b; b = temp; ``` 2. **标准库`std::swap`**(推荐方式): ```cpp #include <utility> std::swap(a, b); // 直接交换a和b的值 ``` **原理说明**: - `std::swap`通过移动语义(C++11后)实现高效交换: ```cpp template <class T> void swap(T& a, T& b) { T temp = std::move(a); a = std::move(b); b = std::move(temp); } ``` 3. **自定义类型优化**(特化swap): ```cpp class MyClass { int* data; public: friend void swap(MyClass& a, MyClass& b) noexcept { std::swap(a.data, b.data); // 仅交换指针 } }; ``` **典型应用场景**: - 避免深拷贝(如容器`vector::swap`) - 实现异常安全保证(copy-and-swap惯用法) - 排序算法中的元素交换操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值