Directly Mapping Texels to Pixels (Direct3D 9)

简介: 原文地址:Directly Mapping Texels to Pixels (Direct3D 9) When rendering 2D output using pre-transformed vertices, care must be ...

原文地址:Directly Mapping Texels to Pixels (Direct3D 9)

When rendering 2D output using pre-transformed vertices, care must be taken to ensure that each texel area correctly corresponds to a single pixel area, otherwise texture distortion can occur. By understanding the basics of the process that Direct3D follows when rasterizing and texturing triangles, you can ensure your Direct3D application correctly renders 2D output.

Illustration of a 6x6 resolution display

The preceding diagram shows pixels that are modeled as squares. In reality, however, pixels are dots, not squares. Each square in the preceding diagram indicates the area lit by the pixel, but a pixel is always just a dot at the center of a square. This distinction, though seemingly small, is important. A better illustration of the same display is shown in the following diagram.

Illustration of a display that consists of pixels

The preceding diagram correctly shows each physical pixel as a point in the center of each cell. The screen space coordinate (0, 0) is located directly at the top-left pixel, and therefore at the center of the top-left cell. The top-left corner of the display is therefore at (-0.5, -0.5) because it is 0.5 cells to the left and 0.5 cells up from the top-left pixel. Direct3D will render a quad with corners at (0, 0) and (4, 4), as shown in the following illustration.

Illustration of an outline of an unrasterized quad between (0, 0) and (4, 4)

The preceding illustration shows where the mathematical quad is in relation to the display, but does not show what the quad will look like once Direct3D rasterizes it and sends it to the display. In fact, it is impossible for a raster display to fill the quad exactly as shown because the edges of the quad do not coincide with the boundaries between pixel cells. In other words, because each pixel can only display a single color, each pixel cell is filled with only a single color; if the display were to render the quad exactly as shown, the pixel cells along the quad's edge would need to show two distinct colors: blue where covered by the quad and white where only the background is visible.

Instead, the graphics hardware is tasked with determining which pixels should be filled to approximate the quad. This process is called rasterization, and is detailed in Rasterization Rules (Direct3D 9). For this particular case, the rasterized quad is shown in the following illustration.

Illustration of an untextured quad drawn from (0,0) to (4,4)

Note that the quad passed to Direct3D has corners at (0, 0) and (4, 4), but the rasterized output (the preceding illustration) has corners at (-0.5,-0.5) and (3.5,3.5). Compare the preceding two illustrations for rendering differences. You can see that what the display actually renders is the correct size, but has been shifted by -0.5 cells in the x and y directions. However, except for multi-sampling techniques, this is the best possible approximation to the quad. (See the Antialias Sample for thorough coverage of multi-sampling.) Be aware that if the rasterizer filled every cell the quad crossed, the resulting area would be of dimension 5 x 5 instead of the desired 4 x 4.

If you assume that screen coordinates originate at the top-left corner of the display grid instead of the top-left pixel, the quad appears exactly as expected. However, the difference becomes clear when the quad is given a texture. The following illustration shows the 4 x 4 texture you'll map directly onto the quad.

Illustration of a 4x4 texture

Because the texture is 4 x 4 texels and the quad is 4 x 4 pixels, you might expect the textured quad to appear exactly like the texture regardless of the location on the screen where the quad is drawn. However, this is not the case; even slight changes in position influence how the texture is displayed. The following illustration shows how a quad between (0, 0) and (4, 4) is displayed after being rasterized and textured.

Illustration of a textured quad drawn from (0, 0) and (4, 4)

The quad drawn in the preceding illustration shows the textured output (with a linear filtering mode and a clamp addressing mode) with the superimposed rasterized outline. The rest of this article explains exactly why the output looks the way it does instead of looking like the texture, but for those who want the solution, here it is: The edges of the input quad need to lie upon the boundary lines between pixel cells. By simply shifting the x and y quad coordinates by -0.5 units, texel cells will perfectly cover pixel cells and the quad can be perfectly recreated on the screen. (The last illustration in this topic shows the quad at the corrected coordinates.)

The details of why the rasterized output only bears slight resemblance to the input texture are directly related to the way Direct3D addresses and samples textures. What follows assumes you have a good understanding of texture coordinate space And bilinear texture filtering.

Getting back to our investigation of the strange pixel output, it makes sense to trace the output color back to the pixel shader: The pixel shader is called for each pixel selected to be part of the rasterized shape. The solid blue quad depicted in an earlier illustration could have a particularly simple shader:

float4 SolidBluePS() : COLOR
{ 
	return float4( 0, 0, 1, 1 );
} 


For the textured quad, the pixel shader has to be changed slightly:

texture MyTexture;

sampler MySampler = 
sampler_state 
{ 
	Texture = <MyTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
	AddressU = Clamp;
	AddressV = Clamp;
};

float4 TextureLookupPS( float2 vTexCoord : TEXCOORD0 ) : COLOR
{
	return tex2D( MySampler, vTexCoord );
} 


That code assumes the 4 x 4 texture is stored in MyTexture. As shown, the MySampler texture sampler is set to perform bilinear filtering on MyTexture. The pixel shader gets called once for each rasterized pixel, and each time the returned color is the sampled texture color at vTexCoord. Each time the pixel shader is called, the vTexCoord argument is set to the texture coordinates at that pixel. That means the shader is asking the texture sampler for the filtered texture color at the exact location of the pixel, as detailed in the following illustration.

Illustration of sampling locations for texture coordinates

