D3D处理2D图像:2D Quad坐标系介绍(1)

Direct3D

Direct3D是Windows平台上开发硬件加速的程序一个架构,它提供了

  • 图形处理和渲染加速
  • 视频编解码加速
  • 并行计算加速

Direct3D和OpenGL非常相似,很多概念、术语和流程基本上是相通的,在显卡(GPU)内部的加速原理基本上差不多,它不仅是Windows和XBox游戏开发的渲染基本技术架构,我们还能利用显卡(GPU)强大的计算能力,为高清视频、图像和图形编解码,渲染和特效处理提供实时和流畅的处理能力,是Windows平台上视频和图像处理进阶的必备编程技术。
本系列主要介绍如何用D3D处理2D视频和图像,所有演示程序和代码均基于Direct3D11。

Direct3D坐标系

Direct3D提供了这么几种坐标系,这些对于D3D rendering pipeline以及shader的代码阅读和编写来说非常重要,毕竟GPU端的代码的调试和优化相对于CPU端来说是比较麻烦的,对于基本概念的不了解会造成代码编写和运行效率的低下。

Pixel Coordinate System

这个坐标系的原点(0, 0)在Render Target(渲染目标)的左上方,这个坐标系与图像和视频decoded buffer是一致的,大部分图片和主流视频都是用这个坐标系来表示图像像素信息,下面这张图片很好的描述了它:
图一

图一

从这张图可以看到,因为D3D是矢量图,所以像素的大小和位置可以是小数,而且它们也是连续的,一个像素方框的左上方是整数坐标系,比如(0, 0), (1, 4)等, 一个像素的中心是左上方整数坐标加上(0.5, 0.5)的偏移量。
可以把它理解成buffer坐标系。

Texel Coordinate System

在2D Quad Render Target里面可以理解填充素材(Texture)坐标,也称作uv坐标,主要用来描述素材中哪部分区域用来填充到Render Target中。左上方的坐标为(0, 0),然后右下方normlized坐标为(1, 1):
在这里插入图片描述

图二
在HLSL shader中一般用TEXCOORD来申明。 可以把它理解重素材填充坐标系

NDC(Normalized Device Coordinates)

中文好像翻译成标准化设备坐标,原点(0, 0)在Render Target的中心,左上角坐标为(-1, 1),右下角为(1, -1)。在D3D中一般用来描述点在渲染区域的坐标,比如顶点(Vertex)的坐标。也可以在图一中看到对应的图示。在D3D中,Shader中的SV_Position语义就用在这个坐标系来描述指定屏幕空间坐标(screen space coordinates)。
可以把它理解成渲染坐标系。

相关代码

有了上面这些基本概念之后,来看一段用HSL写的Vetex Shader的代码:

