DirectX9 SDK Samples(20) HDRDemo Sample(1)

HDR-DEMO高动态范围渲染 高手做的HDR渲染,包含很多有用的SHader.值得借鉴 立即下载

HDR,这里有一篇写得不错的:http://www.zwqxin.com/archives/shaderglsl/review-high-dynamic-range.html

原理可以参考上面的博文~那就进入代码。

首先我们看看Luminance部分,也就是计算平均亮度的部分。

LPDIRECT3DPIXELSHADER9 g_pLumDispPS = NULL;             // PShader used to display the debug panel

LPDIRECT3DPIXELSHADER9 g_pLum1PS = NULL;             // PShader that does a 2x2 downsample and convert to greyscale
LPD3DXCONSTANTTABLE g_pLum1PSConsts = NULL;             // Interface to set the sampling points for the above PS

LPDIRECT3DPIXELSHADER9 g_pLum3x3DSPS = NULL;             // The PS that does each 3x3 downsample operation
LPD3DXCONSTANTTABLE g_pLum3x3DSPSConsts = NULL;             // Interface for the above PS

static const DWORD g_dwLumTextures = 6;                // How many luminance textures we're creating
// Be careful when changing this value, higher than 6 might
// require you to implement code that creates an additional
// depth-stencil buffer due to the luminance textures dimensions
// being greater than that of the default depth-stencil buffer.
LPDIRECT3DTEXTURE9 g_pTexLuminance[ g_dwLumTextures ];         // An array of the downsampled luminance textures
D3DFORMAT g_fmtHDR = D3DFMT_UNKNOWN;   // Should be either G32R32F or G16R16F depending on the hardware
上面的是用到的比较重要的变量。

下面将逐个对函数代码进行阅读。

//--------------------------------------------------------------------------------------
//  CreateResources( )
//
//      DESC:
//          This function creates the necessary texture chain for downsampling the
//          initial HDR texture to a 1x1 luminance texture.
函数描述。

    //[ 2 ] CREATE HDR RENDER TARGETS
    //-------------------------------
    int iTextureSize = 1;
    for( int i = 0; i < Luminance::g_dwLumTextures; i++ )
    {

        // Create this element in the array
        V( pDevice->CreateTexture( iTextureSize, iTextureSize, 1,
                                   D3DUSAGE_RENDERTARGET, Luminance::g_fmtHDR,
                                   D3DPOOL_DEFAULT, &Luminance::g_pTexLuminance[i], NULL ) );
        // Increment for the next texture
        iTextureSize *= 3;

    }
创建计算平均亮度所需要的纹理。

    V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Shader Code\\Luminance.psh" ) );
    V( D3DXCompileShaderFromFile(str, NULL, NULL, "LuminanceDisplay", "ps_2_0", 0, &pCode, NULL, NULL ) );
编译亮度展示着色器。
    V( pDevice->CreatePixelShader( reinterpret_cast< DWORD* >( pCode->GetBufferPointer() ),
                                   &Luminance::g_pLumDispPS ) );
。。。。。接下来照葫芦画瓢,创建两个减取样着色器,一个用作转换为灰阶,一个计算平均值来进行减采样。这个函数就完成任务了。


下一个函数~

//--------------------------------------------------------------------------------------
//  MeasureLuminance( )
//
//      DESC:
//          This is the core function for this particular part of the application, it's
//          job is to take the previously rendered (in the 'HDRScene' namespace) HDR
//          image and compute the overall luminance for the scene. This is done by
//          repeatedly downsampling the image until it is only 1x1 in size. Doing it
//          this way (pixel shaders and render targets) keeps as much of the work on
//          the GPU as possible, consequently avoiding any resource transfers, locking
//          and modification.
    HDRScene::GetOutputTexture( &pSourceTex );
    pDestTex = Luminance::g_pTexLuminance[ Luminance::g_dwLumTextures - 1 ];

    pDevice->SetRenderTarget( 0, pDestSurf );
    pDevice->SetTexture( 0, pSourceTex );

    pDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
    pDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
