zoukankan      html  css  js  c++  java
  • 如何在GPU上产生随机数

     

        随机数在CPU中可以有rand()这类函数,但是GPU中却没有。如果想直接在GPU中使用随机数?有一个折中的方法,先再CPU中产生一个随机数数组,然后写入一个texture1D中。最后再利用GPU来提取。

        下面来看看具体代码:

        先看Directx的部分。

        1:主要在Directx中生成并初始化一个存储了随机数的纹理ID3D11Texture1D m_pRanTex和它的“指针”ID3D11ShaderResourceView m_pRanTexRV。

    HRESULT ParticleSystem::BuildRandomTex()

    {

    HRESULT hr=S_OK;

    D3DXVECTOR4 randomValue[1024];

    for(int i=0;i<1024;i++)

    {

    randomValue[i].x=RandF(-1.0f,1.0f);

    randomValue[i].y=RandF(-1.0f,1.0f);

    randomValue[i].z=RandF(-1.0f,1.0f);

    randomValue[i].w=RandF(-1.0f,1.0f);

    }

    D3D11_SUBRESOURCE_DATA subData;

    ZeroMemory(&subData,sizeof(D3D11_SUBRESOURCE_DATA));

    subData.pSysMem=randomValue;

    D3D11_TEXTURE1D_DESC texDesc;

    ZeroMemory(&texDesc,sizeof(texDesc));

    texDesc.ArraySize=1;

    texDesc.BindFlags=D3D11_BIND_SHADER_RESOURCE;

    texDesc.CPUAccessFlags=0;

    texDesc.Format=DXGI_FORMAT_R32G32B32A32_FLOAT;

    texDesc.MiscFlags=0;

    texDesc.Width=1024;

    texDesc.MipLevels=1;

    texDesc.Usage=D3D11_USAGE_IMMUTABLE;

    IFR(m_pDevice->CreateTexture1D(&texDesc,&subData,&m_pRanTex));

    D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;

    viewDesc.Format=texDesc.Format;

    viewDesc.ViewDimension=D3D11_SRV_DIMENSION_TEXTURE1D;

    viewDesc.Texture1D.MostDetailedMip=0;

    viewDesc.Texture1D.MipLevels=texDesc.MipLevels;

    IFR(m_pDevice->CreateShaderResourceView(m_pRanTex,&viewDesc,&m_pRanTexRV));

    return hr;

       2:在每帧的绘制函数中绑定我们上面生成的纹理:

        ID3DX11EffectShaderResouceVariable::SetResource(m_pRanTexRV);

     

    再来看HLSL部分的代码

    cbuffer PerFrame

    {

       float gameTime;

    };

    Texture1D gRandomTex;

    float3 GetRandomVec3(float offset)

    {

    float u=offset+gameTime;

    float3 v=gRandomTex.SampleLevel(gLinearSam,u,0);

    return v;

    }

          上面的gameTime是当前系统经过的时间,为了保证随时间变化的随机性而做的一个小技巧。,offset是根据你的需要而做的一个额外参数,比如你一次性需要5个随机向量,那么可以

      float3 randVec3[5];

      For(int i=0;i<5;i++)

      {

         float3 randVec3[i]=GetRandomVec3((float)i/5.0f);

      }

  • 相关阅读:
    最近几天在winform系统界面操作,流程操作上的经验总结.
    人生路痛苦中......
    Contact List
    奥巴马当选
    在会计电算化软件中设计凭证数据库的几种方法转摘(粗浅)
    修改ListItem时出现Error: Invalid data has been used to update the list item. The field you are trying to update may be read only.
    MOSS误删网站集后,如何还原?
    SharePoint2007 管理中心TimerJob”计时器作业定义”页面报错
    SharePoint 2010 常用技巧及方法总结
    利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext
  • 原文地址:https://www.cnblogs.com/bester/p/3255788.html
Copyright © 2011-2022 走看看