The texture (shown superimposed) is sampled directly at pixel locations (shown as black dots). Texture coordinates are not affected by rasterization (they remain in the projected screen-space of the original quad). The black dots show where the rasterization pixels are. The texture coordinates at each pixel are easily determined by interpolating the coordinates stored at each vertex: The pixel at (0,0) coincides with the vertex at (0, 0); therefore, the texture coordinates at that pixel are simply the texture coordinates stored at that vertex, UV (0.0, 0.0). For the pixel at (3, 1), the interpolated coordinates are UV (0.75, 0.25) because that pixel is located at three-fourths of the texture's width and one-fourth of its height. These interpolated coordinates are what get passed to the pixel shader.

The texels do not line up with the pixels in this example; each pixel (and therefore each sampling point) is positioned at the corner of four texels. Because the filtering mode is set to Linear, the sampler will average the colors of the four texels sharing that corner. This explains why the pixel expected to be red is actually three-fourths gray plus one-fourth red, the pixel expected to be green is one-half gray plus one-fourth red plus one-fourth green, and so on.

To fix this problem, all you need to do is correctly map the quad to the pixels to which it will be rasterized, and thereby correctly map the texels to pixels. The following illustration shows the results of drawing the same quad between (-0.5, -0.5) and (3.5, 3.5), which is the quad intended from the outset.

Illustration of a textured quad that matches the rasterized quad

The preceding illustration demonstrates that the quad (shown outlined from (-0.5, -0.5) to (3.5, 3.5)) exactly matches the rasterized area.

Summary

In summary, pixels and texels are actually points, not solid blocks. Screen space originates at the top-left pixel, but texture coordinates originate at the top-left corner of the texture's grid. Most importantly, remember to subtract 0.5 units from the x and y components of your vertex positions when working in transformed screen space in order to correctly align texels with pixels.

The following code is an example of offsetting the vertices of a 256 by 256 square to properly display a 256 by 256 texture in transformed screen space.

相关文章
|
缓存 Kubernetes API
K8S 性能优化 - K8S APIServer 调优
K8S 性能优化 - K8S APIServer 调优
|
编解码
如何让开源鸿蒙系统适配其它机型?
如何让开源鸿蒙系统适配其它机型?
981 0
|
Linux KVM 虚拟化
云计算——虚拟化层架构
云计算——虚拟化层架构
1144 0
|
2月前
|
人工智能 编解码 自然语言处理
2026阿里云双11大促|Qwen-image-3.0重磅升级:新版通义千问核心特性、技术架构与实战教程全解析
2026年阿里云双十一大促,不只是一场促销——它是你以最低成本拥抱Qwen-image-3.0图像生成能力、Qwen3.7-Max智能体基座、Qwen3.8-Max-Preview旗舰模型的黄金窗口。从4.5K Token超长提示词到万亿参数MoE架构,从10px小字渲染到35小时自主任务执行,阿里云百炼正在将"大模型能力民主化"从口号变为现实。 立即访问阿里云百炼大模型服务平台页面,领取你的7000万Tokens免费额度,开启AI生产力的全新篇章!
592 2
|
2月前
|
人工智能 安全 架构师
【AI时代软件项目管理系列】9. AI 生成需求文档靠谱吗?项目经理应该如何审核
AI 可以快速生成结构完整的需求文档,但完整并不等于可信。需求审核应重点确认来源、业务真实性、项目范围、规则一致性、异常边界和验收标准,并区分已确认需求、待确认项和 AI 建议。项目团队还需要明确 BA、客户、架构、测试和项目经理的审核责任,保证未经确认的 AI 初稿不能直接进入需求基线。
302 2
|
3月前
|
数据采集 JSON 数据挖掘
Temperature 原理详解:大模型随机性参数与场景设置
Temperature 是调节大模型输出随机性的生成参数。它影响模型按概率分布预测下一个词,了解代码生成温度设置和数据抽取取值示例,有助于按任务平衡稳定性与变化空间。
667 0
|
11月前
|
弹性计算 编解码 大数据
2025年新版阿里云服务器配置价格表及相关活动信息整理说明
2026年阿里云服务器价格及相关活动信息总结。 1、2026年阿里云轻量应用服务器价格表。 2、2026年阿里云ECS云服务器价格表。 3、2026年阿里云GPU云服务器价格表。
1185 3
|
12月前
|
弹性计算 数据库 异构计算
如何快速查询阿里云服务器价格?云服务器价格计算器使用指南
阿里云服务器由于其弹性伸缩的特性,价格受到地域、CPU与内存配置、架构、实例规格、数据盘大小及公网带宽等多重因素影响,价格计算器可以帮助我们自助选配和估算价格。以下是如何通过阿里云的价格计算器快速查询您所需云服务器价格以及各个收费项目明细的详细步骤。
1225 6
|
存储 消息中间件 NoSQL
跟着大厂学架构01:如何利用开源方案,复刻B站那套“永不崩溃”的评论系统?
本文基于B站技术团队分享的《B站评论系统的多级存储架构》,解析其在高并发场景下的设计精髓,并通过开源技术栈(MySQL、Redis、Java)复刻其实现。文章深入讲解了多级存储、数据同步、容灾降级等关键设计,并附有完整代码实现,助你掌握大厂架构设计之道。
868 0
|
API Windows
微软新一代输入法框架 TSF - Text Service Framework 小小的研究
原文:微软新一代输入法框架 TSF - Text Service Framework 小小的研究 虽说是转载的,但是其中,有很多我自己的评论,我会用红色的字标出来,参考的博文有: TSF架构:http://blog.
5794 0