首先得到整个场景作为源纹理,并得到最后一个亮度纹理作为输出。

    //[ 2 ] RENDER AND DOWNSAMPLE THE HDR TEXTURE
    //      TO THE LUMINANCE MAP.
    //-------------------------------------------
    // Set which shader we're going to use. g_pLum1PS corresponds
    // to the 'GreyScaleDownSample' entry point in 'Luminance.psh'.
    pDevice->SetPixelShader( Luminance::g_pLum1PS );

    // We need to compute the sampling offsets used for this pass.
    // A 2x2 sampling pattern is used, so we need to generate 4 offsets.
    D3DXVECTOR4 offsets[4];

    // Find the dimensions for the source data
    D3DSURFACE_DESC srcDesc;
    pSourceTex->GetLevelDesc( 0, &srcDesc );

    // Because the source and destination are NOT the same sizes, we
    // need to provide offsets to correctly map between them.
    float sU = ( 1.0f / static_cast< float >( srcDesc.Width ) );
    float sV = ( 1.0f / static_cast< float >( srcDesc.Height ) );

    // The last two components (z,w) are unused. This makes for simpler code, but if
    // constant-storage is limited then it is possible to pack 4 offsets into 2 float4's
    offsets[0] = D3DXVECTOR4( -0.5f * sU, 0.5f * sV, 0.0f, 0.0f );
//省略其他offset设置
    // Set the offsets to the constant table
    Luminance::g_pLum1PSConsts->SetVectorArray( pDevice, "tcLumOffsets", offsets, 4 );

    // With everything configured we can now render the first, initial, pass
    // to the luminance textures.
    RenderToTexture( pDevice );
这一个过程每计算一次平均亮度只需要进行一次,因为转换为灰阶只需要进行一次。下面先看看RenderToTexture再回来继续下面的亮度计算。

//--------------------------------------------------------------------------------------
//  RenderToTexture( )
//
//      DESC:
//          A simple utility function that draws, as a TL Quad, one texture to another
//          such that a pixel shader (configured before this function is called) can
//          operate on the texture. Used by MeasureLuminance() to perform the
//          downsampling and filtering.
    // To correctly map from texels->pixels we offset the coordinates
    // by -0.5f:
    float fWidth = static_cast< float >( desc.Width ) - 0.5f;
    float fHeight = static_cast< float >( desc.Height ) - 0.5f;

    // Now we can actually assemble the screen-space geometry
    Luminance::TLVertex v[4];

    v[0].p = D3DXVECTOR4( -0.5f, -0.5f, 0.0f, 1.0f );
    v[0].t = D3DXVECTOR2( 0.0f, 0.0f );

    v[1].p = D3DXVECTOR4( fWidth, -0.5f, 0.0f, 1.0f );
    v[1].t = D3DXVECTOR2( 1.0f, 0.0f );

    v[2].p = D3DXVECTOR4( -0.5f, fHeight, 0.0f, 1.0f );
    v[2].t = D3DXVECTOR2( 0.0f, 1.0f );

    v[3].p = D3DXVECTOR4( fWidth, fHeight, 0.0f, 1.0f );
    v[3].t = D3DXVECTOR2( 1.0f, 1.0f );

    // Configure the device and render..
    pDev->SetVertexShader( NULL );
    pDev->SetFVF( Luminance::FVF_TLVERTEX );
    pDev->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, v, sizeof( Luminance::TLVertex ) );
