1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
#include "GenericBuffer.h"
#include "GraphicsUtil.h"
#include <iostream>
VertexFormatBaseType GetVertexFormatBaseType(VertexFormat format)
{
switch (format) {
case VertexFormat::Float:
case VertexFormat::Float2:
case VertexFormat::Float3:
case VertexFormat::Float4:
return VertexFormatBaseType::Float;
case VertexFormat::Byte:
case VertexFormat::Byte2:
case VertexFormat::Byte3:
case VertexFormat::Byte4:
return VertexFormatBaseType::Byte;
case VertexFormat::UByte:
case VertexFormat::UByte2:
case VertexFormat::UByte3:
case VertexFormat::UByte4:
return VertexFormatBaseType::UByte;
case VertexFormat::Int:
case VertexFormat::Int2:
case VertexFormat::Int3:
case VertexFormat::Int4:
return VertexFormatBaseType::Int;
case VertexFormat::UInt:
case VertexFormat::UInt2:
case VertexFormat::UInt3:
case VertexFormat::UInt4:
return VertexFormatBaseType::UInt;
default:
return VertexFormatBaseType::Float; // std::unreachable();
}
}
GLenum VertexFormatToGLType(VertexFormat format)
{
switch (format)
{
case VertexFormat::Byte:
case VertexFormat::Byte2:
case VertexFormat::Byte3:
case VertexFormat::Byte4:
case VertexFormat::ByteNorm:
case VertexFormat::Byte2Norm:
case VertexFormat::Byte3Norm:
case VertexFormat::Byte4Norm:
return GL_BYTE;
case VertexFormat::UByte:
case VertexFormat::UByte2:
case VertexFormat::UByte3:
case VertexFormat::UByte4:
case VertexFormat::UByteNorm:
case VertexFormat::UByte2Norm:
case VertexFormat::UByte3Norm:
case VertexFormat::UByte4Norm:
return GL_UNSIGNED_BYTE;
case VertexFormat::Float:
case VertexFormat::Float2:
case VertexFormat::Float3:
case VertexFormat::Float4:
return GL_FLOAT;
case VertexFormat::Int:
case VertexFormat::Int2:
case VertexFormat::Int3:
case VertexFormat::Int4:
return GL_INT;
case VertexFormat::UInt:
case VertexFormat::UInt2:
case VertexFormat::UInt3:
case VertexFormat::UInt4:
return GL_UNSIGNED_INT;
default:
return GL_INVALID_ENUM; // std::unreachable()
}
}
GLint VertexFormatToGLSize(VertexFormat format)
{
switch (format)
{
case VertexFormat::Byte:
case VertexFormat::UByte:
case VertexFormat::ByteNorm:
case VertexFormat::UByteNorm:
case VertexFormat::Float:
case VertexFormat::Int:
case VertexFormat::UInt:
return 1;
case VertexFormat::Byte2:
case VertexFormat::UByte2:
case VertexFormat::Byte2Norm:
case VertexFormat::UByte2Norm:
case VertexFormat::Float2:
case VertexFormat::Int2:
case VertexFormat::UInt2:
return 2;
case VertexFormat::Byte3:
case VertexFormat::UByte3:
case VertexFormat::Byte3Norm:
case VertexFormat::UByte3Norm:
case VertexFormat::Float3:
case VertexFormat::Int3:
case VertexFormat::UInt3:
return 3;
case VertexFormat::Byte4:
case VertexFormat::UByte4:
case VertexFormat::Byte4Norm:
case VertexFormat::UByte4Norm:
case VertexFormat::Float4:
case VertexFormat::Int4:
case VertexFormat::UInt4:
return 4;
default:
return 0; // std::unreachable()
}
}
bool VertexFormatIsNormalized(VertexFormat format)
{
switch (format)
{
case VertexFormat::ByteNorm:
case VertexFormat::Byte2Norm:
case VertexFormat::Byte3Norm:
case VertexFormat::Byte4Norm:
case VertexFormat::UByteNorm:
case VertexFormat::UByte2Norm:
case VertexFormat::UByte3Norm:
case VertexFormat::UByte4Norm:
return true;
default:
return false;
}
}
GLboolean VertexFormatToGLNormalized(VertexFormat format)
{
auto normalized = VertexFormatIsNormalized(format);
return normalized ? GL_TRUE : GL_FALSE;
}
std::size_t GetSizeOfVertexFormat(VertexFormat format)
{
switch (format)
{
case VertexFormat::Byte:
case VertexFormat::UByte:
case VertexFormat::ByteNorm:
case VertexFormat::UByteNorm:
return 1;
case VertexFormat::Byte2:
case VertexFormat::UByte2:
case VertexFormat::Byte2Norm:
case VertexFormat::UByte2Norm:
return 2;
case VertexFormat::Byte3:
case VertexFormat::UByte3:
case VertexFormat::Byte3Norm:
case VertexFormat::UByte3Norm:
return 3;
case VertexFormat::Byte4:
case VertexFormat::UByte4:
case VertexFormat::Byte4Norm:
case VertexFormat::UByte4Norm:
return 4;
case VertexFormat::Float:
return 4;
case VertexFormat::Float2:
return 8;
case VertexFormat::Float3:
return 12;
case VertexFormat::Float4:
return 16;
case VertexFormat::Int:
return 4;
case VertexFormat::Int2:
return 8;
case VertexFormat::Int3:
return 12;
case VertexFormat::Int4:
return 16;
case VertexFormat::UInt:
return 4;
case VertexFormat::UInt2:
return 8;
case VertexFormat::UInt3:
return 12;
case VertexFormat::UInt4:
return 16;
default:
return 0; // std::unreachable()
}
}
GenericBuffer::GenericBuffer(buffer_layout layout, GLenum usage)
: m_buffer_usage(usage)
, m_layout(layout)
{
}
GenericBuffer::~GenericBuffer()
{
for (auto i = 0; i < m_desc.size(); ++i) {
auto& glID = desc_glIDs[i];
if (glID) {
glDeleteBuffers(1, &glID);
}
}
if (m_interleavedID) {
glDeleteBuffers(1, &m_interleavedID);
}
}
bool GenericBuffer::bufferData(BufferDataDesc&& desc)
{
m_desc = std::move(desc);
desc_glIDs = std::vector<GLuint>(m_desc.size());
return evaluate();
}
bool GenericBuffer::bufferData(
BufferDataDesc&& desc, const void* data, size_t len, size_t stride)
{
bool ok = true;
m_desc = std::move(desc);
desc_glIDs = std::vector<GLuint>(m_desc.size());
m_interleaved = true;
m_stride = stride;
ok = genBuffer(m_interleavedID, len, data);
return ok;
}
void GenericBuffer::bufferSubData(size_t offset, size_t size, void* data, size_t index)
{
assert("Invalid Desc index" && index < m_desc.size());
assert("Invalid GLDesc index" && index < desc_glIDs.size());
auto glID = m_interleaved ? m_interleavedID : desc_glIDs[index];
glBindBuffer(bufferType(), glID);
glBufferSubData(bufferType(), offset, size, data);
}
void GenericBuffer::bufferReplaceData(size_t offset, size_t len, const void* data)
{
glBindBuffer(bufferType(), m_interleavedID);
glBufferSubData(bufferType(), offset, len, data);
}
bool GenericBuffer::evaluate()
{
if (bufferType() == GL_ELEMENT_ARRAY_BUFFER) {
return seqBufferData();
} else {
switch (m_layout) {
case buffer_layout::SEPARATE:
return sepBufferData();
break;
case buffer_layout::SEQUENTIAL:
return seqBufferData();
break;
case buffer_layout::INTERLEAVED:
return interleaveBufferData();
break;
}
}
return true; // unreacheable/Should be false?
}
bool GenericBuffer::sepBufferData()
{
for (auto i = 0; i < m_desc.size(); ++i) {
// If the specified size is 0 but we have a valid pointer
// then we are going to glVertexAttribXfv X in {1,2,3,4}
const auto& d = m_desc[i];
auto& glID = desc_glIDs[i];
if (d.data_ptr && (m_buffer_usage == GL_STATIC_DRAW)) {
if (d.data_size) {
if (!genBuffer(glID, d.data_size, d.data_ptr)) {
return false;
}
}
}
}
return true;
}
bool GenericBuffer::seqBufferData() {
// this is only going to use a single opengl vbo
m_interleaved = true;
size_t buffer_size { 0 };
for ( auto & d : m_desc ) {
buffer_size += d.data_size;
}
std::vector<std::uint8_t> buffer_data(buffer_size);
auto data_ptr = buffer_data.data();
size_t offset = 0;
for ( auto & d : m_desc ) {
d.offset = offset;
if (d.data_ptr)
memcpy(data_ptr, d.data_ptr, d.data_size);
else
memset(data_ptr, 0, d.data_size);
data_ptr += d.data_size;
offset += d.data_size;
}
return genBuffer(m_interleavedID, buffer_size, buffer_data.data());
}
bool GenericBuffer::interleaveBufferData()
{
const std::size_t bufferCount = m_desc.size();
std::size_t stride = 0;
std::vector<const uint8_t*> data_table(bufferCount);
std::vector<const uint8_t*> ptr_table(bufferCount);
std::vector<std::size_t> size_table(bufferCount);
std::size_t count =
m_desc[0].data_size / GetSizeOfVertexFormat(m_desc[0].m_format);
// Maybe assert that all pointers in d_desc are valid?
for (size_t i = 0; i < bufferCount; ++i) {
auto& d = m_desc[i];
// offset is the current stride
d.offset = stride;
// These must come after so that offset starts at 0
// Size of 3 normals or whatever the current type is
size_table[i] = GetSizeOfVertexFormat(d.m_format);
// Increase our current estimate of the stride by this amount
stride += size_table[i];
// Does the addition of that previous stride leave us on a word boundry?
int m = stride % 4;
stride = (m ? (stride + (4 - m)) : stride);
// data_table a pointer to the begining of each array
data_table[i] = static_cast<const std::uint8_t*>(d.data_ptr);
// We will move these pointers along by the values in the size table
ptr_table[i] = data_table[i];
}
m_stride = stride;
std::size_t interleavedSize = count * stride;
std::vector<std::uint8_t> interleavedData(interleavedSize);
auto iPtr = interleavedData.data();
while (iPtr != (interleavedData.data() + interleavedSize)) {
for (size_t i = 0; i < bufferCount; ++i) {
if (ptr_table[i]) {
memcpy(iPtr, ptr_table[i], size_table[i]);
ptr_table[i] += size_table[i];
}
iPtr += size_table[i];
}
}
m_interleaved = true;
return genBuffer(m_interleavedID, interleavedSize, interleavedData.data());
}
bool GenericBuffer::genBuffer(GLuint& id, size_t size, const void* ptr)
{
glGenBuffers(1, &id);
if (!CheckGLErrorOK(nullptr, "GenericBuffer::genBuffer failed\n"))
return false;
glBindBuffer(bufferType(), id);
if (!CheckGLErrorOK(nullptr, "GenericBuffer::bindBuffer failed\n"))
return false;
glBufferData(bufferType(), size, ptr, GL_STATIC_DRAW);
if (!CheckGLErrorOK(nullptr, "GenericBuffer::bufferData failed\n"))
return false;
return true;
}
void VertexBuffer::bind_attrib(GLuint prg, const BufferDesc& d, GLuint glID)
{
GLint loc = glGetAttribLocation(prg, d.attr_name);
auto type_dim = VertexFormatToGLSize(d.m_format);
auto type = VertexFormatToGLType(d.m_format);
auto data_norm = VertexFormatToGLNormalized(d.m_format);
bool masked = false;
for (GLint lid : m_attribmask)
if (lid == loc)
masked = true;
if (loc >= 0)
m_locs.push_back(loc);
if (loc >= 0 && !masked) {
if (!m_interleaved && glID)
glBindBuffer(bufferType(), glID);
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, type_dim, type, data_norm, m_stride,
reinterpret_cast<const void*>(d.offset));
}
};
VertexBuffer::VertexBuffer(buffer_layout layout, GLenum usage)
: GenericBuffer(layout, usage)
{
}
void VertexBuffer::bind() const
{
// we shouldn't use this one
if (m_interleaved)
glBindBuffer(bufferType(), m_interleavedID);
}
void VertexBuffer::bind(GLuint prg, int index)
{
if (index >= 0) {
glBindBuffer(bufferType(), m_interleavedID);
bind_attrib(prg, m_desc[index], desc_glIDs[index]);
} else {
if (m_interleaved && m_interleavedID)
glBindBuffer(bufferType(), m_interleavedID);
for (auto i = 0; i < m_desc.size(); ++i) {
const auto& d = m_desc[i];
auto glID = desc_glIDs[i];
bind_attrib(prg, d, glID);
}
m_attribmask.clear();
}
}
void VertexBuffer::unbind()
{
for (auto& d : m_locs) {
glDisableVertexAttribArray(d);
}
m_locs.clear();
glBindBuffer(bufferType(), 0);
}
void VertexBuffer::maskAttributes(std::vector<GLint> attrib_locs)
{
m_attribmask = std::move(attrib_locs);
}
void VertexBuffer::maskAttribute(GLint attrib_loc)
{
m_attribmask.push_back(attrib_loc);
}
std::uint32_t VertexBuffer::bufferType() const
{
return GL_ARRAY_BUFFER;
}
void IndexBuffer::bind() const
{
glBindBuffer(bufferType(), m_interleavedID);
}
void IndexBuffer::unbind()
{
glBindBuffer(bufferType(), 0);
}
std::uint32_t IndexBuffer::bufferType() const
{
return GL_ELEMENT_ARRAY_BUFFER;
}
/***********************************************************************
* RENDERBUFFER
***********************************************************************/
#ifdef PURE_OPENGL_ES_2
const static int rbo_lut[rbo::storage::COUNT] = { GL_DEPTH_COMPONENT16,
GL_DEPTH_COMPONENT16 };
#else
const static int rbo_lut[rbo::storage::COUNT] = { GL_DEPTH_COMPONENT16,
GL_DEPTH_COMPONENT24 };
#endif
void rbo::unbind() { glBindRenderbuffer(GL_RENDERBUFFER, 0); }
void renderBuffer_t::genBuffer() {
glGenRenderbuffers(1, &_id);
glBindRenderbuffer(GL_RENDERBUFFER, _id);
glRenderbufferStorage(GL_RENDERBUFFER, rbo_lut[(int)_storage],
_width,
_height);
CheckGLErrorOK(nullptr, "GLRenderBuffer::genBuffer failed");
}
void renderBuffer_t::freeBuffer() { glDeleteRenderbuffers(1, &_id); }
void renderBuffer_t::bind() const { glBindRenderbuffer(GL_RENDERBUFFER, _id); }
void renderBuffer_t::unbind() const { glBindRenderbuffer(GL_RENDERBUFFER, 0); }
/***********************************************************************
* TEXTURE
***********************************************************************/
const static int tex_lut[tex::max_params] = {
#ifdef PURE_OPENGL_ES_2
GL_TEXTURE_2D, GL_TEXTURE_2D,
GL_TEXTURE_2D, GL_RGBA,
GL_RGBA, GL_RGB,
#else
GL_TEXTURE_1D, GL_TEXTURE_2D,
GL_TEXTURE_3D, GL_RED,
GL_RG, GL_RGB,
#endif
GL_RGBA, GL_UNSIGNED_BYTE,
GL_FLOAT, GL_FLOAT,
GL_NEAREST, GL_LINEAR,
GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR,
GL_REPEAT,
GL_CLAMP,
GL_REPEAT, // GL_MIRROR_REPEAT,
#ifdef PURE_OPENGL_ES_2
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
#else
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER,
#endif
GL_CLAMP_TO_EDGE, // GL_MIRROR_CLAMP_TO_EDGE
#ifdef PURE_OPENGL_ES_2
GL_REPLACE, GL_REPLACE
#else
GL_TEXTURE_ENV_MODE, GL_REPLACE
#endif
};
#ifndef GL_R8
#define GL_R8 GL_RGBA
#define GL_RG8 GL_RGBA
#define GL_RGB8 GL_RGB
#define GL_RGBA8 GL_RGBA
#endif
#ifndef GL_R32F
#define GL_R32F GL_R8
#define GL_RG32F GL_RG8
#define GL_RGB32F GL_RGB8
#define GL_RGBA32F GL_RGBA8
#endif
#ifndef GL_R16F
#define GL_R16F GL_R32F
#define GL_RG16F GL_RG32F
#define GL_RGB16F GL_RGB32F
#define GL_RGBA16F GL_RGBA32F
#endif
#ifdef _WEBGL
static int tex_format_internal_byte(tex::format f) {
return tex_lut[(int)f];
};
static int tex_format_internal_float(tex::format f) {
return tex_format_internal_byte(f);
};
static int tex_format_internal_half_float(tex::format f) {
return tex_format_internal_byte(f);
};
#else
static int tex_format_internal_float(tex::format f) {
using namespace tex;
switch (f) {
case format::R:
return GL_R32F;
break;
case format::RG:
return GL_RG32F;
break;
case format::RGB:
return GL_RGB32F;
break;
case format::RGBA:
return GL_RGBA32F;
break;
default:
return GL_RGBA32F;
break;
}
};
static int tex_format_internal_half_float(tex::format f) {
using namespace tex;
switch (f) {
case format::R:
return GL_R16F;
break;
case format::RG:
return GL_RG16F;
break;
case format::RGB:
return GL_RGB16F;
break;
case format::RGBA:
return GL_RGBA16F;
break;
default:
return GL_RGBA16F;
break;
}
};
static int tex_format_internal_byte(tex::format f) {
using namespace tex;
switch (f) {
case format::R:
return GL_R8;
break;
case format::RG:
return GL_RG8;
break;
case format::RGB:
return GL_RGB8;
break;
case format::RGBA:
return GL_RGBA8;
break;
default:
return GL_RGBA8;
break;
}
};
#endif
template <typename T> static int tex_tab(T val) { return tex_lut[(int)val]; }
void tex::env(tex::env_name name, tex::env_param param) {
#ifdef PURE_OPENGL_ES_2
fprintf(stderr,
"No Support for Texture Env\n");
#else
glTexEnvf(GL_TEXTURE_ENV, tex_tab(name), tex_tab(param));
#endif
}
void textureBuffer_t::genBuffer() {
GLenum dim = tex_tab(_dim);
glGenTextures(1, &_id);
glBindTexture(dim, _id);
glTexParameteri(dim, GL_TEXTURE_MAG_FILTER, tex_tab(_sampling[0]));
glTexParameteri(dim, GL_TEXTURE_MIN_FILTER, tex_tab(_sampling[1]));
glTexParameteri(dim, GL_TEXTURE_WRAP_S, tex_tab(_sampling[2]));
if (_sampling[3])
glTexParameteri(dim, GL_TEXTURE_WRAP_T, tex_tab(_sampling[3]));
#ifndef PURE_OPENGL_ES_2
if (_sampling[4])
glTexParameteri(dim, GL_TEXTURE_WRAP_R, tex_tab(_sampling[4]));
#endif
CheckGLErrorOK(nullptr, "GLTextureBuffer::genBuffer failed");
}
void textureBuffer_t::freeBuffer() { glDeleteTextures(1, &_id); }
void textureBuffer_t::texture_data_1D(int width, const void *data) {
#ifdef PURE_OPENGL_ES_2
fprintf(stderr,
"No support for 1D textures\n");
#else
using namespace tex;
_width = width;
bind();
switch (_type) {
case data_type::HALF_FLOAT:
glTexImage1D(GL_TEXTURE_1D, 0, tex_format_internal_half_float(_format), _width, 0,
tex_tab(_format), tex_tab(tex::data_type::FLOAT), data);
break;
case data_type::FLOAT:
glTexImage1D(GL_TEXTURE_1D, 0, tex_format_internal_float(_format), _width, 0,
tex_tab(_format), tex_tab(_type), data);
break;
case data_type::UBYTE:
glTexImage1D(GL_TEXTURE_1D, 0, tex_format_internal_byte(_format), _width, 0,
tex_tab(_format), tex_tab(_type), data);
break;
default:
break;
};
CheckGLErrorOK(nullptr, "GLTextureBuffer::texture_data_1D failed");
#endif
}
void textureBuffer_t::texture_data_2D(int width, int height, const void *data) {
using namespace tex;
_width = width;
_height = height;
bind();
switch (_type) {
case data_type::HALF_FLOAT:
glTexImage2D(GL_TEXTURE_2D, 0, tex_format_internal_half_float(_format), _width,
_height, 0, tex_tab(_format), tex_tab(tex::data_type::FLOAT), data);
break;
case data_type::FLOAT:
glTexImage2D(GL_TEXTURE_2D, 0, tex_format_internal_float(_format), _width,
_height, 0, tex_tab(_format), tex_tab(_type), data);
break;
case data_type::UBYTE:
glTexImage2D(GL_TEXTURE_2D, 0, tex_format_internal_byte(_format), _width,
_height, 0, tex_tab(_format), tex_tab(_type), data);
break;
default:
break;
}
CheckGLErrorOK(nullptr, "GLTextureBuffer::texture_data_2D failed");
}
void textureBuffer_t::texture_subdata_2D(
int xoffset, int yoffset, int width, int height, const void* data)
{
using namespace tex;
bind();
switch (_type) {
case data_type::HALF_FLOAT:
glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, width, height,
tex_tab(_format), tex_tab(tex::data_type::FLOAT), data);
break;
case data_type::FLOAT:
case data_type::UBYTE:
glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, width, height,
tex_tab(_format), tex_tab(_type), data);
break;
}
CheckGLErrorOK(nullptr, "GLTextureBuffer::texture_subdata_2D failed");
}
void textureBuffer_t::texture_data_3D(int width, int height, int depth,
const void *data) {
#ifdef PURE_OPENGL_ES_2
fprintf(stderr,
"No support for 3D textures\n");
#else
_width = width;
_height = height;
_depth = depth;
bind();
switch(_type) {
case tex::data_type::HALF_FLOAT:
glTexImage3D(GL_TEXTURE_3D, 0, tex_format_internal_half_float(_format), _width,
_height, _depth, 0, tex_tab(_format), tex_tab(tex::data_type::FLOAT), data);
break;
case tex::data_type::FLOAT:
glTexImage3D(GL_TEXTURE_3D, 0, tex_format_internal_float(_format), _width,
_height, _depth, 0, tex_tab(_format), tex_tab(tex::data_type::FLOAT), data);
break;
case tex::data_type::UBYTE:
glTexImage3D(GL_TEXTURE_3D, 0, tex_format_internal_byte(_format), _width,
_height, _depth, 0, tex_tab(_format), tex_tab(_type), data);
break;
default:
break;
}
#endif
CheckGLErrorOK(nullptr, "GLTextureBuffer::texture_data_3D failed");
}
void textureBuffer_t::bind() const { glBindTexture(tex_tab(_dim), _id); }
void textureBuffer_t::bindToTextureUnit(std::uint8_t textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
bind();
}
void textureBuffer_t::unbind() const { glBindTexture(tex_tab(_dim), 0); }
/***********************************************************************
* FRAMEBUFFER
***********************************************************************/
const static int fbo_lut[fbo::attachment::COUNT] = {
GL_COLOR_ATTACHMENT0,
#if defined(PURE_OPENGL_ES_2) && !defined(_WEBGL)
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT0,
#else
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3,
#endif
GL_DEPTH_ATTACHMENT
};
template <typename T> static int fbo_tab(T val) { return fbo_lut[(int)val]; }
void fbo::unbind() { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
void frameBuffer_t::genBuffer() { glGenFramebuffers(1, &_id); }
void frameBuffer_t::freeBuffer() { glDeleteFramebuffers(1, &_id); }
void frameBuffer_t::attach_texture(textureBuffer_t *texture,
fbo::attachment loc) {
size_t id = texture->get_hash_id();
_attachments.emplace_back(id, loc);
bind();
glFramebufferTexture2D(GL_FRAMEBUFFER, fbo_tab(loc), GL_TEXTURE_2D,//tex_tab(texture->_dim),
texture->_id, 0);
checkStatus();
}
void frameBuffer_t::attach_renderbuffer(renderBuffer_t *renderbuffer,
fbo::attachment loc) {
size_t id = renderbuffer->get_hash_id();
_attachments.emplace_back(id, loc);
bind();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, fbo_tab(loc), GL_RENDERBUFFER,
renderbuffer->_id);
checkStatus();
}
void frameBuffer_t::bind() const {
glBindFramebuffer(GL_FRAMEBUFFER, _id);
}
void frameBuffer_t::checkStatus() {
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch (status) {
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
printf("Incomplete attachment\n");
break;
#ifndef PURE_OPENGL_ES_2
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
printf("Incomplete dimensions\n");
break;
#endif
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
printf("Incomplete missing attachment\n");
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
printf("Framebuffer combination unsupported\n");
break;
}
}
/***********************************************************************
* RENDERTARGET
***********************************************************************/
renderTarget_t::~renderTarget_t() {
for (auto &t : _textures)
delete t;
if (_fbo)
delete _fbo;
if (_rbo && !_shared_rbo)
delete _rbo;
}
void renderTarget_t::layout(std::vector<rt_layout_t> &&desc,
renderBuffer_t *with_rbo) {
_fbo = new frameBuffer_t();
if (with_rbo) {
_rbo = with_rbo;
_shared_rbo = true;
} else {
_rbo = new renderBuffer_t(_size.x, _size.y, rbo::storage::DEPTH24);
}
for (auto &d : desc) {
if (!d.width)
d.width = _size.x;
if (!d.height)
d.height = _size.y;
tex::data_type type;
switch (d.type) {
case rt_layout_t::UBYTE:
type = tex::data_type::UBYTE;
break;
case rt_layout_t::FLOAT:
type = tex::data_type::FLOAT;
break;
default:
printf("Error: %s:%d\n", __FILE__, __LINE__);
return;
}
tex::format format;
switch (d.nchannels) {
case 1:
format = tex::format::R;
break;
case 2:
format = tex::format::RG;
break;
case 3:
format = tex::format::RGB;
break;
case 4:
format = tex::format::RGBA;
break;
default:
printf("Error: %s:%d\n", __FILE__, __LINE__);
return;
}
_textures.push_back(new textureBuffer_t(
format, type, tex::filter::LINEAR, tex::filter::LINEAR,
tex::wrap::CLAMP, tex::wrap::CLAMP));
auto tex = _textures.back();
tex->texture_data_2D(d.width, d.height, nullptr);
fbo::attachment loc;
switch (_textures.size()) {
case 1:
loc = fbo::attachment::COLOR0;
break;
case 2:
loc = fbo::attachment::COLOR1;
break;
case 3:
loc = fbo::attachment::COLOR2;
break;
case 4:
loc = fbo::attachment::COLOR3;
break;
default:
loc = fbo::attachment::COLOR0;
// print error
break;
}
_fbo->attach_texture(tex, loc);
}
_fbo->attach_renderbuffer(_rbo, fbo::attachment::DEPTH);
_desc = std::move(desc);
CheckGLErrorOK(nullptr, "GLRenderBuffer::layout failed\n");
}
void renderTarget_t::resize(shape_type size) {
_size = size;
if (!_shared_rbo) {
delete _rbo;
_rbo = nullptr;
}
for (auto i : _textures) {
delete i;
}
_textures.clear();
delete _fbo;
std::vector<rt_layout_t> desc;
for (auto& i : _desc) {
desc.emplace_back(i.nchannels, i.type, size.x, size.y);
}
layout(std::move(desc), _rbo);
}
void renderTarget_t::bind(bool clear) const {
_fbo->bind();
if (clear) {
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
}
|