⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dx9runtime.cpp

📁 用于GPU通用计算的编程语言BrookGPU 0.4
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    f3.z = 0;    f3.w = 1;    interpolant.vertices[0] = f1;    interpolant.vertices[1] = f2;     interpolant.vertices[2] = f3;  }  void GPUContextDX9Impl::getStreamInterpolant( const TextureHandle inTexture,    unsigned int rank,    const unsigned int* domainMin,    const unsigned int* domainMax,    const unsigned int outputWidth,    const unsigned int outputHeight,     GPUInterpolant &interpolant) const  {    DX9Texture* texture = (DX9Texture*)inTexture;    unsigned int minX, minY, maxX, maxY;    if( rank == 1 )    {        minX = domainMin[0];        minY = 0;        maxX = domainMax[0];        maxY = 0;    }    else    {        minX = domainMin[1];        minY = domainMin[0];        maxX = domainMax[1];        maxY = domainMax[0];    }    unsigned int textureWidth = texture->getWidth();    unsigned int textureHeight = texture->getHeight();    float xmin = (float)minX / (float)textureWidth;    float ymin = (float)minY / (float)textureHeight;    float width = (float)(maxX - minX) / (float)textureWidth;    float height = (float)(maxY - minY) / (float)textureHeight;        float xmax = xmin + 2*width;    float ymax = ymin + 2*height;    interpolant.vertices[0] = float4(xmin,ymin,0.5,1);    interpolant.vertices[1] = float4(xmax,ymin,0.5,1);    interpolant.vertices[2] = float4(xmin,ymax,0.5,1);    if( _shouldBiasInterpolants )    {        float biasX = 0.0f;        float biasY = 0.0f;        if( textureWidth > 1 )            biasX = kInterpolantBias / (float)(textureWidth);        if( textureHeight > 1 )            biasY = kInterpolantBias / (float)(textureHeight);        for( int i = 0; i < 3; i++ )        {            interpolant.vertices[i].x += biasX;            interpolant.vertices[i].y += biasY;        }    }  }  void GPUContextDX9Impl::getStreamOutputRegion( const TextureHandle inTexture,    unsigned int rank,    const unsigned int* domainMin,    const unsigned int* domainMax,    GPURegion &region) const  {    DX9Texture* texture = (DX9Texture*)inTexture;    unsigned int minX, minY, maxX, maxY;    if( rank == 1 )    {        minX = domainMin[0];        minY = 0;        maxX = domainMax[0];        maxY = 1;    }    else    {        minX = domainMin[1];        minY = domainMin[0];        maxX = domainMax[1];        maxY = domainMax[0];    }    region.vertices[0] = float4(-1,1,0.5,1);    region.vertices[1] = float4(3,1,0.5,1);    region.vertices[2] = float4(-1,-3,0.5,1);    region.viewport.minX = minX;    region.viewport.minY = minY;    region.viewport.maxX = maxX;    region.viewport.maxY = maxY;  }  void GPUContextDX9Impl::getStreamReduceInterpolant( const TextureHandle inTexture,    const unsigned int outputWidth,    const unsigned int outputHeight,     const unsigned int minX,    const unsigned int maxX,     const unsigned int minY,    const unsigned int maxY,    GPUInterpolant &interpolant) const  {    DX9Texture* texture = (DX9Texture*)inTexture;    unsigned int textureWidth = texture->getWidth();    unsigned int textureHeight = texture->getHeight();    GPULOG(3) << "texw = " << textureWidth << " texh = " << textureHeight << std::endl;    float xmin = (float)minX / (float)textureWidth;    float ymin = (float)minY / (float)textureHeight;    float width = (float)(maxX - minX) / (float)textureWidth;    float height = (float)(maxY - minY) / (float)textureHeight;        float xmax = xmin + 2*width;    float ymax = ymin + 2*height;    interpolant.vertices[0] = float4(xmin,ymin,0.5,1);    interpolant.vertices[1] = float4(xmax,ymin,0.5,1);    interpolant.vertices[2] = float4(xmin,ymax,0.5,1);    if( _shouldBiasInterpolants )    {        int width = maxX - minX;        int height = maxY - minY;        float biasX = textureWidth <= 1 ? 0.0f : kInterpolantBias / (float)(textureWidth);        float biasY = textureHeight <= 1 ? 0.0f : kInterpolantBias / (float)(textureHeight);        for( int i = 0; i < 3; i++ )        {            interpolant.vertices[i].x += biasX;            interpolant.vertices[i].y += biasY;        }    }  }  void GPUContextDX9Impl::getStreamReduceOutputRegion( const TextureHandle inTexture,    const unsigned int minX,    const unsigned int maxX,     const unsigned int minY,    const unsigned int maxY,    GPURegion &region) const  {    region.vertices[0] = float4(-1,1,0.5,1);    region.vertices[1] = float4(3,1,0.5,1);    region.vertices[2] = float4(-1,-3,0.5,1);    region.viewport.minX = minX;    region.viewport.minY = minY;    region.viewport.maxX = maxX;    region.viewport.maxY = maxY;  }  GPUContextDX9Impl::TextureHandle GPUContextDX9Impl::createTexture2D( size_t inWidth, size_t inHeight, TextureFormat inFormat )  {    int components;    switch( inFormat )    {    case kTextureFormat_Float1:      components = 1;      break;    case kTextureFormat_Float2:      components = 2;      break;    case kTextureFormat_Float3:      components = 3;      break;    case kTextureFormat_Float4:      components = 4;      break;    default:      GPUError("Unknown format for DX9 Texture");      return 0;      break;    }    DX9Texture* result = DX9Texture::create( this, inWidth, inHeight, components );    return result;  }  void GPUContextDX9Impl::releaseTexture( TextureHandle inTexture )  {    DX9Texture* texture = (DX9Texture*)inTexture;    delete texture;  }  void GPUContextDX9Impl::setTextureData(    TextureHandle inTexture, const float* inData, size_t inStrideBytes, size_t inComponentCount,    unsigned int inRank,    const unsigned int* inDomainMin,    const unsigned int* inDomainMax,    const unsigned int* inExtents, bool inUsesAddressTranslation )  {    DX9Texture* texture = (DX9Texture*)inTexture;    texture->setData( inData, inStrideBytes, inComponentCount, inRank, inDomainMin, inDomainMax, inExtents, inUsesAddressTranslation );    texture->markShadowDataChanged();  }  void GPUContextDX9Impl::getTextureData(    TextureHandle inTexture, float* outData, size_t inStrideBytes, size_t inComponentCount,    unsigned int inRank,    const unsigned int* inDomainMin,    const unsigned int* inDomainMax,    const unsigned int* inExtents, bool inUsesAddressTranslation )  {    DX9Texture* texture = (DX9Texture*)inTexture;    texture->getData( outData, inStrideBytes, inComponentCount, inRank, inDomainMin, inDomainMax, inExtents, inUsesAddressTranslation );  }  GPUContextDX9Impl::PixelShaderHandle GPUContextDX9Impl::createPixelShader( const char* inSource )  {    IDirect3DPixelShader9* shader;    HRESULT result;    LPD3DXBUFFER codeBuffer;    LPD3DXBUFFER errorBuffer;    result = D3DXAssembleShader( inSource, strlen(inSource), NULL, NULL, 0, &codeBuffer, &errorBuffer );    if( errorBuffer != NULL )    {      const char* errorMessage = (const char*)errorBuffer->GetBufferPointer();      GPUWARN << "Pixel shader failed to compile:\n" << errorMessage;      return NULL;    }    else if( FAILED(result) )    {      GPUWARN << "Pixel shader failed to compile.";      return NULL;    }    result = _device->CreatePixelShader( (DWORD*)codeBuffer->GetBufferPointer(), &shader );    codeBuffer->Release();    if( FAILED(result) )    {      DX9WARN << "Failed to allocate pixel shader.";      return NULL;    }    return (PixelShaderHandle)shader;  }  GPUContextDX9Impl::VertexShaderHandle GPUContextDX9Impl::createVertexShader( const char* inSource )  {    IDirect3DVertexShader9* shader;    HRESULT result;    LPD3DXBUFFER codeBuffer;    LPD3DXBUFFER errorBuffer;    result = D3DXAssembleShader( inSource, strlen(inSource), NULL, NULL, 0, &codeBuffer, &errorBuffer );    if( errorBuffer != NULL )    {      const char* errorMessage = (const char*)errorBuffer->GetBufferPointer();      GPUWARN << "Vertex shader failed to compile:\n" << errorMessage;      return NULL;    }    else if( FAILED(result) )    {      GPUWARN << "Vertex shader failed to compile.";      return NULL;    }    result = _device->CreateVertexShader( (DWORD*)codeBuffer->GetBufferPointer(), &shader );    codeBuffer->Release();    if( FAILED(result) )    {      DX9WARN << "Failed to allocate vertex shader.";      return NULL;    }    return (VertexShaderHandle)shader;  }  void GPUContextDX9Impl::beginScene()  {    HRESULT result = _device->BeginScene();    GPUAssert( !FAILED(result), "BeginScene failed" );  }  void GPUContextDX9Impl::endScene()  {    HRESULT result = _device->EndScene();    GPUAssert( !FAILED(result), "BeginScene failed" );    for( size_t i = 0; i < kMaximumSamplerCount; i++ )      _boundTextures[i] = NULL;    for( size_t j = 0; j < kMaximumOutputCount; j++ )      _boundOutputs[j] = NULL;  }  void GPUContextDX9Impl::bindConstant( PixelShaderHandle /* unused */,                                     size_t inIndex, const float4& inValue )  {    HRESULT result = _device->SetPixelShaderConstantF( inIndex, (const float*)&inValue, 1 );    GPUAssert( !FAILED(result), "SetPixelShaderConstantF failed" );  }  void GPUContextDX9Impl::bindTexture( size_t inIndex, TextureHandle inTexture )  {    DX9Texture* texture = (DX9Texture*)inTexture;    _boundTextures[inIndex] = texture;    HRESULT result = _device->SetTexture( inIndex, texture->getTextureHandle() );    GPUAssert( !FAILED(result), "SetTexture failed" );    result = _device->SetSamplerState( inIndex, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );    GPUAssert( !FAILED(result), "SetSamplerState failed" );    result = _device->SetSamplerState( inIndex, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );    GPUAssert( !FAILED(result), "SetSamplerState failed" );  }  void GPUContextDX9Impl::bindOutput( size_t inIndex, TextureHandle inTexture )  {    DX9Texture* texture = (DX9Texture*)inTexture;    _boundOutputs[inIndex] = texture;    HRESULT result = _device->SetRenderTarget( inIndex, texture->getSurfaceHandle() );    GPUAssert( !FAILED(result), "SetRenderTarget failed" );  }  void GPUContextDX9Impl::disableOutput( size_t inIndex )  {    HRESULT result = _device->SetRenderTarget( inIndex, NULL );    GPUAssert( !FAILED(result), "SetRenderTarget failed" );  }  void GPUContextDX9Impl::bindPixelShader( PixelShaderHandle inPixelShader )  {    HRESULT result = _device->SetPixelShader( (IDirect3DPixelShader9*)inPixelShader );    GPUAssert( !FAILED(result), "SetPixelShader failed" );  }  void GPUContextDX9Impl::bindVertexShader( VertexShaderHandle inVertexShader )  {    HRESULT result = _device->SetVertexShader( (IDirect3DVertexShader9*)inVertexShader );    GPUAssert( !FAILED(result), "SetVertexShader failed" );  }  void* GPUContextDX9Impl::getTextureRenderData( TextureHandle inTexture )  {
    DX9Texture* texture = (DX9Texture*)inTexture;    return texture->getTextureHandle();  }  void GPUContextDX9Impl::synchronizeTextureRenderData( TextureHandle inTexture )  {
    DX9Texture* texture = (DX9Texture*)inTexture;    texture->validateCachedData();  }  void GPUContextDX9Impl::drawRectangle(    const GPURegion& inOutputRegion,     const GPUInterpolant* inInterpolants,     unsigned int inInterpolantCount )  {    HRESULT result;    unsigned int minX = inOutputRegion.viewport.minX;    unsigned int minY = inOutputRegion.viewport.minY;    unsigned int maxX = inOutputRegion.viewport.maxX;    unsigned int maxY = inOutputRegion.viewport.maxY;    GPULOG(4) << "[ <" << minX << ", " << minY << ">, <" << maxX << ", " << maxY << "> )";    D3DVIEWPORT9 viewport;    viewport.X = minX;    viewport.Y = minY;    viewport.Width = maxX - minX;    viewport.Height = maxY - minY;    viewport.MinZ = 0.0f;    viewport.MaxZ = 1.0f;    // TIM: we have to flush any host-side changes to    // the output buffers forward, since we might    // only be writing to a domain    for( size_t j = 0; j < kMaximumOutputCount; j++ )    {      if( _boundOutputs[j] )        _boundOutputs[j]->validateCachedData();    }    result = _device->SetViewport( &viewport );    DX9AssertResult( result, "SetViewport failed" );//    result = _device->Clear( 0, NULL, D3DCLEAR_TARGET, 0, 0.0, 0 );//    DX9AssertResult( result, "Clear failed" );    DX9Vertex* vertices;    result = _vertexBuffer->Lock( 0, 0, (void**)&vertices, D3DLOCK_DISCARD );    DX9AssertResult( result, "VB::Lock failed" );    DX9Assert( inInterpolantCount <= 8,      "Can't have more than 8 texture coordinate interpolators" );    DX9Vertex vertex;    for( size_t i = 0; i < 3; i++ )    {      float4 position = inOutputRegion.vertices[i];      // TIM: bad      vertex.position.x = position.x;      vertex.position.y = position.y;      vertex.position.z = 0.5f;      vertex.position.w = 1.0f;      GPULOGPRINT(4) << "v[" << i << "] pos=<" << position.x << ", " << position.y << ">";      for( size_t t = 0; t < inInterpolantCount; t++ )      {        vertex.texcoords[t] = inInterpolants[t].vertices[i];        GPULOGPRINT(4) << " t" << t << "=<" << vertex.texcoords[t].x << ", " << vertex.texcoords[t].y << ">";      }      GPULOGPRINT(4) << std::endl;      *vertices++ = vertex;    }    result = _vertexBuffer->Unlock();    DX9AssertResult( result, "VB::Unlock failed" );    result = _device->SetVertexDeclaration( _vertexDecl );    DX9AssertResult( result, "SetVertexDeclaration failed" );    result = _device->SetStreamSource( 0, _vertexBuffer, 0, sizeof(DX9Vertex) );    DX9AssertResult( result, "SetStreamSource failed" );    for( size_t t = 0; t < kMaximumSamplerCount; t++ )    {      if( _boundTextures[t] )        _boundTextures[t]->validateCachedData();    }    result = _device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );    DX9AssertResult( result, "DrawPrimitive failed" );    for( size_t j = 0; j < kMaximumOutputCount; j++ )    {      if( _boundOutputs[j] )        _boundOutputs[j]->markCachedDataChanged();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -