r/programming • • Dec 20 '10

A Christmas Tree in 1k of Javascript

http://www.romancortes.com/blog/how-i-did-the-1kb-christmas-tree/
1.1k Upvotes

221 comments sorted by

View all comments

Show parent comments

15

u/ascendant512 Dec 21 '10

I was forced to register because of this thread and some comments in it. The Chrome fanboys here are quite ridiculous.

Chrome does not have hardware-accelerated javascript. Hardware-accelerated javascript does not exist. You are mistaking it with WebGL which is present in Safari, Firefox (3 and 4) and Chrome. WebGL has nothing to do with javascript, it is a hardware accelerated <canvas> element (HTML5). Chrome has barely any more hardware acceleration than Microsoft Powerpoint.

Furthermore, the Christmas tree is not rendered with polygons, it is rendered with sprites. It says so right on the front page.

5

u/[deleted] Dec 21 '10

hint: a sprite is a textured polygon. you can very much take advantage of hardware acceleration using just sprites.

Although i doubt this tree example uses any hardware acceleration.

-2

u/ascendant512 Dec 21 '10

Yeah, thanks for the comment. I know that you can hardware-accelerate sprites by turning them into a textured polygon. Hardware accelerated sprites seem to be both common and rare. The only example I can think of is the mouse pointer.

It's just that usually when you see a game or something that uses sprites, it's not 3D accelerated. Even more so the older it is.

Perhaps someone can write a WebGL canvas version of this toy. It'll probably be faster >_>

2

u/ZorbaTHut Dec 21 '10

Actually, the vast majority of modern 2d games are hardware-accelerated. Software rendering is long since dead. I'd say you've got a 95%+ chance of any 2d game in the last five years being hardware-accelerated.

That's probably an underestimation.

0

u/ascendant512 Dec 21 '10

The vast majority of what you think are modern 2D games are not 2D. They are rendered in a 3D environment with a fixed camera perspective using 3D textured polygons (again, not sprites); not because it's easier to do it that way but because it's much, much faster. The rest of them use non-hardware accelerated java or non-hardware accelerated flash. Which use sprites. Which use the CPU.

You are wrong, go away.

5

u/phort99 Dec 21 '10

So, because hardware-accelerated 2D games use some GPU tricks that involve them being rendered in the same manner as 3D graphics, that makes them suddenly 3D? All it is is a different architecture for doing exactly the same thing. Because a shader and a textured quad get involved, suddenly it's 3D?

WebGL is not the only hardware acceleration tech making its way into browsers, canvas is also getting hardware acceleration. IE9 and chrome both use hardware accelerated canvas, from what I understand.

What exactly is the point you're trying to make? You started out arguing that hardware accelerated javascript does not exist. I don't see anybody trying to make the argument that it does.

It seems that javascript is one of the things that is fully output with software only. If there was hardware acceleration then it would be blazing fast.

This comment was probably not talking about hardware-accelerating javascript, it was talking about the output that javascript controls: when the script says "draw this image here," does the CPU set each pixel or does the GPU?

1

u/ascendant512 Dec 21 '10

Oh gosh, you're right

I misread the original comment by tgc1. But then ZorbaTHut came in with implications that more than just <canvas>, <video> and CSS is accelerated. I really should just stop replying to him.

There are also other commenters that chrome's hardware acceleration is responsible for differences in drawing speed for the Christmas tree and I was mixing up posts. That's really what made me lose it in the first place.

1

u/phort99 Dec 21 '10 edited Dec 21 '10

Oh good, for a minute I thought you were one of those people that just clung to their argument until the other party gave up. ;) Like this ridiculous tirade: http://www.reddit.com/r/linux/comments/ecejv/wouldnt_it_have_been_awesome_if_netflix_released/c171b1m

Unrelated: today I learned that you can format text as code in the middle of a sentence.

Blah blah blah `code` blah  

Gives: Blah blah blah code blah

5