渲染代码挺简单,注意texel和pixel之间的坐标转换就好了。下面回到MeasureLuminance。
    //[ 3 ] SCALE EACH RENDER TARGET DOWN
    //      The results ("dest") of each pass feeds into the next ("src")
    //-------------------------------------------------------------------
    for( int i = ( Luminance::g_dwLumTextures - 1 ); i > 0; i-- )
    {

        // Configure the render targets for this iteration
        pSourceTex = Luminance::g_pTexLuminance[ i ];
        pDestTex = Luminance::g_pTexLuminance[ i - 1 ];
        if( FAILED( pDestTex->GetSurfaceLevel( 0, &pDestSurf ) ) )
        {
//省略
        }

        pDevice->SetRenderTarget( 0, pDestSurf );
        pDevice->SetTexture( 0, pSourceTex );
//省略
        // Because each of these textures is a factor of 3
        // different in dimension, we use a 3x3 set of sampling
        // points to downscale.
        D3DSURFACE_DESC srcTexDesc;
        pSourceTex->GetLevelDesc( 0, &srcTexDesc );

        // Create the 3x3 grid of offsets
        D3DXVECTOR4 DSoffsets[9];
        int idx = 0;
        for( int x = -1; x < 2; x++ )
        {
            for( int y = -1; y < 2; y++ )
            {
                DSoffsets[idx++] = D3DXVECTOR4(
                    static_cast< float >( x ) / static_cast< float >( srcTexDesc.Width ),
                    static_cast< float >( y ) / static_cast< float >( srcTexDesc.Height ),
                    0.0f,   //unused
                    0.0f    //unused
                    );
            }
        }
        // Set them to the current pixel shader
        pDevice->SetPixelShader( Luminance::g_pLum3x3DSPS );
        Luminance::g_pLum3x3DSPSConsts->SetVectorArray( pDevice, "tcDSOffsets", DSoffsets, 9 );

        // Render the display to this texture
        RenderToTexture( pDevice );
//省略
    }
接下来是DisplayLuminance函数

//--------------------------------------------------------------------------------------
//  DisplayLuminance( )
//
//      DESC:
//          This function is for presentation purposes only - and isn't a *required*
//          part of the HDR rendering pipeline. It draws the 6 stages of the luminance
//          calculation to the appropriate part of the screen.
所以我就不说了。。。Next

//--------------------------------------------------------------------------------------
//  GetLuminanceTexture( )
//
//      DESC:
//          The final 1x1 luminance texture created by the MeasureLuminance() function
//          is required as an input into the final image composition. Because of this
//          it is necessary for other parts of the application to have access to this
//          particular texture.
这个函数是输出最后的1X1结果

HRESULT GetLuminanceTexture( IDirect3DTexture9** pTex )
{

    // [ 0 ] ERASE ANY DATA IN THE INPUT
    //----------------------------------
    SAFE_RELEASE( *pTex );

    // [ 1 ] COPY THE PRIVATE REFERENCE
    //---------------------------------
    *pTex = Luminance::g_pTexLuminance[ 0 ];

    // [ 2 ] INCREMENT THE REFERENCE COUNT..
    //--------------------------------------
    ( *pTex )->AddRef();

    return S_OK;

}

这样我们就完成了HDR三部分的其中之一(Tone Mapping,另外还有Bloom和最终的合成)。

dx9_2d_demo_game directx9实现2D游戏例程 Windows Develop dx9_2d_demo_game directx9实现2D游戏例程 是一个用Direct9 写的一个2D 飞机游戏 立即下载

相关推荐

D3D DX9 游戏demo源码 场景loading 人物骨骼动画

上学的时候写的一个demo 记得写了好久.. 很多封装类可以直接用。基本功能都有了 自我感觉还算完善~可以选人 可以升级- - 天空盒 碰撞 拾取 骨骼动画 模型 等等

DirectX9 SDK Samples(20) HDRDemo Sample(3)

HDR需要对源场景进行处理,因此在渲染初始源场景的时候,也要创建一个纹理,并且将场景渲染到这个纹理上,然后进行HDR处理。这个例子中,上述工作由HDRScene完成。关于渲染到纹理,这篇博文讲得很清楚:http://www.cnblogs.com/graphics/archive/2011/04/23/2024294.html 那么到此为止,我们明白了如何进行计算平均亮度,如何进行Bloom,如

972

DirectX9.0实现游戏DEMO

一个以DX9开始的3D游戏DEMO,使用了地形LOG管理、粒子、骨骼动画等多种3D游戏技术,适合对3D游戏设计有兴趣者参考。

HDR视频完全解密