struct PS_INPUT
{
	float4 Pos : SV_POSITION;
	float2 Tex : TEXCOORD;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS(uint vI : SV_VERTEXID)
{
	PS_INPUT output = (PS_INPUT)0;
	float2 texcoord = float2(vI & 1, vI >> 1);
	output.Pos = float4((texcoord.x - 0.5f) * 2.0f, -(texcoord.y - 0.5f) * 2.0f, 0.0f, 1.0f);
	output.Tex = texcoord;
	return output;
}

从这段代码可以看出, 这个vertex shader的输出PS_INPUT用来告诉pixel shader的输入坐标信息,包括素材(ShaderResourceView)中哪个区域(Tex: TEXCOORD, Texel Coordinate System)应该填充到哪个渲染区域(Render Target)(Pos: SV_POSITION, NDC),然后Vertex Shader就根据设计的场景来构建素材内容和渲染区域,很多转场动画都是通过调整这个输出来达到要想的一些效果。
再来看一段Pixel Shader的代码:

//--------------------------------------------------------------------------------------
// ScreenPS.hlsl
//--------------------------------------------------------------------------------------
Texture2D txInput : register(t0);

SamplerState GenericSampler : register(s0);

struct PS_INPUT
{
	float4 Pos : SV_POSITION;
	float2 Tex : TEXCOORD;
};

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(PS_INPUT input) : SV_Target
{
	return txInput.Sample(GenericSampler, input.Tex);
}

这段代码中Pixel Shader的输入为PS_INPUT input, pos: SV_POSITION为在NDC坐标系要处理的某点的D3D坐标(x, y, z, w),而Tex : TEXCOORD则用来描述填充素材(Texture)上对应某点填充到pos:SV_POSITION对应的这个点。而语句txInput.Sample(GenericSampler, input.Tex)则是根据Texel Coordinate System坐标系统从素材(Texture)上取一点的具体信息,比如颜色信息(Blue, Green, Red和Alpha)值,然后把它渲染到Render Target中。当然在这个最简单的场景中, Pixel Coordinate System和NDC坐标并没有参与到具体运算逻辑来影响输出效果。

结论

通过上面的介绍,应该能对用于2D场景中各个坐标系有个粗略的了解,在后面的文章中会进一步讲解这三个坐标如何用在一些实际场景中。

原创不易,如果对你有帮助,望点赞和关注。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book describes the Direct3D graphics pipeline, from presentation of scene data to pixels appearing on the screen. The book is organized sequentially following the data °ow through the pipeline from the application to the image displayed on the monitor. Each major section of the pipeline is treated by a part of the book, with chapters and subsections detailing each discrete stage of the pipeline. This section summarizes the contents of the book. Part I begins with a review of basic concepts used in 3D computer graphics and their representations in Direct3D. The IDirect3D9 interface is introduced and device selection is described. The IDirect3DDevice9 interface is introduced and an overview of device methods and internal state is given. Finally, a basic framework is given for a 2D application. Chapter 1 begins with an overview of the entire book. A review is given of display technology and the important concept of gamma correction. The representation of color in Direct3D and the macros for manipulating color values are described. The relevant mathematics of vectors, geometry and matrices are reviewed and summarized. A summary of COM and the IUnknown interface is COM: Component Object Model given. Finally, the coding style conventions followed in this book are presented along with some useful C++ coding techniques. Chapter 2 describes the Direct3D object. Every application instantiates this object to select a device from those available. Available devices advertise their location in the Win32 virtual desktop and their capabilities to applications 34 CHAPTER 1. INTRODUCTION through the Direct3D object. Selecting a device from those available and exam- ining a device's capabilities are described. Multiple monitor considerations are also discussed. Chapter 3 describes the Direct3D device object which provides access to the rendering pipeline. The device is the interface an application will use most often and it has a large amount of internal state that controls every stage of the rendering pipeline. This chapter provides a high-level overview of the device and its associated internal state. Detailed discussion of the device state appears throughout the rest of the book. Chapter 4 describes the basic architecture of a typical Direct3D application. Every 3D application can use 2D operations for manipulating frame bu®er con- tents directly. An application can run in full-screen or windowed modes and the di®erences are presented here. The handling of Windows messages and a ba- sic display processing loop are presented. At times it may be convenient to use GDI in a Direct3D application window and a method for mixing these two Win- dows subsystems is presented. Almost every full-screen application will want to use the cursor management provided by the device. Device color palettes and methods for gamma correction are presented. Part II describes the geometry processing portion of the graphics pipeline. The application delivers scene data to the pipeline in the form of geometric primitives. The pipeline processes the geometric primitives through a series of stages that results in pixels displayed on the monitor. This part describes the start of the pipeline where the processing of geometry takes place. Chapter 5 describes how to construct a scene representing the digital world that is imaged by the imaginary camera of the device. A scene consists of a collection of models drawn in sequence. Models are composed of a collection of graphic primitives. Graphic primitives are composed from streams of vertex and index data de¯ning the shape and appearance of objects in the scene. Vertices and indices are stored in resources created through the device. Chapter 6 covers vertex transformations, vertex blending and user-de¯ned clipping planes. With transformations, primitives can be positioned relative to each other in space. Vertex blending, also called \skinning", allows for smooth mesh interpolation. User-de¯ned clipping planes can be used to provide cut away views of primitives. Chapter 7 covers viewing with a virtual camera and projection onto the viewing plane which is displayed as pixels on the monitor. After modeling, objects are positioned relative to a camera. Objects are then projected from 3D camera space into the viewing plane for conversion into 2D screen pixels. Chapter 8 describes the lighting of geometric primitives. The lighting model is introduced and the supported shading algorithms and light types are de- scribed. Chapter 9 covers programmable vertex shading. Programmable vertex shaders can process the vertex data streams with custom code, producing a single ver- tex that is used for rasterization. The vertex shading machine architecture and instruction set are presented. Part III covers the rasterization portion of the pipeline where geometry is1.1. OVERVIEW 5 converted to a series of pixels for display on the monitor. Geometric primitives are lit based on the lighting of their environment and their material properties. After light has been applied to a primitive, it is scan converted into pixels for processing into the frame bu®er. Textures can be used to provide detailed surface appearance without extensive geometric modeling. Pixel shaders can be used to provide custom per-pixel appearance processing instead of the ¯xed- function pixel processing provided by the stock pipeline. Finally, the pixels generated from the scan conversion process are incorporated into the render target surface by the frame bu®er. Chapter 10 describes the scanline conversion of primitives into pixel frag- ments. Lighting and shading are used to process vertex positions and their associated data into a series of pixel fragments to be processed by the frame bu®er. Chapter 11 describes textures and volumes. Textures provide many e±cient per-pixel e®ects and can be used in a variety of manners. Volumes extend texture images to three dimensions and can be used for a volumetric per-pixel rendering e®ects. Chapter 13 describes programmable pixel shaders. Programmable pixel shaders combine texture map information and interpolated vertex information to produce a source pixel fragment. The pixel shading machine architecture and instruction set are presented. Chapter 14 describes how fragments are processed into the frame bu®er. After pixel shading, fragments are processed by the fog, alpha test, depth test, stencil test, alpha blending, dither, and color channel mask stages of the pipeline before being incorporated into the render target. A render target is presented for display on the monitor and video scan out. Part IV covers the D3DX utility library. D3DX provides an implementation of common operations used by Direct3D client programs. The code in D3DX consists entirely of client code and no system components. An application is free to reimplement the operations provided by D3DX, if necessary. Chapter 15 introduces D3DX and summarizes features not described else- where. Chapter 16 describes the abstract data types provided by D3DX. D3DX provides support for RGBA color, point, vector, plane, quaternion, and matrix data types. Chapter 17 describes the helper COM objects provided by D3DX. D3DX provides a matrix stack object to assist in rendering frame hierarchies, a font object to assist in the rendering of text, a sprite object to assist in the rendering of 2D images, an object to assist in rendering to a surface or an environment map and objects for the rendering of special e®ects. Chapter 19 describes the mesh objects provided by D3DX. The mesh objects provided by D3DX encompass rendering of indexed triangle lists as well as progressive meshes, mesh simpli¯cation and skinned meshes. Chapter 21 describes the X ¯le format with the ¯le extension .x. The X ¯le format provides for extensible hierarchical storage of data objects with object instancing.6 CHAPTER 1. INTRODUCTION Part V covers application level considerations. This part of the book de- scribes issues that are important to applications but aren't strictly part of the graphics pipeline. Debugging strategies for applications are presented. Almost all Direct3D applications will be concerned with performance; API related per- formance issues are discussed here. Finally, installation and deployment issues for Direct3D applications are discussed. Chapter 22 describes debugging strategies for Direct3D applications. This includes using the debug run-time for DirectX 9.0c, techniques for debugging full-screen applications and remote debugging. Chapter 23 covers application performance considerations. All real devices have limitations that a®ect performance. A general consideration of how the pipeline state a®ects performance is given. Chapter 24 covers application installation and setup. Appendix A provides a guided tour of the DirectX SDK materials.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值