Merge pull request #953 from benoitjacquier/cubemap_as_texturearray

Cubemap as texturearray
This commit is contained in:
Branimir Karadžić 2016-10-21 09:10:34 -07:00 committed by GitHub
commit ad7da4131e
8 changed files with 141 additions and 27 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Stanlo Slasinski. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "bgfx_compute.sh"
IMAGE2D_ARRAY_WR(s_texColor,rgba32f,0);
uniform vec4 u_time;
NUM_THREADS(16, 16, 1)
void main()
{
vec3 colors[] = {
vec3(1,0,0),
vec3(1,1,0),
vec3(1,0,1),
vec3(0,1,0),
vec3(0,1,1),
vec3(0,0,1),
};
for (int face=0;face<6;face++)
{
vec3 color = colors[face]*0.75 + sin( u_time.x*4.0 )*0.25;
ivec3 dest = ivec3( gl_GlobalInvocationID.xy, face );
imageStore( s_texColor, dest, vec4(color,1) );
}
}

View File

@ -162,7 +162,8 @@ public:
const bgfx::Caps* caps = bgfx::getCaps();
m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
m_numm_textures3d = 0;
m_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
m_numm_textures3d = 0;
if (m_texture3DSupported)
{
@ -215,6 +216,12 @@ public:
m_program3d = loadProgram("vs_update", "fs_update_3d");
}
m_programCompute.idx = bgfx::invalidHandle;
if (m_computeSupported)
{
m_programCompute = bgfx::createProgram( loadShader( "cs_update" ), true );
}
// Create texture sampler uniforms.
s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
@ -222,6 +229,11 @@ public:
// Create time uniform.
u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
{
m_textureCube[ii].idx = bgfx::invalidHandle;
}
m_textureCube[0] = bgfx::createTextureCube(
textureside
, false
@ -241,6 +253,17 @@ public:
);
}
if (m_computeSupported)
{
m_textureCube[2] = bgfx::createTextureCube(
textureside
, false
, 1
, bgfx::TextureFormat::RGBA8
, BGFX_TEXTURE_COMPUTE_WRITE
);
}
m_texture2d = bgfx::createTexture2D(
texture2dSize
, texture2dSize
@ -284,11 +307,15 @@ public:
}
bgfx::destroyTexture(m_texture2d);
bgfx::destroyTexture(m_textureCube[0]);
if (m_blitSupported)
for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
{
bgfx::destroyTexture(m_textureCube[1]);
if (bgfx::isValid(m_textureCube[ii]))
{
bgfx::destroyTexture(m_textureCube[ii]);
}
}
bgfx::destroyIndexBuffer(m_ibh);
bgfx::destroyVertexBuffer(m_vbh);
if (bgfx::isValid(m_program3d) )
@ -296,6 +323,10 @@ public:
bgfx::destroyProgram(m_program3d);
}
bgfx::destroyProgram(m_programCmp);
if (bgfx::isValid(m_programCompute) )
{
bgfx::destroyProgram(m_programCompute);
}
bgfx::destroyProgram(m_program);
bgfx::destroyUniform(u_time);
bgfx::destroyUniform(s_texColor);
@ -426,26 +457,36 @@ public:
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
for (uint32_t ii = 0; ii < 1 + uint32_t(m_blitSupported); ++ii)
// Update texturecube using compute shader
if (m_computeSupported )
{
float mtx[16];
bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, time, time*0.37f, 0.0f, -1.5f*m_blitSupported + ii*3.0f, 0.0f, 0.0f);
bgfx::setImage( 0, s_texCube, m_textureCube[2], 0, bgfx::Access::Write );
bgfx::dispatch( 0, m_programCompute, textureside/16, textureside/16 );
}
// Set model matrix for rendering.
bgfx::setTransform(mtx);
for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
{
if (bgfx::isValid(m_textureCube[ii]))
{
float mtx[16];
bx::mtxSRT( mtx, 0.7f, 0.7f, 0.7f, time, time*0.37f, 0.0f, -2.0f +ii*2.0f, 0.0f, 0.0f );
// Set vertex and index buffer.
bgfx::setVertexBuffer(m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Set model matrix for rendering.
bgfx::setTransform( mtx );
// Bind texture.
bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
// Set vertex and index buffer.
bgfx::setVertexBuffer( m_vbh );
bgfx::setIndexBuffer( m_ibh );
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
// Bind texture.
bgfx::setTexture( 0, s_texCube, m_textureCube[ii] );
// Submit primitive for rendering to view 0.
bgfx::submit(0, m_program);
// Set render states.
bgfx::setState( BGFX_STATE_DEFAULT );
// Submit primitive for rendering to view 0.
bgfx::submit( 0, m_program );
}
}
// Set view and projection matrix for view 1.
@ -557,6 +598,7 @@ public:
uint32_t m_numm_textures3d;
bool m_texture3DSupported;
bool m_blitSupported;
bool m_computeSupported;
std::list<PackCube> m_quads;
RectPackCubeT<256> m_cube;
@ -573,11 +615,12 @@ public:
bgfx::TextureHandle m_textures[9];
bgfx::TextureHandle m_textures3d[3];
bgfx::TextureHandle m_texture2d;
bgfx::TextureHandle m_textureCube[2];
bgfx::TextureHandle m_textureCube[3];
bgfx::IndexBufferHandle m_ibh;
bgfx::VertexBufferHandle m_vbh;
bgfx::ProgramHandle m_program3d;
bgfx::ProgramHandle m_programCmp;
bgfx::ProgramHandle m_programCompute;
bgfx::ProgramHandle m_program;
bgfx::UniformHandle u_time;
bgfx::UniformHandle s_texColor;

Binary file not shown.

Binary file not shown.

View File

@ -19,6 +19,7 @@
#define rg16f float2
#define rgba16f float4
#define rgba8 float4
#define rgba32f float4
#define IMAGE2D_RO( _name, _format, _reg) Texture2D<_format> _name : register(t[_reg])
#define UIMAGE2D_RO(_name, _format, _reg) Texture2D<_format> _name : register(t[_reg])
@ -27,6 +28,13 @@
#define IMAGE2D_RW( _name, _reg) RWTexture2D<float> _name : register(u[_reg])
#define UIMAGE2D_RW(_name, _reg) RWTexture2D<uint> _name : register(u[_reg])
#define IMAGE2D_ARRAY_RO( _name, _format, _reg) Texture2DArray<_format> _name : register(t[_reg])
#define UIMAGE2D_ARRAY_RO(_name, _format, _reg) Texture2DArray<_format> _name : register(t[_reg])
#define IMAGE2D_ARRAY_WR( _name, _format, _reg) RWTexture2DArray<_format> _name : register(u[_reg])
#define UIMAGE2D_ARRAY_WR(_name, _format, _reg) RWTexture2DArray<_format> _name : register(u[_reg])
#define IMAGE2D_ARRAY_RW( _name, _reg) RWTexture2DArray<float> _name : register(u[_reg])
#define UIMAGE2D_ARRAY_RW(_name, _reg) RWTexture2DArray<uint> _name : register(u[_reg])
#define IMAGE3D_RO( _name, _format, _reg) Texture3D<_format> _name : register(t[_reg])
#define UIMAGE3D_RO(_name, _format, _reg) Texture3D<_format> _name : register(t[_reg])
#define IMAGE3D_WR( _name, _format, _reg) RWTexture3D<_format> _name : register(u[_reg])
@ -41,12 +49,15 @@
#define NUM_THREADS(_x, _y, _z) [numthreads(_x, _y, _z)]
#define __IMAGE_IMPL(_textureType, _storeComponents, _type, _loadComponents) \
_type imageLoad( Texture2D<_textureType> _image, ivec2 _uv) { return _image[_uv ]._loadComponents; } \
_type imageLoad( Texture3D<_textureType> _image, ivec3 _uvw) { return _image[_uvw]._loadComponents; } \
_type imageLoad(RWTexture2D<_textureType> _image, ivec2 _uv) { return _image[_uv ]._loadComponents; } \
_type imageLoad(RWTexture3D<_textureType> _image, ivec3 _uvw, _type _value) { return _image[_uvw]._loadComponents; } \
void imageStore(RWTexture2D<_textureType> _image, ivec2 _uv, _type _value) { _image[_uv ] = _value._storeComponents; } \
void imageStore(RWTexture3D<_textureType> _image, ivec3 _uvw, _type _value) { _image[_uvw] = _value._storeComponents; }
_type imageLoad( Texture2D<_textureType> _image, ivec2 _uv) { return _image[_uv ]._loadComponents; } \
_type imageLoad(RWTexture2DArray<_textureType> _image, ivec3 _uvw) { return _image[_uvw ]._loadComponents; } \
_type imageLoad( Texture3D<_textureType> _image, ivec3 _uvw) { return _image[_uvw]._loadComponents; } \
_type imageLoad( RWTexture2D<_textureType> _image, ivec2 _uv) { return _image[_uv ]._loadComponents; } \
_type imageLoad(RWTexture2DArray<_textureType> _image, ivec3 _uvw, _type _value) { return _image[_uvw]._loadComponents; } \
_type imageLoad( RWTexture3D<_textureType> _image, ivec3 _uvw, _type _value) { return _image[_uvw]._loadComponents; } \
void imageStore( RWTexture2D<_textureType> _image, ivec2 _uv, _type _value) { _image[_uv ] = _value._storeComponents; } \
void imageStore(RWTexture2DArray<_textureType> _image, ivec3 _uvw, _type _value) { _image[_uvw] = _value._storeComponents; } \
void imageStore( RWTexture3D<_textureType> _image, ivec3 _uvw, _type _value) { _image[_uvw] = _value._storeComponents; }
__IMAGE_IMPL(float, x, vec4, xxxx)
__IMAGE_IMPL(vec2, xy, vec4, xyyy)
@ -148,6 +159,13 @@ uint atomicCompSwap(uint _mem, uint _compare, uint _data)
#define IMAGE2D_RW( _name, _reg) __IMAGE_XX(_name, r32f, _reg, image2D, readwrite)
#define UIMAGE2D_RW(_name, _reg) __IMAGE_XX(_name, r32ui, _reg, uimage2D, readwrite)
#define IMAGE2D_ARRAY_RO( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2DArray, readonly)
#define UIMAGE2D_ARRAY_RO(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2DArray, readonly)
#define IMAGE2D_ARRAY_WR( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2DArray, writeonly)
#define UIMAGE2D_ARRAY_WR(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2DArray, writeonly)
#define IMAGE2D_ARRAY_RW( _name, _reg) __IMAGE_XX(_name, r32f, _reg, image2DArray, readwrite)
#define UIMAGE2D_ARRAY_RW(_name, _reg) __IMAGE_XX(_name, r32ui, _reg, uimage2DArray, readwrite)
#define IMAGE3D_RO( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image3D, readonly)
#define UIMAGE3D_RO(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage3D, readonly)
#define IMAGE3D_WR( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image3D, writeonly)

View File

@ -3088,11 +3088,17 @@ BX_PRAGMA_DIAGNOSTIC_POP();
switch (texture.m_type)
{
case TextureD3D11::Texture2D:
case TextureD3D11::TextureCube:
desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
desc.Texture2D.MipSlice = _mip;
break;
case TextureD3D11::TextureCube:
desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY;
desc.Texture2DArray.ArraySize = 6;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.MipSlice = _mip;
break;
case TextureD3D11::Texture3D:
desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D;
desc.Texture3D.MipSlice = _mip;

View File

@ -3012,6 +3012,7 @@ data.NumQualityLevels = 0;
D3D12_UNORDERED_ACCESS_VIEW_DESC tmpUavd;
D3D12_UNORDERED_ACCESS_VIEW_DESC* uavd = &_texture.m_uavd;
if (0 != _mip)
{
memcpy(&tmpUavd, uavd, sizeof(tmpUavd) );
@ -3024,6 +3025,10 @@ data.NumQualityLevels = 0;
uavd->Texture2D.MipSlice = _mip;
uavd->Texture2D.PlaneSlice = 0;
break;
case D3D12_UAV_DIMENSION_TEXTURE2DARRAY:
uavd->Texture2DArray.MipSlice = _mip;
uavd->Texture2DArray.PlaneSlice = 0;
break;
case D3D12_UAV_DIMENSION_TEXTURE3D:
uavd->Texture3D.MipSlice = _mip;
@ -4239,6 +4244,14 @@ data.NumQualityLevels = 0;
m_uavd.Texture2D.MipSlice = 0;
m_uavd.Texture2D.PlaneSlice = 0;
}
if( m_type==TextureCube )
{
m_uavd.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
m_uavd.Texture2DArray.MipSlice = 0;
m_uavd.Texture2DArray.ArraySize = 6;
}
break;
case Texture3D:

View File

@ -3763,6 +3763,7 @@ namespace bgfx { namespace gl
GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_1D);
GLSL_TYPE(GL_IMAGE_2D);
GLSL_TYPE(GL_IMAGE_2D_ARRAY);
GLSL_TYPE(GL_INT_IMAGE_2D);
GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_2D);
@ -3860,6 +3861,7 @@ namespace bgfx { namespace gl
case GL_UNSIGNED_INT_IMAGE_1D:
case GL_IMAGE_2D:
case GL_IMAGE_2D_ARRAY:
case GL_INT_IMAGE_2D:
case GL_UNSIGNED_INT_IMAGE_2D:
@ -6389,7 +6391,7 @@ namespace bgfx { namespace gl
GL_CHECK(glBindImageTexture(ii
, texture.m_id
, bind.m_un.m_compute.m_mip
, GL_FALSE
, texture.isCubeMap()?GL_TRUE:GL_FALSE
, 0
, s_access[bind.m_un.m_compute.m_access]
, s_imageFormat[bind.m_un.m_compute.m_format])