转自:http://www.icpcw.com/Parts/Peripheral/dnwspc/3271/327191.htm 技术总是在不断进步中,特别是针对我们的视觉体验,各个厂商都在不遗余力地开发着新技术来满足观众们的需求。在H.265、10bit、4K已经越来越难引起消费者关注的时候,HDR的横空出世,再一次引发了新一轮的图像话题。不夸张地说,在2016年,无论是平板电视、4K蓝

haima1998的专栏 1万+

directx9 dxsample

directx9 dxsample

directx9 sdk 完整版

完整版directx9.0全部开发工具和文件,包括sdk说明

DirectX9 SDK Samples(20) HDRDemo Sample(2)

接下来进入第二部分,Bloom,SDK里称为post-processing effect。代码所在文件为PostProcess.cpp。 LPDIRECT3DTEXTURE9 g_pBrightPassTex = NULL; // The results of performing a bright pass on the original HDR imagery L

1056

Shadow Map在DirectX9_0 SDK Sample 的实现方法

Shadow Map在DirectX9_0 SDK Sample 的实现方法

Directx9 sdk中有用的例子-Demo说明

转自http://hi.baidu.com/lf460/item/f13775134d5f22affeded502 directx9sdk中有用的例 C:\DXSDK\Samples\C++\DirectShow\VMR9\VMR9Allocator\ 一个avi视频在三维空间中旋转、变换! C:\DXSDK\Samples\C++\DirectShow\C

sherry_0009的专栏 2467

在DirectX中支持HDR和高级颜色设置

windows 10下如何支持HDR,HDR更好的 动态范围 + 更好的 色域空间+更好的 位深精度,讲述了在direct11下如何支持HDR

B站充电视频下载器(需配合会员Cookie使用,仅供学习交流,Python)

这个程序是一个用于下载B站充电视频的工具,依赖于用户提供的会员Cookies。

qq_40753021的博客 2万+

Direct3D 9的介绍以及Demo演示

Direct3D 9的介绍以及Demo演示

森明帮大于黑虎帮的博客 3417

Android 13/14/15 默认授权应用权限的实现方法

Android版本修改方式实现原理13/14修改Permission.java将所有权限标记为Normal权限15修改DeviceIdleController.java + DefaultPermissionGrantPolicy.java监听应用安装并主动授权。

m0_60898338的博客 2908

DirectX9 SDK Samples(24) HDRLighting Sample

这是DX9 SDK Sample中最后一个关于HDR的例子了。看过了以前的例子,我感觉HDR实际上可以改动的地方还不少(注意,是改动不是改良)。但无论怎么变,求平均亮度和BLOOM都是需要的。这个例子跟SDK中其他HDR例子相比差别并不是很大,因此就仅仅以一张图结束HDR吧。

1155

ADS SerDes仿真实战:深入解析Tx_Diff均衡器(EQ)参数对眼图质量的影响

本文以ADS仿真平台为基础,深入解析了SerDes设计中Tx_Diff均衡器(EQ)参数对信号完整性的核心影响。通过实战演示,详细对比了去加重(De-emphasis)与FIR抽头系数两种调节模式,揭示了它们如何有效对抗信道损耗与码间干扰,从而显著改善眼图的高度、宽度与抖动性能,为高速串行链路设计提供关键调优思路。

weixin_29230901的博客 225

YOLOv13+SAHI实战:5步搞定无人机航拍小目标检测(附VisDrone数据集调参技巧)

本文详细介绍了如何结合YOLOv13与SAHI(切片辅助超推理)技术,在VisDrone2019数据集上实现高效的无人机航拍小目标检测。通过5个实战步骤,包括环境配置、SAHI参数解析、数据集调优、完整训练推理流程和性能优化技巧,帮助开发者显著提升小目标检测的准确率和效率。

egg99的博客 429

DirectX9例程:绘制简单立方体

总结至红龙书提供的立方体绘制例程代码及重要函数:   d3dUtility.h #ifndef __d3dUtilityH__ #define __d3dUtilityH__ #include &lt;d3dx9.h&gt; namespace d3d { //初始化DirectX3D bool InitD3D( HINS...

