From 7ec5de1ad1528c69c3824209cc6bd69c0796be24 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 6 Jan 2013 17:53:45 -0800 Subject: [PATCH] Added texture update example. --- README.md | 3 + examples/06-bump/bump.cpp | 12 +- examples/08-update/fs_update.sc | 15 + examples/08-update/makefile | 17 + examples/08-update/update.cpp | 336 ++++++++++++++++++++ examples/08-update/varying.def.sc | 4 + examples/08-update/vs_update.sc | 15 + examples/runtime/shaders/dx11/fs_update.bin | Bin 0 -> 638 bytes examples/runtime/shaders/dx11/vs_update.bin | Bin 0 -> 1084 bytes examples/runtime/shaders/dx9/fs_update.bin | Bin 0 -> 201 bytes examples/runtime/shaders/dx9/vs_update.bin | Bin 0 -> 319 bytes examples/runtime/shaders/gles/fs_update.bin | Bin 0 -> 192 bytes examples/runtime/shaders/gles/vs_update.bin | Bin 0 -> 332 bytes examples/runtime/shaders/glsl/fs_update.bin | Bin 0 -> 153 bytes examples/runtime/shaders/glsl/vs_update.bin | Bin 0 -> 293 bytes include/bgfx.h | 1 + premake/example-08-update.lua | 39 +++ premake/premake4.lua | 1 + src/bgfx.cpp | 8 + src/bgfx_p.h | 7 +- src/renderer_d3d11.cpp | 2 +- src/renderer_d3d9.cpp | 30 +- src/renderer_d3d9.h | 1 + 23 files changed, 472 insertions(+), 19 deletions(-) create mode 100644 examples/08-update/fs_update.sc create mode 100644 examples/08-update/makefile create mode 100644 examples/08-update/update.cpp create mode 100644 examples/08-update/varying.def.sc create mode 100644 examples/08-update/vs_update.sc create mode 100644 examples/runtime/shaders/dx11/fs_update.bin create mode 100644 examples/runtime/shaders/dx11/vs_update.bin create mode 100644 examples/runtime/shaders/dx9/fs_update.bin create mode 100644 examples/runtime/shaders/dx9/vs_update.bin create mode 100644 examples/runtime/shaders/gles/fs_update.bin create mode 100644 examples/runtime/shaders/gles/vs_update.bin create mode 100644 examples/runtime/shaders/glsl/fs_update.bin create mode 100644 examples/runtime/shaders/glsl/vs_update.bin create mode 100644 premake/example-08-update.lua diff --git a/README.md b/README.md index b1a129c2..90a69016 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,9 @@ Loading textures. Implementing application specific callbacks for taking screen shots, caching OpenGL binary shaders, and video capture. +### 08-update +Updating textures. + Internals --------- diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index cc8be850..0cd8ef86 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -338,11 +338,11 @@ int _main_(int _argc, char** _argv) // Load diffuse texture. mem = loadTexture("fieldstone-rgba.dds"); - bgfx::TextureHandle texture_rgba = bgfx::createTexture(mem); + bgfx::TextureHandle textureColor = bgfx::createTexture(mem); // Load normal texture. mem = loadTexture("fieldstone-n.dds"); - bgfx::TextureHandle texture_n = bgfx::createTexture(mem); + bgfx::TextureHandle textureNormal = bgfx::createTexture(mem); while (true) { @@ -437,8 +437,8 @@ int _main_(int _argc, char** _argv) bgfx::setInstanceDataBuffer(idb, numInstances); // Bind textures. - bgfx::setTexture(0, u_texColor, texture_rgba); - bgfx::setTexture(1, u_texNormal, texture_n); + bgfx::setTexture(0, u_texColor, textureColor); + bgfx::setTexture(1, u_texNormal, textureNormal); // Set render states. bgfx::setState(BGFX_STATE_RGB_WRITE @@ -459,8 +459,8 @@ int _main_(int _argc, char** _argv) bgfx::destroyIndexBuffer(ibh); bgfx::destroyVertexBuffer(vbh); bgfx::destroyProgram(program); - bgfx::destroyTexture(texture_rgba); - bgfx::destroyTexture(texture_n); + bgfx::destroyTexture(textureColor); + bgfx::destroyTexture(textureNormal); bgfx::destroyUniform(u_texColor); bgfx::destroyUniform(u_texNormal); diff --git a/examples/08-update/fs_update.sc b/examples/08-update/fs_update.sc new file mode 100644 index 00000000..6af633c5 --- /dev/null +++ b/examples/08-update/fs_update.sc @@ -0,0 +1,15 @@ +$input v_texcoord0 + +/* + * Copyright 2011-2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +SAMPLERCUBE(u_texCube, 0); + +void main() +{ + gl_FragColor = textureCube(u_texCube, v_texcoord0); +} diff --git a/examples/08-update/makefile b/examples/08-update/makefile new file mode 100644 index 00000000..3bb2f87f --- /dev/null +++ b/examples/08-update/makefile @@ -0,0 +1,17 @@ +# +# Copyright 2011-2012 Branimir Karadzic. All rights reserved. +# License: http://www.opensource.org/licenses/BSD-2-Clause +# + +BGFX_DIR=../.. +RUNTIME_DIR=$(BGFX_DIR)/examples/runtime +BUILD_DIR=../../.build + +include $(BGFX_DIR)/premake/shader.mk + +rebuild: + @make -s --no-print-directory TARGET=0 clean all + @make -s --no-print-directory TARGET=1 clean all + @make -s --no-print-directory TARGET=2 clean all + @make -s --no-print-directory TARGET=3 clean all + @make -s --no-print-directory TARGET=4 clean all diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp new file mode 100644 index 00000000..146f5b94 --- /dev/null +++ b/examples/08-update/update.cpp @@ -0,0 +1,336 @@ +/* + * Copyright 2011-2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include +#include +#include +#include "../common/dbg.h" +#include "../common/math.h" + +#include +#include + +struct PosColorVertex +{ + float m_x; + float m_y; + float m_z; + float m_u; + float m_v; + float m_w; +}; + +static bgfx::VertexDecl s_PosTexcoordDecl; + +static PosColorVertex s_cubeVertices[24] = +{ + {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }, + { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, + {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f }, + { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f }, + + {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f }, + { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f }, + {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f }, + { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f }, + + {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }, + {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f }, + {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f }, + {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f }, + + { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, + { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f }, + { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f }, + { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f }, + + {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }, + { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, + {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f }, + { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f }, + + {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f }, + {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f }, + { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f }, + { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f }, +}; + +static const uint16_t s_cubeIndices[36] = +{ + 0, 2, 1, // 0 + 1, 2, 3, + + 4, 5, 6, // 2 + 5, 7, 6, + + 8, 9, 10, // 4 + 9, 11, 10, + + 12, 13, 14, // 6 + 14, 13, 15, + + 16, 17, 18, // 8 + 18, 17, 19, + + 20, 21, 22, // 10 + 21, 23, 22, +}; + +static const char* s_shaderPath = NULL; + +static void shaderFilePath(char* _out, const char* _name) +{ + strcpy(_out, s_shaderPath); + strcat(_out, _name); + strcat(_out, ".bin"); +} + +long int fsize(FILE* _file) +{ + long int pos = ftell(_file); + fseek(_file, 0L, SEEK_END); + long int size = ftell(_file); + fseek(_file, pos, SEEK_SET); + return size; +} + +static const bgfx::Memory* load(const char* _filePath) +{ + FILE* file = fopen(_filePath, "rb"); + if (NULL != file) + { + uint32_t size = (uint32_t)fsize(file); + const bgfx::Memory* mem = bgfx::alloc(size+1); + size_t ignore = fread(mem->data, 1, size, file); + BX_UNUSED(ignore); + fclose(file); + mem->data[mem->size-1] = '\0'; + return mem; + } + + return NULL; +} + +static const bgfx::Memory* loadShader(const char* _name) +{ + char filePath[512]; + shaderFilePath(filePath, _name); + return load(filePath); +} + +int _main_(int _argc, char** _argv) +{ + bgfx::init(); + bgfx::reset(1280, 720); + + // Enable debug text. + bgfx::setDebug(BGFX_DEBUG_TEXT); + + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, 1280, 720); + + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT + , 0x303030ff + , 1.0f + , 0 + ); + + // Setup root path for binary shaders. Shader binaries are different + // for each renderer. + switch (bgfx::getRendererType() ) + { + default: + case bgfx::RendererType::Direct3D9: + s_shaderPath = "shaders/dx9/"; + break; + + case bgfx::RendererType::Direct3D11: + s_shaderPath = "shaders/dx11/"; + break; + + case bgfx::RendererType::OpenGL: + s_shaderPath = "shaders/glsl/"; + break; + + case bgfx::RendererType::OpenGLES2: + case bgfx::RendererType::OpenGLES3: + s_shaderPath = "shaders/gles/"; + break; + } + + // Create vertex stream declaration. + s_PosTexcoordDecl.begin(); + s_PosTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float); + s_PosTexcoordDecl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float); + s_PosTexcoordDecl.end(); + + const bgfx::Memory* mem; + + // Create static vertex buffer. + mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ); + bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl); + + // Create static index buffer. + mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ); + bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem); + + // Create texture sampler uniforms. + bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv); + + // Load vertex shader. + mem = loadShader("vs_update"); + bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem); + + // Load fragment shader. + mem = loadShader("fs_update"); + bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem); + + // Create program from shaders. + bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh); + + // We can destroy vertex and fragment shader here since + // their reference is kept inside bgfx after calling createProgram. + // Vertex and fragment shader will be destroyed once program is + // destroyed. + bgfx::destroyVertexShader(vsh); + bgfx::destroyFragmentShader(fsh); + + uint32_t blockSide = 0; + uint32_t blockX = 0; + uint32_t blockY = 0; + const uint32_t blockWidth = 8; + const uint32_t blockHeight = 8; + const uint32_t textureSide = 256; + + bgfx::TextureHandle textureCube = + bgfx::createTextureCube(6 + , textureSide + , 1 + , bgfx::TextureFormat::BGRA8 + , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT + ); + + bgfx::TextureInfo ti; + bgfx::calcTextureSize(ti, blockWidth, blockHeight, 1, 1, bgfx::TextureFormat::BGRA8); + + uint8_t rr = rand()%255; + uint8_t gg = rand()%255; + uint8_t bb = rand()%255; + + int64_t updateTime = 0; + + while (true) + { + // This dummy draw call is here to make sure that view 0 is cleared + // if no other draw calls are submitted to view 0. + bgfx::submit(0); + + int64_t now = bx::getHPCounter(); + static int64_t last = now; + const int64_t frameTime = now - last; + last = now; + const int64_t freq = bx::getHPFrequency(); + const double toMs = 1000.0/double(freq); + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + + if (now > updateTime) + { +// updateTime = now + freq/100; + const bgfx::Memory* mem = bgfx::alloc(ti.storageSize); + uint8_t* data = (uint8_t*)mem->data; + for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii) + { + data[0] = bb; + data[1] = rr; + data[2] = gg; + data[3] = 0xff; + data += 4; + } + + bgfx::updateTextureCube(textureCube, blockSide, 0, blockX, blockY, blockWidth, blockHeight, mem); + + blockX += 8; + if (blockX > textureSide-1) + { + blockX = 0; + blockY += 8; + + if (blockY > textureSide-1) + { + rr = rand()%255; + gg = rand()%255; + bb = rand()%255; + + blockY = 0; + ++blockSide; + + if (blockSide > 5) + { + blockSide = 0; + } + } + } + } + + float at[3] = { 0.0f, 0.0f, 0.0f }; + float eye[3] = { 0.0f, 0.0f, -5.0f }; + + float view[16]; + float proj[16]; + mtxLookAt(view, eye, at); + mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f); + + // Set view and projection matrix for view 0. + bgfx::setViewTransform(0, view, proj); + + float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) ); + + float mtx[16]; + mtxRotateXY(mtx, time, time*0.37f); + + // Set model matrix for rendering. + bgfx::setTransform(mtx); + + // Set vertex and fragment shaders. + bgfx::setProgram(program); + + // Set vertex and index buffer. + bgfx::setVertexBuffer(vbh); + bgfx::setIndexBuffer(ibh); + + // Bind texture. + bgfx::setTexture(0, u_texCube, textureCube); + + // Set render states. + bgfx::setState(BGFX_STATE_RGB_WRITE + |BGFX_STATE_DEPTH_WRITE + |BGFX_STATE_DEPTH_TEST_LESS + ); + + // Submit primitive for rendering to view 0. + bgfx::submit(0); + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + } + + // Cleanup. + bgfx::destroyIndexBuffer(ibh); + bgfx::destroyVertexBuffer(vbh); + bgfx::destroyProgram(program); + + // Shutdown bgfx. + bgfx::shutdown(); + + return 0; +} diff --git a/examples/08-update/varying.def.sc b/examples/08-update/varying.def.sc new file mode 100644 index 00000000..73b1866c --- /dev/null +++ b/examples/08-update/varying.def.sc @@ -0,0 +1,4 @@ +vec3 v_texcoord0 : TEXCOORD0 = vec3(9.0, 0.0, 0.0); + +vec3 a_position : POSITION; +vec3 a_texcoord0 : TEXCOORD0; diff --git a/examples/08-update/vs_update.sc b/examples/08-update/vs_update.sc new file mode 100644 index 00000000..fa425fcb --- /dev/null +++ b/examples/08-update/vs_update.sc @@ -0,0 +1,15 @@ +$input a_position, a_texcoord0 +$output v_texcoord0 + +/* + * Copyright 2011-2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_texcoord0 = a_texcoord0; +} diff --git a/examples/runtime/shaders/dx11/fs_update.bin b/examples/runtime/shaders/dx11/fs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..27e8a824a3fdf3ec451684877764a0573976f8a6 GIT binary patch literal 638 zcmaJ-T}#4X7=8Q*i-?97K@hUbq>|Kvgph1=Ixr`DXGjEo8({Je~ zM3-Il2iAFCKn?QR@95 zGEa!sP*1>5P=z@KKR{D0Yp?J^L?QP0Bkue3Of-YM1!L^}0ncU*LGI%_Hb5UhnP*$5 zleTM5l}XPQ-QoDrc76@Zp1KpqrmMl7GkO}`yYjx7ma8>Ulf^@~Z#%Lw8jc6(kqenz zAyYWbW%AiJ7~1d)L7;kiq+eyKY0PY%ah)=ZOT7xNbKLp7jyaDA(l0J{Ki%IFI&~~<9`F_Q%&Fi literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/dx11/vs_update.bin b/examples/runtime/shaders/dx11/vs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..f631afad128c8d2de1323815bdd2d735b0861ba9 GIT binary patch literal 1084 zcma)5%Wl(95FIy$8WuDyut5lU!2*PcNXjcx)w;xW6BOzwj-qts78+=fi&5Ib3aLvL z2#GI%6&wFV7W@J`_=!sF5apcs4vJ7Dm}oLH=bn4+c&^=z>Q;JXdFe#PW`bBUvgQus zy>zFabO-&{tzr87jEH56WBa`awTCZ0ehp;)`_1r6{KI~8&%$oU7CD7@8~zQztr?NG z@OK4~w!ap90!CgP&UhIv_OBE9Ad&!BVm-ykrCnW5(#PA$zGPWO?ks!@K2id5$K)Pz zTG7-lqKBGJQ~U<-Bh((8ruZW85o!-kQ+$PbMr&pQ4~ZFklD`G)XbOH9zrs`{D@Z|X z{M2%s(-t#*jl6})e3GL#)B}OKTl;lp!)J5g_q8!ZT;tegG5A%JGZ4ok3h`DLH9C!OLpp1{ zS{Sx{h=$N?Vf}AS?orJ-CZxd&&rnuJsivP|34RKh3 AX#fBK literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/dx9/fs_update.bin b/examples/runtime/shaders/dx9/fs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..ca4d6423665521d908f1449f39bdadef20e8aca2 GIT binary patch literal 201 zcmZ<@_F&AHv$J7f*u%iU{Qv*I|4IzbA&yQm3=9mxKnxON1X3VALn8x&50EkdVrC#_ zWME$BL=hCE9hSjTC82Es4AP4~k#qq}R1`NKL$wm3a`DrBz8bO*09zMZ7 z3c(qPDXB#Y&iT0onK?j)rJj+co~5agp0S~!A%i>v0~0F)LjwZ?`vedhXxa=04*msB VAnQ5!f#OdlFmOCs@c%zh1_1g6DVP8N literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/dx9/vs_update.bin b/examples/runtime/shaders/dx9/vs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..6565a4a1cc401bdfb7a6bfae9699a9c70fce7f91 GIT binary patch literal 319 zcmZ8c%WA?<5Ir}Eh^zd7*g!XS6O7VDKuW6{7Ybs0EQ4sI5#^Rx?Kb^`d_vcLRTh4S z>6w%wICBp(bLMep=D|Se#!FYhK8Aa}j&}1n`Wh#CTT_6Ax`KHvuEitz)3;t7z=9Bt zX-JH3U`*2@48kH_36Z;zFsT+*dPPo$uoJd0idTs~=*_3ooIE*$VK8)p_vJcDoW9IxrdV%mnE-` z`-rSI7SGr6R6r-DqMq1eLv_=kb*zRIBI7k#Op>n#!INcau~%*9jAY?vFqDJ2&ntyd z$%A~2qp5Z{&b(q4y`3H=jjbY&>lmJZSi3S8RcGv2@PLy#1TmlB`d8fiN8%J-0E;*F CYd^C9 literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/gles/vs_update.bin b/examples/runtime/shaders/gles/vs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..ffd89389795f6c8ca8e7127b92c27b95d1facfa9 GIT binary patch literal 332 zcmaKnJ8!~35QIs)KO$O*2B$C}=nQm-6e*C9Xital&R%5STF3c8Q2u+4gol+P-D+m{ zo1Ojc{e@pwiy;}M4Q@WfdIy!$DV>Nd=9J|Xnu4WIV5qGkgX8gS4Qfk-8LcS%oAqO@eTr;}(}VjYCvV>&~A+mEs9R%nGr+@q;(|(*fTe#OR9ZmG~j`Dt!SA Fz5!YaZa4q{ literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/glsl/fs_update.bin b/examples/runtime/shaders/glsl/fs_update.bin new file mode 100644 index 0000000000000000000000000000000000000000..a3158b1a647cc181b33391e028697bdcb3cb3cc1 GIT binary patch literal 153 zcmZ<@_F&AHv$IhyOD!tS%+FIWG&11gD$UDG%P-1RC{D~R$Vn}7E=@{RD2*>ktpKsC zxyllYDl_xa70ObRjTOp3;>r2>MJWbAnf%NYh1|r*j*T-O2>M9YwnMu{V836hx@8Y;2iWhQu=YCI>zGTjSx0?YFiSZa8eBkCed2s1{D zLu2@}V}H8}Yhj$y0Z|mrB@}o*xe^$@V=%IECK@F!c;7(_8;Am@8f9I7aVeu;Ai$}l Z>5K0=?`H!nK16>&jYd33qe(YHCU?+?VRrxk literal 0 HcmV?d00001 diff --git a/include/bgfx.h b/include/bgfx.h index 44deafe9..69f4bf74 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -630,6 +630,7 @@ namespace bgfx void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem); /// Update Cube texture. + /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is -Y, 4 is +Z, and 5 is -Z. void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem); /// Destroy texture. diff --git a/premake/example-08-update.lua b/premake/example-08-update.lua new file mode 100644 index 00000000..52a9d78f --- /dev/null +++ b/premake/example-08-update.lua @@ -0,0 +1,39 @@ +project "example-08-update" + uuid "e011e246-5862-11e2-b202-b7cb257a7926" + kind "WindowedApp" + + debugdir (BGFX_DIR .. "examples/runtime/") + + includedirs { + BX_DIR .. "include", + BGFX_DIR .. "include", + } + + files { + BGFX_DIR .. "examples/common/**.cpp", + BGFX_DIR .. "examples/common/**.h", + BGFX_DIR .. "examples/08-update/**.cpp", + BGFX_DIR .. "examples/08-update/**.h", + } + + links { + "bgfx", + } + + configuration { "emscripten" } + targetextension ".bc" + + configuration { "nacl" } + targetextension ".nexe" + + configuration { "nacl", "Release" } + postbuildcommands { + "@echo Stripping symbols.", + "@$(NACL)/bin/x86_64-nacl-strip -s \"$(TARGET)\"" + } + + configuration { "linux" } + links { + "GL", + "pthread", + } diff --git a/premake/premake4.lua b/premake/premake4.lua index 5f58faec..655d2842 100644 --- a/premake/premake4.lua +++ b/premake/premake4.lua @@ -41,3 +41,4 @@ dofile "example-04-mesh.lua" dofile "example-05-instancing.lua" dofile "example-06-bump.lua" dofile "example-07-callback.lua" +dofile "example-08-update.lua" diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 20a6d63e..9f6b4fd8 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -921,6 +921,11 @@ namespace bgfx void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format) { + _width = uint32_max(1, _width); + _height = uint32_max(1, _height); + _depth = uint32_max(1, _depth); + _numMips = uint32_max(1, _numMips); + uint32_t width = _width; uint32_t height = _height; uint32_t depth = _depth; @@ -985,6 +990,7 @@ namespace bgfx tc.m_flags = _flags; tc.m_width = _width; tc.m_height = _height; + tc.m_sides = 0; tc.m_depth = 0; tc.m_numMips = _numMips; tc.m_format = uint8_t(_format); @@ -1023,6 +1029,7 @@ namespace bgfx tc.m_flags = _flags; tc.m_width = _width; tc.m_height = _height; + tc.m_sides = 0; tc.m_depth = _depth; tc.m_numMips = _numMips; tc.m_format = uint8_t(_format); @@ -1060,6 +1067,7 @@ namespace bgfx TextureCreate tc; tc.m_flags = _flags; tc.m_width = _width; + tc.m_height = _width; tc.m_sides = _sides; tc.m_depth = 0; tc.m_numMips = _numMips; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 62785b97..d7ee4e57 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -164,11 +164,8 @@ namespace bgfx { uint32_t m_flags; uint16_t m_width; - union - { - uint16_t m_height; - uint16_t m_sides; - }; + uint16_t m_height; + uint16_t m_sides; uint16_t m_depth; uint8_t m_numMips; uint8_t m_format; diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 97309507..3d859ed0 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -1645,7 +1645,7 @@ namespace bgfx , ... ); #else - deviceCtx->UpdateSubresource(m_ptr, subres, &box, _mem->data, _rect.m_width, 0); + deviceCtx->UpdateSubresource(m_ptr, subres, &box, _mem->data, _rect.m_width*4, 0); #endif // 0 } diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index adce237c..9be473ff 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -1291,11 +1291,25 @@ namespace bgfx case TextureCube: { - D3DLOCKED_RECT rect; - DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &rect, NULL, 0) ); - _pitch = rect.Pitch; + D3DLOCKED_RECT lockedRect; + + if (NULL != _rect) + { + RECT rect; + rect.left = _rect->m_x; + rect.top = _rect->m_y; + rect.right = rect.left + _rect->m_width; + rect.bottom = rect.top + _rect->m_height; + DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &lockedRect, &rect, 0) ); + } + else + { + DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &lockedRect, NULL, 0) ); + } + + _pitch = lockedRect.Pitch; _slicePitch = 0; - return (uint8_t*)rect.pBits; + return (uint8_t*)lockedRect.pBits; } } @@ -1343,6 +1357,7 @@ namespace bgfx if (parseDds(dds, _mem) ) { + m_format = dds.m_type; const TextureFormatInfo& tfi = s_textureFormat[dds.m_type]; bool decompress = false; @@ -1448,6 +1463,7 @@ namespace bgfx { TextureCreate tc; bx::read(&reader, tc); + m_format = (TextureFormat::Enum)tc.m_format; if (tc.m_cubeMap) { @@ -1507,9 +1523,9 @@ namespace bgfx { uint32_t pitch; uint32_t slicePitch; - uint8_t* bits = lock(0, _mip, pitch, slicePitch, &_rect); + uint8_t* bits = lock(_side, _mip, pitch, slicePitch, &_rect); - uint32_t srcpitch = _rect.m_width; + uint32_t srcpitch = _rect.m_width*s_textureFormat[m_format].m_bpp/8; uint32_t dstpitch = pitch; for (uint32_t yy = 0, height = _rect.m_height; yy < height; ++yy) { @@ -1518,7 +1534,7 @@ namespace bgfx memcpy(dst, src, srcpitch); } - unlock(0, _mip); + unlock(_side, _mip); } void Texture::commit(uint8_t _stage) diff --git a/src/renderer_d3d9.h b/src/renderer_d3d9.h index e7a26e97..6eeaf33d 100644 --- a/src/renderer_d3d9.h +++ b/src/renderer_d3d9.h @@ -345,6 +345,7 @@ namespace bgfx D3DTEXTUREADDRESS m_tau; D3DTEXTUREADDRESS m_tav; D3DTEXTUREADDRESS m_taw; + TextureFormat::Enum m_format; Enum m_type; bool m_srgb; };