u/ZorbaTHut Dec 21 '10

So what exactly is the difference between a 2D game and a game "rendered in a 3D environment with a fixed camera perspective using 3D textured polygons"?

If it looks like a duck and quacks like a duck, it's a duck. I'd call that a 2D game.

-3

u/ascendant512 Dec 21 '10

At this point, I suspect you're just being deliberately dense, but to help other redditors who might be confused:

It's a 3D game if it uses 3D rendering APIs such as Direct3D (a part of DirectX) or OpenGL and the accelerated 3D rendering pathways in the GPU for its rendering engine.

Real 2D games usually are stuff like solitaire and Linux games that use GTK and QT.

8

u/ZorbaTHut Dec 21 '10

I disagree strongly.

A game's "3dness" or "2dness" doesn't depend on the rendering pathways involved. By that definition, no game on any modern console system can be 2d, since they literally don't have those layers.

Are you implying that an implementation of GTK or QT that used 3d hardware - like, say, Compiz on Linux, which GTK and QT can render under - magically turns 2d games into 3d games? What about the OSX Quartz Compositor? Is that a "3d window manager" because it uses 3d hardware? Is Windows 7 a 3d operating system?

This definition is useless to the point of inanity. The only possible reasonable definition of "2d" vs "3d" is whether the game makes use of effects and gameplay techniques that are generally considered 3d. Super Meat Boy is a 2d game because it uses 2d graphics techniques to provide a 2d game experience. The fact that it uses 3d hardware for special effects and rendering is irrelevant.

Are you seriously going to claim that this is a 3d game?

2

u/[deleted] Dec 21 '10

Agreed. The definition proposed by ascendant512 shows a profound misunderstanding of the difference between what something is and how it was made. His definition is like saying "it isn't bread because you didn't make it with a bread-maker."

The difference between 2D and 3D is in the perspective and how the depth dimension is used. In 2D the perspective is orthogonal and the depth is used as a way of ordering drawing without having to do it yourself (lazyness!). In 3D the perspective is, well, perspective and the depth is used (equally with the other two dimensions) to reshape objects based on distance and angle.

-1

u/ascendant512 Dec 21 '10

I guess i deserve to be nitpicked on semantics.

It is a 2D game. It still uses 3D graphics, though.

And Chrome is not hardware accelerating the Christmas tree! I'm out.

→ More replies (0)

1

u/altrego99 Dec 21 '10

The vast majority of what you think are modern 2D games are not 2D

I like Duke Nukem Manhattan project, which falls in this category :) Awesome game!

3

u/ZorbaTHut Dec 21 '10

Hardware Acceleration is Coming to Chrome

You're somewhat out of date. They're trying to get hardware acceleration working for things as simple as webpage layouts. No, the javascript isn't accelerated, but the actual compositing of the sprites certainly would be. And that tree uses a shitload of sprites with alpha channels.

0

u/ascendant512 Dec 21 '10

Take a closer look at the link you posted. It still has nothing to do with javascript, and everything to do with what I said it did. The examples at the bottom of this description and the description itself say that the acceleration's focus is 1. HTML5 and 2. CSS3.

This is in addition to the original hardware acceleration which copies parts of a page already rendered by the CPU directly into the GPU framebuffer. The CPU still does the javascript, and the javascript still does the semitransparent sprites.

4

u/ZorbaTHut Dec 21 '10

Read your own comment again.

Chrome does not have hardware-accelerated javascript. Hardware-accelerated javascript does not exist. You are mistaking it with WebGL which is present in Safari, Firefox (3 and 4) and Chrome. WebGL has nothing to do with javascript, it is a hardware accelerated <canvas> element (HTML5). Chrome has barely any more hardware acceleration than Microsoft Powerpoint.

You're right that there's no hardware-accelerated javascript. Everything else in that paragraph is wrong. Chrome's hardware acceleration isn't WebGL, this acceleration isn't based on <canvas>, and Chrome is working on having significantly more hardware acceleration than Microsoft Powerpoint.