stIck_NHNP的博客 5466

Java开发者必看:如何快速对接七大主流快递API(附完整代码示例)

本文为Java开发者提供了快速对接顺丰、京东、DHL等七大主流快递API的实战指南。文章深入剖析了不同API的认证、通信协议与参数差异,并提供了高可用的HTTP客户端配置、通用签名工具、SOAP请求处理以及可扩展的领域模型与适配层设计等完整代码示例,帮助开发者构建统一、健壮的物流集成框架,显著提升开发效率。

tech5的博客 727

DirectX9 SDK Samples(12) CustomUI Sample

这一次的例子是关于DXUT的UI。下面先翻译文档中的比较重要的说明。 这个例子开始时定义了两个CDXUTDialog对象:g_HUD和g_SampleUI。一个CDXUTDialog是一个装入了一个或多个控件(按钮等)的容器。对话框(CDXUTDialog)位于程序和控件之间。因此程序可以传递信息和渲染命令给对话框,而对话框保证它的所有控件能够正确收到信息和得到渲染。控件是设计成要和对话框一起使

1732

网工、运维利器:MobaXterm,一款全能的远程工具

你好,这里是网络技术联盟站,我是瑞哥。是一款由Mobatek开发的多功能远程计算机管理工具,集成了多种网络协议和工具,专为网络工程师和系统管理员设计。它提供了丰富的远程连接功能,包括SSH、RDP、VNC、X11转发等,支持文件传输和多标签界面,内置了文本编辑器和多种网络工具,是一款全能的远程管理软件。MobaXterm的开发起始于2008年,最初只是一个简单的X服务器,但随着用户需求的不断增加和软件的逐步完善,MobaXterm逐渐发展成了一款功能全面、界面友好的远程管理工具。

网络技术联盟站 5227

AgentScope实战训练营-Java开发HITL人机协作AI应用完整指南

本文手把手带你从零搭建一个基于 Spring Boot WebFlux + AgentScope + 通义千问的人机协作(HITL)AI 聊天应用。项目涵盖流式对话、工具调用、高危操作确认、对话中断、动态 MCP 工具接入、中英文双语界面等完整功能。文章从环境准备、pom.xml 配置、DTO 数据模型、Hook 机制、AgentService 核心服务、ChatController 接口到前端 HTML/CSS/JS 实现,逐一讲解每行代码的设计意图,并附带 REST API 接口说明、测试场景、常见问题

program哲学 712

DirectX9 SDK Samples(23) HDRFormats Sample

HDR需要浮点纹理的支持,如果显卡不支持浮点纹理的话,那只好通过压缩保存浮点在整数纹理上,然后需要的时候再解码。当然,这是以性能下降为代价的。其实,我觉得,既然这机子连浮点纹理都不支持,那还显示什么HDR啊。。。 好,HDR的原理那都是没什么好说的了,前面曾经看过。那现在就看一下这个例子是如何压缩解压的。 假若支持RGB16,那么只需要将RGB分别除以一个比较大的数就可以了,这个例子中是100

1176

【推荐100个unity插件】制作美丽 Unity 景观,增强Terrain材质表现的插件——GTS

GTS地形着色器是一款专为Unity设计的强大工具,可解决地形纹理绘制和光照处理的难题。它提供一键式配置,支持实时调整地形效果,包含细节注入、高度混合、雪注入等核心功能,并兼容网格地形和低多边形模型。插件完美集成Gaia天气系统,支持HDRP细分效果。安装后通过GTS管理器创建配置文件并应用到地形,即可快速生成高质量地形渲染。其模块化设计包含全局设置、网格转换和细节调整等面板,使开发者能灵活控制地形表现,显著提升视觉效果与性能表现。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1564
上一篇: DirectX9 SDK Samples(20) MeshFromObj Sample
下一篇: DirectX9 SDK Samples(20) HDRDemo Sample(2)
HowdChow
博客等级 码龄15年 10粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值