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
use bytes::Bytes;
use std::{fs::File, io::Read};
use warmy::{Load, Loaded, SimpleKey, Storage};
use crate::{
foundation::resource::{error::Error, Store, StoreKey},
platform::{
core::{TextureFormat, TextureSampleType},
gles::{core20::gl, enums::*},
},
};
use super::*;
/// By default should be TextureFormat::Rgba32Uint
/// For Depth And Stencil use TextureFormat::Depth32Float,
/// TextureFormat::Depth24Plus, TextureFormat::Depth24PlusStencil8
///
/// It not yet handled Compressed textures like a:
/// DXT1, DXT3, DXT5, RGTC1, RGTC2, BPTC (Bc1, Bc2, Bc3, Bc4, Bc5 Bc6, Bc7),
/// Etc2, Eac, Etc, Astc
///
// implements Canvas implements Resource
#[derive(Debug, Clone, Copy)]
pub struct Image {
// static
// pub assets: AssetManager,
pub width: u32,
pub height: u32,
pub real_width: u32,
pub real_height: u32,
pub format: TextureFormat,
pub tex: Option<u32>, // = -1;
pub framebuffer: Option<u32>, // = -1;
pub depth_buffer: Option<u32>,
pub stencil_buffer: Option<u32>,
// let bytes: Bytes = None;
}
impl Default for Image {
fn default() -> Self {
Self {
width: 0,
height: 0,
real_width: 0,
real_height: 0,
format: TextureFormat::Rgba32Float,
tex: None, // = -1;
framebuffer: None, // = -1;
depth_buffer: None,
stencil_buffer: None,
// let bytes: Bytes = None;
}
}
}
impl Image {
// return image and still binded to texture so
// you can load data or switch to default texture
pub fn new(width: u32, height: u32, format: TextureFormat, render_target: bool) -> Self {
let tex = Self::create_texture();
let mut instance = Self {
width,
height,
real_width: Image::upper_power_of_two(width),
real_height: Image::upper_power_of_two(height),
format,
tex: Some(tex),
framebuffer: None,
depth_buffer: None,
stencil_buffer: None,
};
// Bind the texture object
gl::bind_texture(GL_TEXTURE_2D, tex);
// Sys.gl.pixelStorei(Sys.gl.UNPACK_FLIP_Y_WEBGL, true);
// Set the filtering mode
gl::tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR as i32);
gl::tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR as i32);
// do not repeat texture horizontal
gl::tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE as i32);
// do not repeat texture vertical
gl::tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE as i32);
// let format_info = format.describe();
if render_target {
let framebuffer = Self::create_framebuffer();
instance.framebuffer = Some(framebuffer);
gl::bind_framebuffer(GL_FRAMEBUFFER, framebuffer);
gl::tex_image_2d(
GL_TEXTURE_2D,
0,
GL_RGBA as i32,
instance.real_width as i32,
instance.real_height as i32,
0,
GL_RGBA,
if format == TextureFormat::Rgba32Float {
GL_FLOAT
} else {
GL_UNSIGNED_BYTE
},
&[] as &[u8],
);
gl::framebuffer_texture_2d(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
match format {
TextureFormat::Depth24Plus | TextureFormat::Depth32Float => {
instance.setup_depth_buffer_only();
}
TextureFormat::Depth24PlusStencil8 => {
// #if debug
// log::info!("DepthAndStencilFormat 'Depth24Stencil8' not (yet?) supported on android, using target defaults");
// #end
instance.run_depth_and_stencil_setup_chain();
}
_ => {
panic!("should use renderTarget with Depth and Stencil formats only");
}
}
gl::bind_framebuffer(GL_FRAMEBUFFER, 0);
} else {
// // why we need to init it here?
// match format {
// TextureFormat::R8Uint => {
// gl::tex_image_2d(
// GL_TEXTURE_2D,
// 0,
// GL_LUMINANCE as i32,
// instance.real_width as i32,
// instance.real_height as i32,
// 0,
// GL_LUMINANCE,
// GL_UNSIGNED_BYTE,
// &[] as &[u8],
// );
// }
// TextureFormat::Rgba32Float => {
// gl::tex_image_2d(
// GL_TEXTURE_2D,
// 0,
// GL_RGBA as i32,
// instance.real_width as i32,
// instance.real_height as i32,
// 0,
// GL_RGBA,
// GL_FLOAT,
// &[] as &[u8],
// );
// }
// TextureFormat::Rgba8Uint => {
// gl::tex_image_2d(
// GL_TEXTURE_2D,
// 0,
// GL_RGBA as i32,
// instance.real_width as i32,
// instance.real_height as i32,
// 0,
// GL_RGBA,
// GL_UNSIGNED_BYTE,
// &[] as &[u8],
// );
// }
// _ => {
// gl::tex_image_2d(
// GL_TEXTURE_2D,
// 0,
// GL_RGBA as i32,
// instance.real_width as i32,
// instance.real_height as i32,
// 0,
// gl::RGBA,
// gl::UNSIGNED_BYTE,
// &[] as &[u8],
// );
// }
// }
}
// Sys.gl.generateMipmap(Sys.gl.TEXTURE_2D);
// // return to default
// gl::bind_texture(gl::TEXTURE_2D, 0);
instance
}
pub fn create(width: u32, height: u32, format: TextureFormat) -> Image {
Image::new(width, height, format, false)
}
// // android only
// // @:allow(LoaderImpl)
// // static createFromFile
// pub fn from_file(filename: String) -> Image {
// let b = BitmapFactory.decodeStream(assets.open(filename)); // IO Exception here
// let image = Image::new(b.getWidth(), b.getHeight(), TextureFormat.RGBA32, false, DepthStencilFormat.NoDepthAndStencil);
// gl::bind_texture(gl::TEXTURE_2D, image.tex);
// gl::tex_image_2d(gl::TEXTURE_2D, 0, gl::RGBA, image.realWidth, image.realHeight, 0, gl::RGBA, gl::UNSIGNED_BYTE, None);
// GLUtils.texSubImage2D(gl::TEXTURE_2D, 0, 0, 0, b, gl::RGBA, gl::UNSIGNED_BYTE);
// // let buffer = ByteBuffer.allocateDirect(b.getWidth() * b.getHeight() * 4);
// // b.copyPixelsToBuffer(buffer);
// // gl::glTexImage2D(gl::TEXTURE_2D, 0, gl::RGBA, image.realWidth, image.realHeight, 0, gl::RGBA, gl::UNSIGNED_BYTE, buffer);
// // gl::tex_sub_image_2d(gl::TEXTURE_2D, 0, 0, 0, b.getWidth(), b.getHeight(), gl::RGBA, gl::UNSIGNED_BYTE, buffer);
// // GLUtils.texImage2D(gl::TEXTURE_2D, 0, b, 0);
// image
// }
// native only
// TODO: we should also handle with compressed types
pub fn from_file(filename: &str) -> Image {
use ::image::ColorType;
// Use tightly packed data
gl::pixel_storei(GL_UNPACK_ALIGNMENT, 1); // ?
let im = ::image::open(filename).unwrap();
// // image color type
// match im.color() {
// // Pixel is 8-bit luminance
// ColorType::L8 => {
// // mean grayscale
// }
// // Pixel contains 8-bit R, G and B channels
// ColorType::Rgb8 => {}
// // Pixel is 8-bit RGB with an alpha channel
// ColorType::Rgba8 => {}
// _ => {
// panic!("Unsupported color format for image {}", filename)
// }
// }
// seems we handle only rgba8 images
let im = im.to_rgba8();
Self::from_bytes(
im.as_raw().as_slice(),
im.width(),
im.height(),
TextureFormat::Rgba8Uint,
)
}
pub fn from_bytes(bytes: &[u8], width: u32, height: u32, format: TextureFormat) -> Image {
let image = Image::new(width, height, format, false);
gl::bind_texture(GL_TEXTURE_2D, image.tex.unwrap());
// Load the texture
gl::tex_image_2d(
GL_TEXTURE_2D,
0,
GL_RGBA as i32,
width as i32,
height as i32,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
bytes,
);
image
}
// static
pub fn create_3d(width: i32, height: i32, depth: i32, format: TextureFormat) -> Image {
unimplemented!()
}
// // static
pub fn create_render_target(
width: u32,
height: u32,
format: TextureFormat,
anti_aliasing_samples: u32, /*= 1*/
context_id: u32, /*= 0*/
) -> Image {
Image::new(width, height, format, true)
}
// static
pub fn from_bytes_3d(
bytes: Bytes,
width: u32,
height: u32,
depth: u32,
format: TextureFormat,
) -> Image {
unimplemented!()
}
// TODO: should deal later
fn run_depth_and_stencil_setup_chain(&self) {
println!("runDepthAndStencilSetupChain");
// let setupModes = [self.setup_oesExtension(), self.setup_separate_buffers()];
// let succeeded = false;
// for setup in setupModes {
// let result = setup();
// Self::logFramebufferStatus(result);
// if result == GL_FRAMEBUFFER_COMPLETE {
// succeeded = true;
// log::info!("working depth/stencil combination found");
// break;
// }
// log::info!("trying next setup");
// }
// if !succeeded {
// log::info!("no valid depth/stencil combination found");
// }
}
fn setup_depth_buffer_only(&mut self) -> u32 {
log::info!("GL_DEPTH_COMPONENT16 setup");
let depth_buffer = gl::gen_renderbuffer();
gl::bind_renderbuffer(GL_RENDERBUFFER, depth_buffer);
gl::renderbuffer_storage(
GL_RENDERBUFFER,
GL_DEPTH_COMPONENT16,
self.real_width as i32,
self.real_height as i32,
);
gl::framebuffer_renderbuffer(
GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,
depth_buffer,
);
gl::bind_renderbuffer(GL_RENDERBUFFER, 0);
let result = gl::check_framebuffer_status(GL_FRAMEBUFFER);
// TODO: WTF?
if result != GL_FRAMEBUFFER_COMPLETE {
gl::delete_renderbuffers(&[depth_buffer]);
}
self.depth_buffer = Some(depth_buffer);
result
}
fn setup_oes_extension(&mut self) -> u32 {
log::info!("GL_OES_packed_depth_stencil");
let depth_buffer = gl::gen_renderbuffer(); // TODO: WTF?
gl::bind_texture(GL_TEXTURE_2D, depth_buffer);
gl::tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR as i32);
// TODO: // need to deal urgent // DV
// gl::tex_image_2d(GL_TEXTURE_2D, 0, GLES11Ext::GL_DEPTH_STENCIL_OES, self.myRealWidth, self.myRealWidth, 0, GLES11Ext.GL_DEPTH_STENCIL_OES,
// GLES11Ext::GL_UNSIGNED_INT_24_8_OES, None);
gl::framebuffer_texture_2d(
GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D,
depth_buffer,
0,
);
gl::framebuffer_texture_2d(
GL_FRAMEBUFFER,
GL_STENCIL_ATTACHMENT,
GL_TEXTURE_2D,
depth_buffer,
0,
);
gl::bind_texture(GL_TEXTURE_2D, 0);
let result = gl::check_framebuffer_status(GL_FRAMEBUFFER);
// TODO: WTF?
if result != GL_FRAMEBUFFER_COMPLETE {
gl::delete_renderbuffers(&[depth_buffer]);
}
self.depth_buffer = Some(depth_buffer);
result
}
// TODO (DK)
// -doesn't fail, but doesn't work on my htc desire x -.-
// -fails on galaxy s4 at work
fn setup_separate_buffers(&mut self) -> u32 {
log::info!("GL_DEPTH_COMPONENT16 / GL_STENCIL_INDEX8 setup");
// self.depthStencilBuffers = gl::gen_renderbuffers(2);
// let depthBuffer = self.depthStencilBuffers[0];
// let stencilBuffer = self.depthStencilBuffers[1];
// gl::bind_renderbuffer(GL_RENDERBUFFER, depthBuffer);
// gl::renderbuffer_storage(
// GL_RENDERBUFFER,
// GL_DEPTH_COMPONENT16,
// self.real_width as i32,
// self.real_height as i32,
// );
// gl::bind_renderbuffer(GL_RENDERBUFFER, stencilBuffer);
// gl::renderbuffer_storage(
// GL_RENDERBUFFER,
// GL_STENCIL_INDEX8,
// self.real_width as i32,
// self.real_height as i32,
// );
// gl::framebuffer_renderbuffer(
// GL_FRAMEBUFFER,
// GL_DEPTH_ATTACHMENT,
// GL_RENDERBUFFER,
// depthBuffer,
// );
// gl::framebuffer_renderbuffer(
// GL_FRAMEBUFFER,
// GL_STENCIL_ATTACHMENT,
// GL_RENDERBUFFER,
// stencilBuffer,
// );
// gl::bind_renderbuffer(GL_RENDERBUFFER, 0);
// let result = gl::check_framebuffer_status(GL_FRAMEBUFFER);
// if result != GL_FRAMEBUFFER_COMPLETE {
// gl::delete_renderbuffers(self.depthStencilBuffers.as_slice());
// }
// result
unimplemented!()
}
// static
fn convert_framebuffer_status(status: u32) -> String {
let message = if status == GL_FRAMEBUFFER_COMPLETE {
"complete"
} else if status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT {
"incomplete attachments"
} else if status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT {
"incomplete missing attachments"
} else if status == GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS {
"incomplete dimensions"
} else if status == GL_FRAMEBUFFER_UNSUPPORTED {
"invalid combination of attachments"
} else {
"unknown"
};
message.into()
}
//static
fn log_framebuffer_status(status: u32) {
let message = Self::convert_framebuffer_status(status);
log::info!("framebuffer status '{}'", message);
}
// pub let g1(get, never): graphics1.Graphics;
// fn get_g1(): graphics1.Graphics {
// if graphics1 == None {
// graphics1 = new graphics2.Graphics1(this);
// }
// return graphics1;
// }
// pub let g2(get, never): graphics2.Graphics;
// fn get_g2(): graphics2.Graphics {
// if graphics2 == None {
// graphics2 = new graphics4.Graphics2(this);
// }
// return graphics2;
// }
// pub let g4(get, never): graphics4.Graphics;
// fn get_g4(): graphics4.Graphics {
// if graphics4 == None {
// graphics4 = new android.Graphics(this);
// }
// return graphics4;
// }
pub fn unload(&mut self) {
if self.tex.is_some() {
gl::delete_textures(&[self.tex.unwrap()]);
self.tex = None;
}
if self.framebuffer.is_some() {
gl::delete_buffers(&[self.framebuffer.unwrap()]);
self.framebuffer = None;
}
if let Some(buffer) = self.depth_buffer {
gl::delete_renderbuffers(&[buffer]);
}
if let Some(buffer) = self.stencil_buffer {
gl::delete_renderbuffers(&[buffer]);
}
}
// static
fn create_framebuffer() -> u32 {
let framebuffers = gl::gen_framebuffers(1);
framebuffers[0]
}
// static
fn create_texture() -> u32 {
let textures = gl::gen_textures(1);
textures[0]
}
pub fn make_active(&self, texture_unit: u32) {
gl::active_texture(GL_TEXTURE0 + texture_unit);
gl::bind_texture(GL_TEXTURE_2D, self.tex.unwrap());
}
// pub let width(get, never) -> i32;
fn width(&self) -> u32 {
self.width
}
// pub let height(get, never) -> i32;
fn height(&self) -> u32 {
self.height
}
// pub let depth(get, never) -> i32;
fn depth(&self) -> u32 {
1
}
// pub let format(get, never): TextureFormat;
fn format(&self) -> TextureFormat {
self.format
}
// pub let realWidth(get, never) -> i32;
fn real_width(&self) -> u32 {
self.real_width
}
// pub let realHeight(get, never) -> i32;
fn real_height(&self) -> u32 {
self.real_height
}
// pub let stride(get, never) -> i32;
fn stride(&self) -> u32 {
// if self.myFormat == TextureFormat::RGBA32 {
// 4 * width
// } else {
// if self.myFormat == TextureFormat::RGBA128 {
// 16 * width
// } else {
// width
// }
// }
unimplemented!()
}
pub fn at(x: i32, y: i32) -> i32 {
0
}
pub fn is_opaque(x: i32, y: i32) -> bool {
// return (b.getPixel(x, y) >> 24) != 0;
true
}
pub fn lock(&self, level: i32 /* = 0*/) -> Bytes {
// let len = if self.myFormat == TextureFormat::RGBA32 {
// 4 * width * height
// } else {
// if myFormat == TextureFormat::RGBA128 {
// 16 * width * height
// } else {
// width * height
// }
// };
// self.bytes = Bytes.alloc(len);
// self.bytes
unimplemented!()
}
pub fn unlock(&self) {
// gl::bind_texture(gl::TEXTURE_2D, self.tex);
// // Sys.gl.pixelStorei(Sys.gl.UNPACK_FLIP_Y_WEBGL, true);
// match self.myFormat {
// TextureFormat::L8 => {
// gl::tex_sub_image_2d(gl::TEXTURE_2D, 0, 0, 0, width, height, gl::LUMINANCE, gl::UNSIGNED_BYTE,
// ByteBuffer.wrap(bytes.getData()));
// }
// TextureFormat::RGBA128 => {
// gl::tex_sub_image_2d(gl::TEXTURE_2D, 0, 0, 0, width, height, gl::RGBA, gl::FLOAT, ByteBuffer.wrap(bytes.getData()));
// }
// TextureFormat::RGBA32 => {
// gl::tex_sub_image_2d(gl::TEXTURE_2D, 0, 0, 0, width, height, gl::RGBA, gl::UNSIGNED_BYTE,
// ByteBuffer.wrap(bytes.getData()));
// }
// _ => {
// gl::tex_sub_image_2d(gl::TEXTURE_2D, 0, 0, 0, width, height, gl::RGBA, gl::UNSIGNED_BYTE,
// ByteBuffer.wrap(bytes.getData()));
// }
// }
// // Sys.gl.generateMipmap(Sys.gl.TEXTURE_2D);
// gl::bind_texture(gl::TEXTURE_2D, 0);
// bytes = None;
unimplemented!()
}
pub fn pixels(&self) -> Bytes {
// return None;
unimplemented!()
}
pub fn generate_mipmaps(&self, levels: i32) {}
pub fn set_mipmaps(&self, mipmaps: Vec<Image>) {}
pub fn set_depth_stencil_from(&self, image: Image) {}
pub fn clear(x: i32, y: i32, z: i32, width: i32, height: i32, depth: i32, color: Color) {}
// static
// pub let maxSize(get, never) -> i32;
// static
fn max_size(&self) -> i32 {
2048
}
// static
// pub let nonPow2Supported(get, never) -> bool {};
// static
fn non_pow2_supported(&self) -> bool {
false
}
// pub static
fn render_targets_inverted_y(&self) -> bool {
true
}
// static
fn upper_power_of_two(v: u32) -> u32 {
// let mut res = v - 1;
// res |= res >> 1;
// res |= res >> 2;
// res |= res >> 4;
// res |= res >> 8;
// res |= res >> 16;
// res += 1;
// res
v
}
}
impl<C> Load<C, StoreKey> for Image {
type Error = Error;
fn load(
key: StoreKey,
storage: &mut Storage<C, StoreKey>,
_: &mut C,
) -> Result<Loaded<Self, StoreKey>, Self::Error> {
// as we only accept filesystem here, we’ll ensure the key is a filesystem one
match key.0 {
SimpleKey::Path(path) => {
let im = ::image::open(&path)
.map_err(|e| Error::IOError(format!("{}: {:?}", e, &path)))?;
let data = im.to_rgba8();
let result = Self::from_bytes(
data.as_raw().as_slice(),
data.width(),
data.height(),
TextureFormat::Rgba8Uint,
);
Ok(result.into())
}
SimpleKey::Logical(_) => Err(Error::CannotLoadFromLogical(
String::from("Load from logical not implemented"),
String::from("No reason"),
)),
}
}
}