Hardware acceleration doesn't imply polygons. You can do it with sprites also. That's what the Chrome team is working on.

And just to stave off the inevitable, while it might not be accelerating Javascript itself, it certainly can accelerate the sprites that are placed by Javascript.

-2

u/ascendant512 Dec 21 '10 edited Dec 21 '10

If you don't bother reading Google's own explanation of how Chrome's hardware acceleration works, then what is the point in reasoning with you? It clearly states:

  • Layer has 3D or perspective transform CSS properties

  • Layer is used by <video> element using accelerated video decoding

  • Layer is used by a <canvas> element with a 3D context

  • Layer uses a CSS animation for its opacity or uses an animated webkit transform

And

Even with hardware acceleration enabled, pages that don't contain a <video> or WebGL elements and don't make use of 3D CSS transformations/animation use the software path.

In the example of this article (regular html and javascript) the new hardware-accelerated features in Chrome would never even be invoked.

And finally, Microsoft PowerPoint (an example I used to illustrate a common and not hotly debated piece of software) features GPU acceleration for animations, transitions, and video decoding. I can't even think of something that Chrome does on top of that either in current builds or future plans.

In short, you are a rabid fanboy.

EDIT: formatting

5

u/ZorbaTHut Dec 21 '10

If you go and read the original link yet again:

Vangelis Kokkevis, one of the engineers behind the Chromium project, notes that Chrome currently only uses hardware acceleration for displaying some content. Now that the basic infrastructure is in place, however, the Chromium team expects to move "even more of the rendering from the CPU to the GPU to achieve impressive speedups."

It's a work in progress. They're not done yet. The page you're using as rock-solid evidence is already months out of date.

I have no idea why you're calling me a fanboy. I use both Chrome and Firefox frequently, and I haven't mentioned anything except a feature that the Chrome devs have been talking about publicly. All I can conclude is that you have some deep loathing of Google and can't bear to hear anything good about them.

-1

u/ascendant512 Dec 21 '10

Since this is actually still slightly relevant to the thread

I'm calling you a fanboy because you're victim to hype and misinformation about Chrome's rendering engine propagated by people like yourself. You are posting as fact that it can do things it can't.

even more of the rendering from the CPU to the GPU

is referring to the CSS3 transforms and other things I listed, not a nebulous generalization for all images and video.

the actual compositing of the sprites certainly would be. it certainly can accelerate the sprites that are placed by Javascript.

No, this is not a current or upcoming feature in Chrome. Chrome will only accelerate images placed with CSS, and only some of them at that.

As far as my deep loathing of Google and Google-related products: you're ridiculous.

→ More replies (1)

1

u/[deleted] Dec 21 '10

Just to clear up, I didn't mean that the browser renders polys, just an example of cpu vs. gpu usage. And I was only wondering if it were the case. I'm not even using chrome, fyi so i'm by no means a fan boy. Matter of fact I have yet to install and try it.

But anyway, thank you for the further info. I know about open GL but not WebGL. I'm going to check that out.

3

u/ascendant512 Dec 21 '10

I'm sorry for calling you a fanboy.

It is probably impossible to hardware accelerate JS, at least not without severely cutting its usefulness. However, it's not really that bad. All the fast javascript engines use JIT (which attempts to make it run as fast as natively compiled code if you don't know), and JS by its nature as a general purpose scripting language is more suited to a general purpose processor anyway.

1

u/techdawg667 Dec 21 '10

then the speed advantage of chrome over firefox is ever more awesome now!

→ More replies (2)

1

u/funnynickname Dec 21 '10

I've got a single core 2.8ghz with 3 gigs and firefox just pinned at 100% then crashed. Chrome renders it flawlessly and fast. I don't believe it is hardware acceleration. Chrome is just seriously optimized for this stuff, etc. Translates the code in to machine code very efficiently.